Compiling non-essential object files using GCC

Consider the following example.

g++ ao bo co -o prog 

If it is so that co does not inject any executable code into prog and there are no dependencies on co in any of the other files, will GCC include the contents of co in prog ?

On the other hand, in addition to compilation time, what (if any) negative consequences can consist in compiling unnecessary files into an executable file?

Thanks in advance; Hooray!

+6
c ++ gcc compilation linker
source share
4 answers

There are no negative consequences, except that your executable file may be unnecessarily large. The linker may be perhaps a dead band of unused code for you, and that will cut it all. You can use some tool to view objects ( otool , objdump , nm , etc.) in the executable file to find out if there are any additional characters in it.

I use a Mac, so there will be some differences if you use the standard set of gcc tools, but here is an example:

 $ make gcc -o app main.c file2.c gcc -Wl,-dead_strip -o app_strip main.c file2.c $ ls -l app* -rwxr-xr-x 1 carl staff 8744 Feb 6 20:05 app -rwxr-xr-x 1 carl staff 8704 Feb 6 20:05 app_strip 

I think that in the non-Apple gcc world, you should pass -Wl,--gc-sections instead of -Wl,-dead_strip in my example. The difference in size in the two executable files that you see is due to the fact that the extra function is separated:

 $ nm app | cut -c 20- > app.txt $ nm app_strip | cut -c 20- > app_strip.txt $ diff app.txt app_strip.txt 8d7 < _function2 
+5
source share

llvm can eliminate dead code at the connection stage. It uses the special llvm-ld linker.

Also, using -fwhole or -ipo (intel) will help remove dead characters.

+3
source share

I just tried it with some C code that I am currently using - I contacted an object containing a method that is not yet used elsewhere in the program. The code was included in the resulting executable file, verified by running nm on the elf file and noting that the T method does exist, even using -O2 and -O3.

+2
source share

Yes, GCC will include all object files. With a very recent compiler (development version 4.5.0), you can use -flto (optimize connection time) to accomplish this.

+1
source share

All Articles