简体   繁体   中英

gcc optimization flags in O2 that causes undefined symbol

I have a problem with -O2 in gcc 4.5.2. Say I have this code:

//file.cpp
void test::f() {}
//file.h
struct test
{
    inline void f();
};

This code is in the shared library. Now, when I compile without -O2, it works fine. With -O2 it says that test::f() is undefined symbol. Obviously gcc just throws it away because it's "inline" (though it is really not).

My question is what specific optimization flag causes this? The idea is that I want to enable -O2 but disable that exact flag so that I can keep inlines untouched (that's not my code).

I can probably just iterate all of them but, this can also be linker flag, right? This is too much work, I just hope someone will have a clue.

The standard requires that the definition of an inline function is present in every TU that uses it.

Either remove inline or else move the definition of the function to the header file. Even if what you want to do were allowed, there would be no benefit in marking the function inline .

It just so happens that on your implementation you have a problem with -O2 but apparently no problem without it.

The best solution is to fix or reject the broken code. Inline functions must be defined in any translation unit that uses them, and this code breaks that rule.

If that isn't an option, then -fkeep-inline-functions might patch over the problem well enough to allow the code to compile and link.

您可以使用-fno-inline标志强制gcc忽略inline -fno-inline

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM