Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what happens to a static c function that is not called

if you have something like this:

int _tmain(int argc, _TCHAR* argv[]) {
    int i;
#if (num>99)
    i = func();
#else
    i= func2();
#endif
    return 0;
}

static int func()
{
    return 1;
}
static int func2()
{
    return 2;
}

Is it reasonable to expect that depending on if num is bigger or smaller then 99 ether func or func2 will be removed from the runtime code?

Or would I rather need to also embed the functions in an #if to achieve this goal?

like image 899
lsteinme Avatar asked Sep 29 '22 19:09

lsteinme


2 Answers

This depends on linker,what does it with dead code is linker specific. You should also include function definition under #if to make sure that it wont results into machine code.

like image 156
Vagish Avatar answered Nov 15 '22 07:11

Vagish


It depends on optimization level. On linux you can check it youself using readelf -s ./a.out | grep func2

But I think you use windows, so you need some similar tool http://www.pe-explorer.com/ for example.

Here is list of tools: Any tool/software in windows for viewing ELF file format?

like image 27
Konstantin.Krivyakin Avatar answered Nov 15 '22 07:11

Konstantin.Krivyakin