Is there a way to remove all functions from an object file that I am not using?

I am trying to save space in my executable file, and I noticed that several functions are added to my object files, although I never call them (code from the library).

Is there a way to tell gcc to remove these features automatically, or do I need to remove them manually?

+4
source share
5 answers

Since I asked this question, GCC 4.5 was released, which includes the ability to merge all files, so it looks like it's just 1 gigantic source file. Using this option, you can easily remove unused functions.

More here

+1
source

If you compile into object files (rather than executable files), then the compiler will never delete any non- static functions, since you can always link an object file to another object file that will call this function. So, your first step is to declare as many static functions as possible.

Secondly, the only way for the compiler to remove any unused functions is to statically link your executable. In this case, there is at least the likelihood that the program can come and find out which functions are used and which of them are not used.

Trap, I don't believe gcc really does this type of cross-modular optimization. It’s best to use the -Os flag to optimize code size, but even if you have an abc.o object file that has some unused non-static functions and you put a static link to some def.exe executable, t believe gcc will go and cross out the code for unused functions.

If you really need this to be done, I think that you probably should actually #include merge the files, so that after going through the preprocessor this will lead to the compilation of one .c file. With gcc compiling one monstrous jumbo source file, you have a chance to avoid unused functions.

+2
source

You looked at calling gcc with -Os (optimize the size.) I'm not sure if it removes unencrypted code, but that would be easy enough to test. You can also, after you have executed your executable file, "split" it. I'm sure the arg gcc command does the same - is it --dead_strip?

+1
source

In addition to -Os for size optimization, this link may be useful.

+1
source

The IIRC default linker does what you want in some specific cases. In short, library files contain a bunch of object files and only file links are linked. If you can understand how to get GCC to emit each function into its own object file, then build it into the library that you should get what you are looking for.

I only know one compiler that can really do this: here (look at the -lib flag)

0
source

Source: https://habr.com/ru/post/1306868/


All Articles