0

I have this code

#include <stdio.h>

void test2()
{
    printf("start test2\n");

    printf("end test2\n");
}

void main ()
{

    printf("abc\n");
    #ifdef A
        test2();
    #endif

}

compile it with gcc test.c -o test -static -D B

when I run the program I see that test2 didn't run (that good)

But when I run strings I can see that end test2 in binary file. why? gcc didn't need to compile it !

When I compile this code

#include <stdio.h>
void test1();
void test2()
{
    printf("start test2\n");
    test1();
    printf("end test2\n");
}

void main ()
{

    printf("abc\n");
    #ifdef A
        test2();
    #endif

}

with gcc test.c -o test -static -D B

Gcc tell me that undefined reference to 'test1' why? I didn't want that gcc even compile function test2 so gcc didn't need to know that I used that undefined function.

What can I do that gcc will not look on test2 when I pass -D that not equal to A ?

  • 2
    Wrap `test2` with `#ifdef A` also. Also, maybe turn up optimization. – Fiddling Bits Jan 09 '20 at 16:28
  • 2
    duplicate of [How to remove unused C/C++ symbols with GCC and ld?](https://stackoverflow.com/questions/6687630/how-to-remove-unused-c-c-symbols-with-gcc-and-ld) and [discard unused functions in GCC](https://stackoverflow.com/questions/36033595/discard-unused-functions-in-gcc) and other things that can be found by searching – underscore_d Jan 09 '20 at 16:28
  • 4
    It was only the call that was compiled conditionally, not the function itself (although it could be optimised out). – Weather Vane Jan 09 '20 at 16:35

3 Answers3

3

The function test2 can still be called from a function in an external module, even if you don't call it here, so the definition of the function must exist.

If you change the function to static so that it can only be referenced from the current file and increase the optimization to -O1 or higher, the entire function is optimized out.

dbush
  • 162,826
  • 18
  • 167
  • 209
1

By default linker does not remove the dead code.

use : -fdata-sections -ffunction-sections -fdce- -Wl,--gc-sections -static command line option

0___________
  • 34,740
  • 4
  • 19
  • 48
  • `-Wl` ____________________ – 0___________ Jan 09 '20 at 17:55
  • thank you ! that work ~ can please explain more about this solution ? `fdata-sections` and `ffuntion-sections` separate each function to another sections , and `-Wl,--gc-sections` just remove the unused sections? why now the linker didn't search about `test1` ? , by the way the string `end test2` still contain in final binary – ndy76729.zzrgg.com Jan 09 '20 at 18:01
  • It removes the "dead code" and data from the output file.\ – 0___________ Jan 09 '20 at 18:31
  • The only problem is that in GMU site I see `When you specify these options, the assembler and linker create larger object and executable files and are also slower`. Is there any solution that not slow down the code and makes the solution file bigger?? – ndy76729.zzrgg.com Jan 09 '20 at 18:42
  • It may increase the size of the static libraries, but not executables. At least on the platforms I use. I have no clue why they wtite something about execution time as it wqill be exactly the same. – 0___________ Jan 09 '20 at 18:46
1

Your approach is a bit like saying "If I don't look at the tree, the tree is not there anymore." which is of course not true.

If you don't want to include a function in your program, remove it. (And all references to it)

#include <stdio.h>
void test1();

#ifdef A
void test2()
{
    printf("start test2\n");
    test1();
    printf("end test2\n");
}
#endif

int main (void)
{
    printf("abc\n");
    #ifdef A
        test2();
    #endif
}
Gerhardh
  • 7,440
  • 2
  • 11
  • 29