gcc does not include extern variables in symbol table when optimisation is used

前端 未结 2 1343
长发绾君心
长发绾君心 2021-01-27 18:30

My program contains many externally defined variables. When I compile it with -O0 flag, I see them in the symbol table, but not when I use -O1 or -O2. How can I force the compil

2条回答
  •  忘了有多久
    2021-01-27 19:04

    If your foo.c only (essentially) has

    extern const int my_symbol;
    

    then compile it with -O1 or -O2, that symbol will be optimized out. However, if you use that symbol in foo.c, for example

    extern const int my_symbol;
    extern int my_flag;
    
    void foo(void)
    {
        if (my_symbol)
            my_flag = 1;
    }
    

    All of those symbols will exist in foo.o even if you compile it with -O1 or -O2.

提交回复
热议问题