David Tong
David Tong

Reputation: 101

extern class: undefined reference when porting from gcc 3.x to 4.x

I'm porting some hideous legacy C++ code from gcc 3.x to 4.x There's a construct that look like this in a header file:

extern class ErrorLog
{
   . . .
} error_log, debug_log;

Under 3.x it compiles and works fine, but under 4.x I get lots of errors of the form

undefined reference to `error_log'

undefined reference to `ErrorLog::log(ErrorLog::LogAttr const&, char const*, ...)'

Upvotes: 1

Views: 525

Answers (3)

David Tong
David Tong

Reputation: 101

The solution turned out to be a simple linker ordering problem. The error_log definition was in a library listed early in the command line, but wasn't used until later on. The 4.x linker must not keep track of defined symbols the way that the 3.x linker does. Adding -Xlinker --whole-archive at the start resolved the problem, though naturally it bloated the object.

Changing the ordering would work too, but the set of archives is automatically generated, so that's more difficult without radically rewriting the Makefile.

Upvotes: 0

TonyK
TonyK

Reputation: 17124

I think this is related to a bug report I submited to GCC a while back. The question is, does this define a type class ErrorLog or not? I suggest you separate the definition and the declarations, like this:

class ErrorLog{...};
extern ErrorLog error_log, debug_log;

Presumably class ErrorLog is defined somewhere else too -- ideally you should change this so that it's only defined once.

Upvotes: 2

dappawit
dappawit

Reputation: 12590

Extern is a declaration, not a definition. Therefore, you are only declaring the name error_log and debug_log, but not allocating a memory location for them. You need to have the definition of those variables somewhere else (probably in a .cpp file).

It may be enough to do this in a source file that includes the header file you mention: ErrorLog error_log, debug_log;

Having the entire class definition in the extern declaration is odd though.

Upvotes: 0

Related Questions