Compiling multiple C files with gcc
You should define the functions that you want to call from modules.c
into main.c
into a header file, let us say modules.h
, and include that header file in main.c
. Once you have the header file, please compile both of the files together: gcc main.c modules.c -o output
Two additional notes. First, modules.o
is an object file and it should not be included in a C source file. Second, we cannot have a C file have a .o
extension. You should actually get an error when compiling a .o
file. Something like:
$ cat t.o
int main() {
int x = 1;
return 0;
}
$
$ gcc t.o
ld: warning: in t.o, file is not of required architecture
Undefined symbols:
"_main", referenced from:
start in crt1.10.6.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
$
Compile multiple C source fles into a unique object file
You can't compile multiple source files into a single object file. An object file is the compiled result of a single source file and its headers (also known as a translation unit).
If you want to combine compiled files, it's usually combined into a static library using the ar
command:
$ ar cr libfoo.a file1.o file2.o file3.o
You can then use this static library when linking, either passing it directly as an object file:
$ gcc file4.o libfoo.a -o myprogram
Or linking with it as a library with the -l
flag
$ gcc file4.o -L. -lfoo -o myprogram
gcc compile multiple files
IMHO, if you rewrite your compilation statement like
gcc -I./ -o test main.c src_print1.c src_print2.c
You'll be good to go. There is no need for -c
flag [NOTE] when you're specifying the output binary using -o
.
Also, as mentioned here, all the files are in same directory, you can even shorten the statement as
gcc -o test main.c src_print1.c src_print2.c
Suggestion: While the above change(s) will do the job, this is not considered an elegant way of doing so. Please consider creating a makefile which will make your life easier.
[Note]:
Regarding the -c
option, as per the online gcc manual, (emphasis mine)
-c
Compile or assemble the source files, but do not link. The linking stage simply is not done. The ultimate output is in the form of an object file for each source file.
So, it should be clear by now, why you got the error.
How can gcc compile multiple source files for use in GDB?
The proper command is
gdb ProgramName
followed by
run
in the prompt.
Automatically including multiple source files with -gcc?
Create a library. If you are on Linux, you have to choose if you want a static library or a shared library.
Static libraries are created using archiver command on linux. Google for "ar".
ar cr libtest.a test1.o test2.o
Now you can link with this archive using the -ltest option (ltest is shorthand for libtest you created) with gcc or g++. If your code just has C code then use gcc. If it has both C and C++ then use g++.
As with header files, the linker looks for libraries in some standard places, including
the /lib and /usr/lib directories that contain the standard system libraries. If you
want the linker to search other directories as well, you should use the -L option,
which is the parallel of the -I option for header files.You can use this line to instruct
the linker to look for libraries in the /usr/local/lib/MyTest directory before looking in
the usual places:
g++ -o reciprocal main.o reciprocal.o -L/usr/local/lib/MyTest -ltest
Although you don’t have to use the -I option to get the preprocessor to search the
current directory (for finding you Header file), you do have to use the -L option to get the linker to search the
current directory. In particular, you could use the following to instruct the linker to
find the test library in the current directory:
gcc -o app app.o -L. -ltest
The shared library creation process is also similar. Once you get the hang of it then you can take care of compilation and linking via a makefile.
(Some part of this post was taken from: Advanced Linux Programming , link: http://www.cse.hcmut.edu.vn/~hungnq/courses/nap/alp.pdf)
makefile compile multiple source files in one call
This is a trick, but it might work:
all: ab.timestamp
SOURCES = a.c b.c ab.h
a.o: a.c ab.h
b.o: b.c ab.h
CHANGED :=
%.o: %.c ; $(eval CHANGED += $<)
ab.timestamp: $(SOURCES) $(patsubst %.c,%.o,$(filter %.c,$(SOURCES)))
gcc -c $(CHANGED)
touch $@
Multiple source files in C- How exactly do makefiles work?
Generally what will happen is you will define your functions for the other files in a header file, which can then be included in main.c. For example, consider these snippets:
main.c:
#include "foo.h"
int main(int argc, char *argv[]) {
do_foo();
return 0;
}
foo.h:
void do_foo();
foo.c:
#include <stdio.h>
#include "foo.h"
void do_foo() {
printf("foo was done\n");
}
What will happen is that main.c will be turned into an object file (main.o), and foo.c will be turned into an object file (foo.o). Then the linker will link these two files together and that is where the do_foo()
function in main.c is 'associated' with the function in foo.o.
Example GCC command:
gcc -o myprogram main.c foo.c
Example makefile
myprogam: main.o foo.o
gcc -o myprogram main.o foo.o
main.o: main.c foo.h
gcc -c main.c
foo.o: foo.c foo.h
gcc -c foo.c
How to use gcc to compile multiple different source .c files into differently named executables
Here's how I would do it with GNU make:
all: $(patsubst %.c,%,$(wildcard *.c))
%: %.o
$(CC) -o $@ $<
Or if you have other .c
files too and just want the 6 you listed to be compiled as programs, make the first line:
all: AA BB CC DD EE FF
Related Topics
How to Get the Path of a Windows "Special Folder" for a Specific User
How to Enable Core Dump in My Linux C++ Program
Receiving Rtsp Stream Using Ffmpeg Library
C++11 Way to Index Tuple at Runtime Without Using Switch
Difference Between Creating Object with () or Without
Const and Non-Const Operator Overloading
Best Way for Interprocess Communication in C++
C++: How to Require That One Template Type Is Derived from the Other
Is There Any Difference Between "T" and "Const T" in Template Parameter
How to Check That an Element Is in a Std::Set
Multithreaded Memory Allocators for C/C++