0

When I build a simple C++ program with glog library (Google's logging library which is not used in the code), I get "undefined reference" errors. When I remove the -lglog from the build command, the link succeeds.

Notice that the library that I added to the link is not used in the code at all and despite that it caused the build to fail. In addition, the glog and log4cpp libraries are supposed to be independent.

Can you explain this unusual behavior?

Environment: Ubuntu 14.04

Code:

//test.cpp

#include "log4cpp/Appender.hh"
#include "log4cpp/FileAppender.hh"

int main() {
     log4cpp::Appender *appender;
     appender = new log4cpp::FileAppender("default", "program.log");
     return 0;
}

Working build command:

$ g++ test.cpp -llog4cpp -lpthread

Failing build command:

$ g++ test.cpp -llog4cpp -lglog -lpthread

//usr/local/lib/liblog4cpp.so: undefined reference to `pthread_key_create'
//usr/local/lib/liblog4cpp.so: undefined reference to `pthread_getspecific'
//usr/local/lib/liblog4cpp.so: undefined reference to `pthread_key_delete'
//usr/local/lib/liblog4cpp.so: undefined reference to `pthread_setspecific'
collect2: error: ld returned 1 exit status

EDIT:

This command also builds successfully:

g++ test.cpp -llog4cpp -lpthread -lglog

This command fails (change the order of libs):

$ g++ test.cpp -llog4cpp -lglog -lpthread
//usr/local/lib/liblog4cpp.so: undefined reference to `pthread_key_create'
//usr/local/lib/liblog4cpp.so: undefined reference to `pthread_getspecific'
//usr/local/lib/liblog4cpp.so: undefined reference to `pthread_key_delete'
//usr/local/lib/liblog4cpp.so: undefined reference to `pthread_setspecific'
collect2: error: ld returned 1 exit status

This succeeds:

$ g++ test.cpp -pthread -llog4cpp

This fails:

$ g++ test.cpp -pthread -llog4cpp -lglog

EDIT 2:

I studied the duplicate suggestions (1) and (2) to find out maybe there's something useful to me there, but it turned out irrelevant because these cases doesn't address the situation where a library that is not used in the code is added to the link and make it fail.

EDIT 3:

The files from my environment (glog libs, log4cpp libs, used log4cpp headers and test.cpp): log_test.zip.

Community
  • 1
  • 1
DavidS
  • 2,160
  • 1
  • 19
  • 22
  • possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – IInspectable Aug 11 '15 at 16:02
  • @IInspectable there are many answers in this post, can you please point me to the one that answers the question? Notice that the library that I added to the link is not used in the code at all and despite that it caused the build to fail. – DavidS Aug 12 '15 at 06:12
  • @SleuthEye, originally I used `-pthread`, now I modified the question to be with `-lpthread`. I verified that it still has the same behavior. – DavidS Aug 12 '15 at 08:15
  • possible duplicate of [undefined reference to \`pthread\_key\_create' (linker error)](http://stackoverflow.com/questions/21116622/undefined-reference-to-pthread-key-create-linker-error) – SleuthEye Aug 12 '15 at 13:12
  • @SleuthEye, from these answers, I can't see why the linker takes objects from `glog` library. The `test.cpp` code doesn't use anything from `glog` so why does the linker tries to insert it to the executable? – DavidS Aug 12 '15 at 13:53
  • I still haven't got an answer to the question... can someone please explain the linker's behavior? – DavidS Aug 13 '15 at 13:43

0 Answers0