C++ 链接器错误 - 未定义的引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12006291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:45:28  来源:igfitidea点击:

Linker errors-Undefined Reference to

c++linkerlinker-errorsnetbeans-7.1

提问by Linda Harrison

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.

日志项目构建得很好,但是当我使用该项目中的日志项目作为链接器选项中的库时,它会生成以下错误,在我的日志项目中,我确实有一个 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)

order of include file in logger.h

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

请帮忙

回答by Rob Kennedy

You have evidently neglected to include the library that defines several functions of the wxRegKeyclass. That's what the linker says it can't find. Are you sure logger.cppdefines wxRegKeyandall of its methods?

您显然忽略了包含定义wxRegKey类的多个函数的库。这就是链接器所说的找不到的内容。你肯定logger.cpp定义wxRegKey其所有的方法呢?

I suspect your source code doesn't actually defineanything related to wxRegKeysince that's actually a class from an external library. Rather than definingthat class, you've includeda header file where that class is declared. The definitionresides in an external file, and you need to tell your build system which file that is.

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

You have a linkingerror, not a compilingerror, so the order of the #includestatements 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.

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

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.

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

回答by user1202136

You seem to use wxwidgets. Are you sure you are linking the correctly library while linking the final executable? Please review the command passed to the linker.

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

回答by vladimir_ki

It seems that you forget add library wxBaseto linker command line, besause of even wxString-symbols aren`t resolved.

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