queen3
queen3

Reputation: 15501

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.

Upvotes: 2

Views: 1587

Answers (3)

Mike Seymour
Mike Seymour

Reputation: 254431

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.

Upvotes: 2

Steve Jessop
Steve Jessop

Reputation: 279205

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.

Upvotes: 6

filmor
filmor

Reputation: 32182

You can force gcc to ignore inline using the flag -fno-inline.

Upvotes: 0

Related Questions