6

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.html The sample program seems to hit the following error when it is compiling. Please help me with solution.

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)    
Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
user1
  • 687
  • 7
  • 13
  • 24

9 Answers9

11

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

When I added new dependencies to my project by editing the vxcproj file, 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).

It went unnoticed for a while, because I used a batch script to compile the code in Release mode. When I switched to Debug mode, 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 .vcxproj file:

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

AJM
  • 1,317
  • 2
  • 15
  • 30
Rob W
  • 341,306
  • 83
  • 791
  • 678
9

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.

4

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

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

ndarriulat
  • 749
  • 1
  • 9
  • 11
3

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

Community
  • 1
  • 1
Mihai8
  • 3,113
  • 1
  • 21
  • 31
  • 2
    I have found out the problem WS2_32.lib should be added in properties->linker->input->additional dependencies.All the errors has gone except the below error.please hlp me out. Error 1 error LNK2019: unresolved external symbol _send_mail referenced in function _wmain – user1 Feb 15 '13 at 09:28
  • `send_mail` is defined in the source code on the [site you linked to](http://cboard.cprogramming.com/cplusplus-programming/125655-sending-simple-email-cplusplus.html). It's called `smtpfuncs.c`. You need to compile it and link it. – Peter Wood Feb 15 '13 at 10:51
  • 1
    Hi Peter thanks for the reply, i used smtpfuncs.cpp instead of smtpfuncs.c, but whats the big deal in it.Its working when i save in .c and giving the linker pblm when saved in .cpp.I didnt get that. – user1 Feb 15 '13 at 11:09
  • 3
    C and C++ are different languages, and your compiler compiles them differently. To make the functions available to C from C++ you need to wrap it in an `extern "C" {` block. – Peter Wood Feb 15 '13 at 13:07
3

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

Scott 混合理论
  • 2,263
  • 8
  • 34
  • 59
1

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

Laurie Stearn
  • 959
  • 1
  • 13
  • 34
0

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

0

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

Milan Pansuriya
  • 2,521
  • 1
  • 19
  • 33
Lucian Gabriel
  • 164
  • 2
  • 11
-1

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.

Box Box Box Box
  • 5,094
  • 10
  • 49
  • 67
STF
  • 1,485
  • 3
  • 19
  • 36