How do you compile multiple files into one project under gcc?
How do you compile multiple files into one project under gcc?
Current Setup: Win 10 with Code::Blocks 17.12 (GNU 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.
If any part of my post is incorrect, please correct me.
This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.
thats another option, and will still tell you any error information, but for larger projects, the method I mentioned above is much more common.
If any part of my post is incorrect, please correct me.
This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.
Actually for larger projects a make file is more common.
Quzah.
Hope is the first step on the road to disappointment.
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
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.