C++ 目标文件中未解析的外部符号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9928238/
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 13:24:38  来源:igfitidea点击:

Unresolved external symbol in object files

c++visual-studiovisual-c++unresolved-external

提问by Novellizator

During coding in Visual Studio I got an unresolved external symbol error and I've got no idea what to do. I don't know what's wrong. Could you please decipher me? Where should I be looking for what kind of errors?

在 Visual Studio 中编码期间,我遇到了一个未解决的外部符号错误,我不知道该怎么做。我不知道怎么了。你能解密我吗?我应该在哪里寻找什么样的错误?

1>Form.obj : error LNK2019: unresolved external symbol "public: class Field * __thiscall Field::addField(class Field *)" (?addField@Field@@QAEPAV1@PAV1@@Z) referenced in function "public: void __thiscall Form::parse(class std::basic_stringstream<char,struct std::char_traits<char>,class std::allocator<char> > &)" (?parse@Form@@QAEXAAV?$basic_stringstream@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)
1>Form.obj : error LNK2019: unresolved external symbol "public: virtual void __thiscall Field::parse(class std::basic_stringstream<char,struct std::char_traits<char>,class std::allocator<char> > &)" (?parse@Field@@UAEXAAV?$basic_stringstream@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function "public: __thiscall InputField::InputField(class std::basic_stringstream<char,struct std::char_traits<char>,class std::allocator<char> > &)" (??0InputField@@QAE@AAV?$basic_stringstream@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)
1>Form.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall Field::prompt(void)" (?prompt@Field@@UAEXXZ)
1>Form.obj : error LNK2001: unresolved external symbol "public: virtual class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall Field::getName(void)" (?getName@Field@@UAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ)
1>Form.obj : error LNK2001: unresolved external symbol "public: virtual class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall Field::getType(void)" (?getType@Field@@UAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ)
1>Form.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall Field::describe(void)" (?describe@Field@@UAEXXZ)
1>C:\Users\tomy\Documents\Visual Studio 2010\Projects\zapoctovkac++\Debug\zapoctovkac++.exe : fatal error LNK1120: 6 unresolved externals

回答by Chris Morris

This error often means that some function has a declaration, but not a definition.

此错误通常意味着某些函数有声明,但没有定义。

Example:

例子:

// A.hpp
class A
{
public:
  void myFunc(); // Function declaration
};

// A.cpp

// Function definition
void A::myFunc()
{
  // do stuff
}

In your case, the definition cannot be found.The issue could be that you are including a header file, which brings in some function declarations, but you either:

在您的情况下,找不到定义。问题可能是你包含了一个头文件,它引入了一些函数声明,但你要么:

  1. do not define the functions in your cpp file (if you wrote this code yourself)
  2. do not include the lib/dll file that contains the definitions
  1. 不要在你的 cpp 文件中定义函数(如果你自己写了这段代码)
  2. 不包括包含定义的 lib/dll 文件

A common mistake is that you define a function as a standalone and forget the class selector, e.g. A::, in your .cppfile:

一个常见的错误是您将函数定义为独立函数而忘记了类选择器,例如A::,在.cpp文件中:

Wrong:void myFunc() { /* do stuff */ }
Right:void A::myFunc() { /* do stuff */ }

错:void myFunc() { /* do stuff */ }
对:void A::myFunc() { /* do stuff */ }

回答by Konrad

Check you are including all the source files within your solution that you are referencing.

检查您是否在解决方案中包含了您正在引用的所有源文件。

If you are not including the source file (and thus the implementation) for the class Fieldin your project it won't be built and you will be unable to link during compilation.

如果您没有Field在项目中包含该类的源文件(以及实现),则不会构建该类,并且您将无法在编译期间进行链接。

Alternatively, perhaps you are using a static or dynamic library and have forgotten to tell the linker about the .libs?

或者,也许您正在使用静态或动态库并且忘记告诉链接器有关.libs 的信息?

回答by Fylix

It looks to be missing a library or include, you can try to figure out what class of your library that have getName, getType etc ... and put that in the header file or using #include.

它看起来缺少一个库或包含,您可以尝试找出具有 getName、getType 等的库的哪个类......并将其放入头文件或使用#include.

Also if these happen to be from an external library, make sure you reference to them on your project file. For example, if this class belongs to an abc.lib then in your Visual Studio

此外,如果这些恰好来自外部库,请确保在项目文件中引用它们。例如,如果此类属于 abc.lib,则在您的 Visual Studio 中

  1. Click on Project Properties.
  2. Go to Configuration Properties, C/C++, Generate, verify you point to the abc.lib location under Additional Include Directories. Under Linker, Input, make sure you have the abc.lib under Additional Dependencies.
  1. 单击项目属性。
  2. 转到配置属性、C/C++、生成,确认您指向附加包含目录下的 abc.lib 位置。在链接器、输入下,确保您在附加依赖项下有 abc.lib。

回答by Alexey257

I've just seen the problem I can't call a function from main in .cpp file, correctly declared in .h file and defined in .c file. Encountered a linker error. Meanwhile I can call function from usual .c file. Possibly it depends on call convention. Solution was to add following preproc lines in every .h file:

我刚刚看到了我无法从 .cpp 文件中的 main 调用函数、在 .h 文件中正确声明并在 .c 文件中定义的问题。遇到链接器错误。同时我可以从通常的 .c 文件中调用函数。可能这取决于调用约定。解决方案是在每个 .h 文件中添加以下 preproc 行:

#ifdef __cplusplus
extern "C"
{
#endif

and these in the end

而这些最终

#ifdef __cplusplus
}
#endif

回答by Gal Bracha

I had an error where my project was compiled as x64project. and I've used a Librarythat was compiled as x86.

我的项目被编译为x64项目时出错。我使用了一个编译为x86

I've recompiled the library as x64 and it solved it.

我已经将库重新编译为 x64 并解决了它。

回答by Shashank

sometimes if a new header file is added, and this error starts coming due to that, you need to add library as well to get rid of unresolved external symbol.

有时,如果添加了新的头文件,并且因此开始出现此错误,您还需要添加库以摆脱unresolved external symbol.

for example:

例如:

#include WtsApi32.h

will need:

将需要:

#pragma comment(lib, "Wtsapi32.lib") 

回答by meJustAndrew

I had the same link errors, but from a test project which was referencing another dll. Found out that after adding _declspec(dllexport)in front of each function which was specified in the error message, the link was working well.

我有相同的链接错误,但是来自一个引用另一个 dll 的测试项目。发现_declspec(dllexport)在错误信息中指定的每个函数前面添加后 ,链接运行良好。

回答by Patrick Nguyen

I believe most of the points regarding the causes and remedies have been covered by all contributors in this thread. I just want to point out for my 'unresolved external' problem, it was caused by a datatype defined as macro that gets substituted differently than expected, which results in that incorrect type being supplied to the function in question, and since the function with type is never defined, it couldn't have been resolved. In particular, under C/C++ -> Language, there is an attribute called 'Treat WChar_t As Built in Type, which should have been defined as 'No (/Zc:wchar_t-)' but did not in my case.

我相信该线程中的所有贡献者都涵盖了有关原因和补救措施的大部分要点。我只想指出我的“未解决的外部”问题,它是由定义为宏的数据类型引起的,该数据类型的替换与预期不同,这导致向相关函数提供了错误的类型,并且由于具有类型的函数从未定义,它无法解决。特别是,在 C/C++ -> Language 下,有一个名为“Treat WChar_t As Built in Type”的属性,它应该被定义为“No (/Zc:wchar_t-)”,但在我的情况下没有。

回答by GMLewisII

In addition to the excellent answer by Chris Morris above, I found a very interesting way you can receive this same fault if you are calling to a virtual method that hasn't been set to pure but doesn't its own implementation. It is the exact same reason (the compiler can't find an implementation of the method and therefore crooks), but my IDE did not catch this fault in the least bit.

除了上面 Chris Morris 的出色回答之外,我还发现了一种非常有趣的方式,如果您正在调用一个尚未设置为纯但没有自己实现的虚拟方法,您可能会收到同样的错误。这是完全相同的原因(编译器找不到该方法的实现,因此是骗子),但是我的 IDE 丝毫没有发现这个错误。

for example, the following code would get a compilation error with the same error message:

例如,下面的代码会得到一个带有相同错误信息的编译错误:

//code testing an interface
class test
{
   void myFunc(); 
}

//define an interface
class IamInterface
{
    virtual void myFunc();
}

//implementation of the interface
class IamConcreteImpl
{
    void myFunc()
    {
       1+1=2;
    }
}

However, changing IamInterface myFunc() to be a pure virtual method (a method that "must" be implemented, that than a virtual method which is a method the "can" be overridden) will eliminate the compilation error.

但是,将 IamInterface myFunc() 更改为纯虚方法(“必须”实现的方法,而不是“可以”覆盖的方法的虚方法)将消除编译错误。

//define an interface
class IamInterface
{
    virtual void myFunc() = 0;
}

Hopes this helps the next StackOverFlow person stepping through code!

希望这有助于下一个 StackOverFlow 人员逐步完成代码!

回答by Alessandro Jacopson

See Linker Tools Error LNK2019at MSDN, it has a detailed list of common problems that cause LNK2019.

请参阅MSDN 上的链接器工具错误 LNK2019,其中包含导致 LNK2019 的常见问题的详细列表。