Weird undefined symbols of static constants inside a struct/class

前端 未结 6 1332
别跟我提以往
别跟我提以往 2020-12-02 23:15

Either I\'m very tired or something weird is happening that I\'m not aware of, because the code below is resulting in undefined symbols for Foo::A and Foo::B when li

6条回答
  •  囚心锁ツ
    2020-12-02 23:26

    There are good answers here, but one additional thing to note is that the parameters of std::min() are references, which is what requires the addresses of the passed in variables, and since those variables don't make it to the object file for the compilation unit, the linker cannot resolve their addresses.

    You are probably getting this in an non-optimized build, correct?

    I bet that you won't get this with gcc if you enable optimizations. The call to std::min() will get inlined and the references will go away.

    Also, if you were to assign Foo::A and Foo::B to two local variables right before the call to std::min(), this issue would also go away.

    This isn't ideal, but if you don't own the code that defines the variables that are causing you this issue, then this is something you can consider.

提交回复
热议问题