Linker errors-Undefined Reference to

Linda Harrison picture Linda Harrison · Aug 17, 2012 · Viewed 26.2k times · Source

I have project which has a dependency of a logging project when i build this other project i get the following linker errors:

The logging project build well however when i use the logging project inside this project as a library in linker options it generates the following errors , in my logging project i do have a logger.cpp in which wxRegKey is defined.

../logging/dist/Debug/MinGW_1-Windows/liblogging.a(Logger.o): In function `ZN7Logging6Logger17CreateRegistryKeyEv':
c:\logging/impl/Logger.cpp:125: undefined reference to `_imp___ZN8wxRegKeyC1ENS_6StdKeyERK8wxString'
c:\logging/impl/Logger.cpp:127: undefined reference to `_imp___ZN8wxRegKeyC1ENS_6StdKeyERK8wxString'
c:\logging/impl/Logger.cpp:129: undefined reference to `_imp___ZNK8wxRegKey6ExistsEv'
c:\logging/impl/Logger.cpp:135: undefined reference to `_imp___ZN8wxRegKeyD1Ev'
c:\logging/impl/Logger.cpp:140: undefined reference to `_imp___ZN8wxRegKeyC1ENS_6StdKeyERK8wxString'
c:\logging/impl/Logger.cpp:142: undefined reference to `_imp___ZNK8wxRegKey6ExistsEv'
c:\logging/impl/Logger.cpp:143: undefined reference to `_imp___ZN8wxRegKey6CreateEb'
c:\logging/impl/Logger.cpp:149: undefined reference to `_imp___ZNK8wxRegKey6ExistsEv'
c:\logging/impl/Logger.cpp:152: undefined reference to `_imp___ZN8wxRegKeyD1Ev'
c:\logging/impl/Logger.cpp:155: undefined reference to `_imp___ZN8wxRegKeyC1ENS_6StdKeyERK8wxString'
c:\logging/impl/Logger.cpp:156: undefined reference to `_imp___ZNK8wxRegKey6ExistsEv'
c:\logging/impl/Logger.cpp:157: undefined reference to `_imp___ZN8wxRegKey6CreateEb'
../logging/dist/Debug/MinGW_1-Windows/liblogging.a(Logger.o): In function 
`ZN7Logging6Logger13GetLoggStatusEv':
c:\logging/impl/Logger.cpp:169: undefined reference to     
`_imp___ZNK8wxRegKey8HasValueEPKc'
c:\logging/impl/Logger.cpp:170: undefined reference to 
`_imp___ZNK8wxRegKey10QueryValueEPKcPl'
c:\logging/impl/Logger.cpp:176: undefined reference to 
`_imp___ZN8wxRegKey8SetValueEPKcl'


../logging/dist/Debug/MinGW_1-Windows/liblogging.a(Logger.o): In function  
`ZN7Logging6Logger12SetLoggLevelEv':
c:\logging/impl/Logger.cpp:186: undefined reference to    
`_imp___ZNK8wxRegKey8HasValueEPKc'
 c:\logging/impl/Logger.cpp:193: undefined reference to 
`_imp___ZN8wxRegKey8SetValueEPKcRK8wxString'
c:\logging/impl/Logger.cpp:200: undefined reference to `_imp__wxConvUTF8'
../logging/dist/Debug/MinGW_1-Windows/liblogging.a(Logger.o): In function 
`ZN12wxStringBase4InitEv':
C:/wxWidgets-2.8.12/include/wx/string.h:270: undefined reference to   
`_imp__wxEmptyString'
../logging/dist/Debug/MinGW_1-Windows/liblogging.a(Logger.o): In function `wxStringBase':
C:/wxWidgets-2.8.12/include/wx/string.h:368: undefined reference to   
`_imp___ZN12wxStringBase4nposE'
C:/wxWidgets-2.8.12/include/wx/string.h:368: undefined reference to 
`_imp___ZN12wxStringBase8InitWithEPKcjj'
../logging/dist/Debug/MinGW_1-Windows/liblogging.a(Logger.o): In function 
 `ZNK8wxRegKey10QueryValueEPKcR8wxString':
C:/wxWidgets-2.8.12/include/wx/msw/registry.h:167: undefined reference to    
`_imp___ZNK8wxRegKey10QueryValueEPKcR8wxStringb'
collect2: ld returned 1 exit status
make[2]: *** [dist/Debug/MinGW_1-Windows/abcproject.dll] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
make[2]: Leaving directory `/c/abcproject'
make[1]: Leaving directory `/c/abcproject'


 BUILD FAILED (exit value 2, total time: 34s)

order of include file in logger.h

 #include <vector>
 #include <sstream>
 #include <string>
 #include <ctime>
 #include <windows.h>
 #include <winbase.h>
 #include <wx/wx.h>
 #include <wx/thread.h>
 #include <wx/log.h>
 #include <wx/app.h>
 #include <wx/msw/registry.h>
 #include <wx/utils.h>
 #include <map>

Please help

Answer

Rob Kennedy picture Rob Kennedy · Aug 17, 2012

You have evidently neglected to include the library that defines several functions of the wxRegKey class. That's what the linker says it can't find. Are you sure logger.cpp defines wxRegKey and all of its methods?

I suspect your source code doesn't actually define anything related to wxRegKey since that's actually a class from an external library. Rather than defining that class, you've included a header file where that class is declared. The definition resides in an external file, and you need to tell your build system which file that is.

You have a linking error, not a compiling error, so the order of the #include statements is irrelevant. The compiler has already finished compiling everything by the time you see the errors you're reporting here. The compiler has converted your text source code into binary object code, and now the linker is trying to gather all the binary files together to form the final executable program. The binary files refer to some functions that the compiler was told would be defined somewhere else, and now the linker is trying to find those definitions, but it cannot.

If you don't know where the functions are defined, or how to tell your build system where to find them, then you might have to post another question: What do I need to link with to use wxRegKey? How do I link with wxWidgets in a NetBeans project? I don't know the answers to those questions because I've never used those tools.