How do you compile multiple files into one project under gcc?
Printable View
How do you compile multiple files into one project under gcc?
There are actually a couple ways to do it, the one I am most familiar with is compiling each source file by separately, then linking them all (and any libraries) together at the end, such as
gcc -c file1.c
gcc -c file2.c
gcc -c file3.c
gcc -o program file1.o file2.o file3.o
The -c option tells the compiler not to do the link phase, simply produce the object code. you can then pass the object code files to gcc, and tell it where the resulting binary should be named with -o
I believe the common reason to do this, is one make files can expand a list of files into multiple lines consisting of that each of the files in the place, and order needed. as well as when doing long builds, if the build fails, you have all the object code from the good files, while you can view the error, fix it, and continue the build without having to completely start from scratch compiling.
?Code:gcc -o app.exe file1.c file2.c file3.c
thats another option, and will still tell you any error information, but for larger projects, the method I mentioned above is much more common.
Actually for larger projects a make file is more common. ;)
Quzah.
Yes, you could use a Makefile like this one:
The <TAB> means a tab: I didn't actually insert a tab because a tab looked too much like eight spaces. Spaces don't work.Code:program.exe: source1.o source2.o sourceN.o
<TAB>gcc -o program.exe source1.o source2.o sourceN.o
Then just use the following command
and make will compile only the files that need compiling.Code:C:\>make