开发者

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 t开发者_运维技巧hat 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*, ...)'


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜