链接器错误 - 未定义参考

时间:2012-08-17 12:57:15

标签: c++ linker linker-errors netbeans-7.1

我有一个项目,当我构建这个其他项目时,它具有日志项目的依赖项,我得到以下链接器错误:

日志记录项目很好地构建,但是当我将此项目中的日志记录项目用作链接器选项中的库时,它会生成以下错误,在我的日志记录项目中,我确实有一个logger.cpp,其中定义了wxRegKey。

../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)

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>

请帮忙

3 个答案:

答案 0 :(得分:9)

你显然忽略了包含定义wxRegKey类的几个函数的库。这就是链接器无法找到的内容。您确定 logger.cpp 定义了wxRegKey 所有方法吗?

我怀疑您的源代码实际上并未定义wxRegKey相关的任何内容,因为它实际上是来自外部库的类。您不是定义该类,而是包含一个声明该类的头文件。 定义驻留在外部文件中,您需要告诉构建系统哪个文件。

您有链接错误,而不是编译错误,因此#include语句的顺序无关紧要。当您看到此处报告的错误时,编译器已完成编译。编译器已将您的文本源代码转换为二进制目标代码,现在链接器正在尝试将所有二进制文件收集在一起以形成最终的可执行程序。二进制文件指的是编译器被告知将在其他地方定义的一些函数,现在链接器正在尝试查找这些定义,但它不能。

如果您不知道函数的定义位置,或者如何告诉构建系统在哪里找到它们,那么您可能需要发布另一个问题:我需要链接什么才能使用wxRegKey?如何在NetBeans项目中链接wxWidgets?我不知道这些问题的答案,因为我从未使用过这些工具。

答案 1 :(得分:0)

您似乎使用wxwidgets。您确定在链接最终可执行文件时链接正确的库吗?请查看传递给链接器的命令。

答案 2 :(得分:0)

您似乎忘了将库wxBase添加到链接器命令行,因为偶数wxString - 符号未解析。