C++ LNK2019错误如何解决

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

How to solve error LNK2019

c++linkerunresolved-externallnk2019

提问by user1

I am sending a simple email in C++. I downloaded a sample C++ program from the below link. http://cboard.cprogramming.com/cplusplus-programming/125655-sending-simple-email-cplusplus.htmlThe sample program seems to hit the following error when it is compiling. Please help me with solution.

我正在用 C++ 发送一封简单的电子邮件。我从下面的链接下载了一个示例 C++ 程序。http://cboard.cprogramming.com/cplusplus-programming/125655-sending-simple-email-cplusplus.html示例程序在编译时好像遇到如下错误。请帮我解决。

Error   8   error LNK2019: unresolved external symbol _send_mail referenced in function _wmain  

Error   9   error LNK2019: unresolved external symbol __imp__recv@16 referenced in function "int __cdecl connect_to_server(char const *)" (?connect_to_server@@YAHPBD@Z)    

Error   10  error LNK2019: unresolved external symbol __imp__connect@12 referenced in function "int __cdecl connect_to_server(char const *)" (?connect_to_server@@YAHPBD@Z) 

Error   11  error LNK2019: unresolved external symbol __imp__htons@4 referenced in function "int __cdecl connect_to_server(char const *)" (?connect_to_server@@YAHPBD@Z)    

Error   12  error LNK2019: unresolved external symbol __imp__socket@12 referenced in function "int __cdecl connect_to_server(char const *)" (?connect_to_server@@YAHPBD@Z)  

Error   13  error LNK2019: unresolved external symbol __imp__getprotobyname@4 referenced in function "int __cdecl connect_to_server(char const *)" (?connect_to_server@@YAHPBD@Z)   

Error   14  error LNK2019: unresolved external symbol __imp__gethostbyname@4 referenced in function "int __cdecl connect_to_server(char const *)" (?connect_to_server@@YAHPBD@Z)    

回答by Rob W

I encountered the same error ("LNK2019: unresolved external symbol ...."). My headers and calls were defined correctly, and it only failed to link in Debug mode (no complaints in Release mode). It turned out that my issue was caused by an incorrect .vcxprojfile.

我遇到了同样的错误(“LNK2019:未解析的外部符号......”)。我的标题和调用被正确定义,它只是在调试模式下无法链接(在发布模式下没有投诉)。原来我的问题是由不正确的.vcxproj文件引起的。

When I added new dependencies to my project by editing the vxcprojfile, I made a mistake: I thought that the two sections were identical except for the file extension, so I copy-pasted two lines from the first <ItemGroup>to the last <ItemGroup>(see below).

当我通过编辑vxcproj文件向我的项目添加新的依赖项时,我犯了一个错误:我认为这两个部分除了文件扩展名之外是相同的,所以我从第一行<ItemGroup>到最后一行复制粘贴了两行<ItemGroup>(见下文)。

It went unnoticed for a while, because I used a batch script to compile the code in Releasemode. When I switched to Debugmode, the project failed at the linking stage. Ultimately, I discovered my error, and resolved the problem with the following patch:

有一段时间没有注意到它,因为我使用批处理脚本在发布模式下编译代码。当我切换到调试模式时,项目在链接阶段失败。最终,我发现了我的错误,并通过以下补丁解决了问题:

-    <ClCompile Include="crypto/crypto.h" />
-    <ClCompile Include="crypto/rsa_public_key.h" />
+    <ClInclude Include="crypto/crypto.h" />
+    <ClInclude Include="crypto/rsa_public_key.h" />

Buggy version of the .vcxprojfile:

.vcxproj文件的错误版本:

  <ItemGroup>
    ...
    <ClCompile Include="main.cpp" />
    <ClCompile Include="crypto/crypto.cpp" />
    <ClCompile Include="crypto/rsa_public_key.cpp" />
  </ItemGroup>
  <ItemGroup>
    <None Include="main.def" />
  </ItemGroup>
  <ItemGroup>
    ...
    <ClInclude Include="main.h" />
    <ClCompile Include="crypto/crypto.h" />
    <ClCompile Include="crypto/rsa_public_key.h" />
  </ItemGroup>
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
  <ImportGroup Label="ExtensionTargets">
  </ImportGroup>
</Project>

Bottom line: When you get LNK2019 and none of the explanations on the internet help, check your project settings. If you use version control, compare the current project file with a known-good older version.

底线:当您获得 LNK2019 并且没有互联网帮助上的任何解释时,请检查您的项目设置。如果您使用版本控制,请将当前项目文件与已知良好的旧版本进行比较。

回答by Ganesh Kamath - 'Code Frenzy'

Probably you have declared the function in a class, but forgotten to use the scope resolution operator in its definition. Atleast that's what gave me that error.

可能您已经在类中声明了该函数,但忘记在其定义中使用范围解析运算符。至少这就是给我那个错误的原因。

回答by ndarriulat

In my case this was happening because a method in an abstract class, was virtualbut was not implemented in any of the sub classes.

就我而言,这是因为抽象类中的方法是虚拟的,但未在任何子类中实现。

However, this might be only one of the multiple causes of that LNK error.

但是,这可能只是 LNK 错误的多种原因之一。

回答by u1230329

Probably you forgot to include some source code files to the project, or you forgot to implement a function, etc. so your compiler cannot find it. ("LNK2019: unresolved external symbol ....").

可能你忘记在项目中包含一些源代码文件,或者你忘记实现一个函数等,所以你的编译器找不到它。(“LNK2019:未解析的外部符号......”)。

回答by Mihai8

If you look error LNK2019: unresolved externalit seem the problem is setting the subsystem. Your question is related to error LNK2019: unresolved external symbol.

如果您查看错误 LNK2019: unresolved external似乎问题出在设置子系统上。您的问题与错误 LNK2019: unresolved external symbol 相关

回答by Laurie Stearn

Point two on the MSDN pageapplied where function parameters were passed as pointers instead of the variable names corresponding to the module wide decls.

应用MSDN 页面上的第二点,其中函数参数作为指针传递,而不是与模块范围的声明对应的变量名称。

回答by prasad nijsure

You need to link your project with the Microsoft SDK libraries for errors involving socket odbc and server connections

对于涉及套接字 odbc 和服务器连接的错误,您需要将您的项目与 Microsoft SDK 库相关联

回答by Lucian Gabriel

Sometimes that error showed up when you don t have int main()function.

有时,当您没有int main()功能时会出现该错误。

回答by STF

I had this error - and my problem was that I call a function that didn't exist. So the visual studio looks for this function in other libraries and dlls.

我遇到了这个错误 - 我的问题是我调用了一个不存在的函数。所以visual studio在其他库和dll中寻找这个函数。