views:

131

answers:

2

I have a workspace built using MS-Visual Studio 2005 with all C code.In that i see many functions which are not called but they are still compiled(they are not under any compile time macro to disable them from compiling).

I set following optimization settings for the MS-VS2005 project to remove that unused code:-

Optimization level - /Ox

Enable whole program optimization - /GL

I tried both Favor speed /Ot and Favor Size /Os

Inspite of all these options, when i see the linker generated map file, I see the symbols(unsed functions) names present in the map file.

Am I missing something? I want to completely remove the unused code.

How do I do this?

+1  A: 

The compiler compiles C files one-at-a-time. Therefore, while compiling a C-file that does contains an unused function, the compiler cannot be sure that it will not be called from another file and hence it will compile that function too. However, if that function were declared as static (file-scope), then the compiler would know it is not used and hence remove it.

Even with whole program optimization, I think it would still not be done since the compilation could be for a library.

Linkers do something similar to what you are looking for. If your code links against a library containing multiple objects, then any objects that do not contain functions used by your code (directly or indirectly) would not be included in the final executable.

One option would be to separate your code into individual libraries and object files.

PS - This is just my guess. The behavior of the compiler (with whole program optimization) or linker essentially depends on the design choices of that particular compiler or linker

Gautham Ganapathy
A: 

On our projects we have a flag set under the project properties\Linker\Refrences. We set it to Eliminate Unreferenced Data (/OPT:REF), according to the description this is supposed to remove function calls or data that are never used. I am just going by the description, I have never tested this or worked with it. But I just happened to see it within the last hour and figured it might be something you could try.

Alex