Consider the following example
g++ a.o b.o c.o -o prog
If it is the case that c.o does not contribute any executable code to prog, nor are there any dependencies on c.o in any of the other files, will GCC yet include the contents of c.o in prog?
Said another way, aside from compilation time, what (if any) negative consequences can there be of compiling unnecessary files into an executable?
Thanks in advance; Cheers!
There aren’t any negative consequences except that your executable might be unnecessarily large. The linker can probably dead strip unused code for you, and that will shrink things back down. You can use some kind of object viewing tool (
otool,objdump,nm, etc.) on the output executable to see if your program has extra symbols in it.I’m using a Mac, so there will be some differences if you’re using the standard set of gcc tools, but here’s an example:
I think in the non-Apple gcc world, you would pass
-Wl,--gc-sectionsinstead of the-Wl,-dead_stripin my example. The size difference in the two executables you can see is due to the extra function being stripped: