【问题标题】:How to solve error LNK2019如何解决错误 LNK2019
【发布时间】:2013-01-31 04:48:48
【问题描述】:

我正在用 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)    

【问题讨论】:

  • 您似乎缺少源文件或库。
  • 很确定你还没有编译源代码或链接它。

标签: c++ linker unresolved-external lnk2019


【解决方案1】:

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

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

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

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

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      在我的例子中,这是因为抽象类中的方法是虚拟的,但没有在任何子类中实现。

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

      【讨论】:

        【解决方案4】:

        如果您查看error LNK2019: unresolved external,似乎问题在于设置子系统。您的问题与error LNK2019: unresolved external symbol 有关。

        【讨论】:

        • 我发现问题 WS2_32.lib 应该添加到属性->链接器->输入->附加依赖项中。除了以下错误之外,所有错误都已消失。请帮帮我。错误1错误LNK2019:函数_wmain中引用的未解析的外部符号_send_mail
        • send_mail 是在site you linked to 的源代码中定义的。它被称为smtpfuncs.c。你需要编译它并链接它。
        • 嗨,彼得,感谢您的回复,我使用 smtpfuncs.cpp 而不是 smtpfuncs.c,但有什么大不了的。当我保存在 .c 中并在保存时提供链接器 pblm .cpp。我没听懂。
        • C 和 C++ 是不同的语言,您的编译器编译它们的方式也不同。要使 C++ 中的 C 函数可用,您需要将其包装在 extern "C" { 块中。
        【解决方案5】:

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

        【讨论】:

        • 作为评论可能会更好。
        【解决方案6】:

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

        【讨论】:

          【解决方案7】:

          您需要将您的项目与 Microsoft SDK 库链接,以解决涉及套接字 odbc 和服务器连接的错误

          【讨论】:

            【解决方案8】:

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

            【讨论】:

              【解决方案9】:

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

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2013-11-22
                • 1970-01-01
                • 2013-11-13
                • 2014-05-12
                • 2014-05-14
                • 1970-01-01
                • 2013-01-18
                相关资源
                最近更新 更多