Home » Category » Microsoft Visual C & C++

Microsoft Visual C & C++: [Visual C++] Question on LNK 2001 Errors while Building EXE

205| Mon, 02 Jun 2008 12:03:00 GMT| anonymous| Comments (5)
Hello? I'm June from S.Korea.
I'm sorry if I am at a wrong place for my question.
I'm practicing echo client code. Below is what I copied from a
textbook.
The problem is,
there're 0 error, 0 warning at compiling.
But There're 10 errors at Building EXE files.
How can I troubleshoot them? I already #included winsock2.h, which has
the definition for the functions listed in the 10 error messages.And
the help says the linker doesn't find definition for the functions
listed.

Thanks for reading and still I'm not sure if I'm at a right place to
ask. But hope you will help me.

--Configuration: echo - Win32
Debug--
Linking...
echo_client_win.obj : error LNK2001: unresolved external symbol
__imp__WSACleanup...0
echo_client_win.obj : error LNK2001: unresolved external symbol
__imp__closesocket...4
echo_client_win.obj : error LNK2001: unresolved external symbol
__imp__recv...16
echo_client_win.obj : error LNK2001: unresolved external symbol
__imp__send...16
echo_client_win.obj : error LNK2001: unresolved external symbol
__imp__connect...12
echo_client_win.obj : error LNK2001: unresolved external symbol
__imp__htons...4
echo_client_win.obj : error LNK2001: unresolved external symbol
__imp__inet_addr...4
echo_client_win.obj : error LNK2001: unresolved external symbol
__imp__socket...12
echo_client_win.obj : error LNK2001: unresolved external symbol
__imp__WSAStartup...8
Debug/echo.exe : fatal error LNK1120: 9 unresolved externals
Error executing link.exe.

echo.exe - 10 error(s), 0 warning(s)

/*echo_client_win.c*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>

#define BUFSIZE 1024
void ErrorHandling(char *message);

int main(int argc, char **argv)
{
WSADATA wsaData;
SOCKET hSocket;
char message[BUFSIZE];
int strLen;

SOCKADDR_IN servAddr;

if(argc!=3)
{
printf("Usage : %s <IP> <port>/n", argv[0]);
exit(1);
}

if(WSAStartup(MAKEWORD(2, 2), &wsaData) !=0) /* load winsock 2.2
dll*/
ErrorHandling("WSAStartup() error!");

hSocket=socket(PF_INET, SOCK_STREAM, 0);

if(hSocket == INVALID_SOCKET)
ErrorHandling("socket() error");

memset(&servAddr, 0, sizeof(servAddr));
servAddr.sin_family=AF_INET;
servAddr.sin_addr.s_addr=inet_addr(argv[1]);
servAddr.sin_port=htons(atoi(argv[2]));

if(connect(hSocket, (SOCKADDR*)&servAddr,
sizeof(servAddr))==SOCKET_ERROR)
ErrorHandling("connect() error!");

while(1)
{
fputs("hello world! (q to quit) : ", stdout);
fgets(message, BUFSIZE, stdin); /* input from data console to be sent
*/
if(!strcmp(message, "q/n"))
break;
send(hSocket, message, strlen(message),0); /* send msg */
strLen=recv(hSocket, message, BUFSIZE-1, 0); /*receive msg*/
message[strLen]=0;
printf("msg from server : %s /n", message);
}

closesocket(hSocket);
WSACleanup();
return 0;
}

void ErrorHandling(char *message)
{
fputs(message, stderr);
fputc('/n', stderr);
exit(1);
}

Keywords & Tags: visual c++, lnk, 2001, errors, building, exe, microsoft, vc

URL: http://www.7prog.com/visual-c-c++/3239/
 
«« Prev - Next »» 5 helpful answers below.
I forgot to say I am using Visual C++ 6.0 .
again thanks for reading.:p

june | Mon, 02 Jun 2008 12:04:00 GMT |

"june" <gahaja2000...yahoo.com> wrote in message
news:1164897122.979393.179970...f1g2000cwa.googlegroups.com...
> Hello? I'm June from S.Korea.

Hi June, you need to include the Winsock library, WS2_32.lib, into your
project by adding it to the Linker settings. This library provides the
functionality of WSAStartup() as well as the other functions that the linker
has errors.
-- David

david | Mon, 02 Jun 2008 12:05:00 GMT |

>The problem is,
>there're 0 error, 0 warning at compiling.
>But There're 10 errors at Building EXE files.
>How can I troubleshoot them? I already #included winsock2.h, which has
>the definition for the functions listed in the 10 error messages.And
>the help says the linker doesn't find definition for the functions
>listed.

You also need to add any missing library file(s) to the list of linked
libraries your program uses (in the Linker project settings). The lib
file the function is in is usually listed in the documentation along
with the header file.

In your case you need to add Ws2_32.lib

Dave

david | Mon, 02 Jun 2008 12:06:00 GMT |

When you look at some call, look down at the bottom of the description. You will find
something like

Requirements
Client: Requires Windows XP, Windows 2000 Professional, Windows NT Workstation, Windows
Me, Windows 98, or Windows 95.
Server: Requires Windows Server 2003, Windows 2000 Server, or Windows NT Server.
Header: Declared in Winsock2.h.
Library: Use Ws2_32.lib.

Note the two relevant lines: the Header: line tells you what header file to include, and
the Library: line tells you what library to add to your link to avoid the unresolved
references. (The above lines were copied directly from the closesocket documentation)
joe

On 30 Nov 2006 06:32:03 -0800, "june" <gahaja2000...yahoo.com> wrote:

>Hello? I'm June from S.Korea.
>I'm sorry if I am at a wrong place for my question.
>I'm practicing echo client code. Below is what I copied from a
>textbook.
>The problem is,
>there're 0 error, 0 warning at compiling.
>But There're 10 errors at Building EXE files.
>How can I troubleshoot them? I already #included winsock2.h, which has
>the definition for the functions listed in the 10 error messages.And
>the help says the linker doesn't find definition for the functions
>listed.
>Thanks for reading and still I'm not sure if I'm at a right place to
>ask. But hope you will help me.
>
>--Configuration: echo - Win32
>Debug--
>Linking...
>echo_client_win.obj : error LNK2001: unresolved external symbol
>__imp__WSACleanup...0
>echo_client_win.obj : error LNK2001: unresolved external symbol
>__imp__closesocket...4
>echo_client_win.obj : error LNK2001: unresolved external symbol
>__imp__recv...16
>echo_client_win.obj : error LNK2001: unresolved external symbol
>__imp__send...16
>echo_client_win.obj : error LNK2001: unresolved external symbol
>__imp__connect...12
>echo_client_win.obj : error LNK2001: unresolved external symbol
>__imp__htons...4
>echo_client_win.obj : error LNK2001: unresolved external symbol
>__imp__inet_addr...4
>echo_client_win.obj : error LNK2001: unresolved external symbol
>__imp__socket...12
>echo_client_win.obj : error LNK2001: unresolved external symbol
>__imp__WSAStartup...8
>Debug/echo.exe : fatal error LNK1120: 9 unresolved externals
>Error executing link.exe.
>echo.exe - 10 error(s), 0 warning(s)
>
>
>
>
>/*echo_client_win.c*/
>#include <stdio.h>
>#include <stdlib.h>
>#include <string.h>
>#include <winsock2.h>
>#define BUFSIZE 1024
>void ErrorHandling(char *message);
>int main(int argc, char **argv)
>{
> WSADATA wsaData;
> SOCKET hSocket;
> char message[BUFSIZE];
> int strLen;
> SOCKADDR_IN servAddr;
> if(argc!=3)
> {
> printf("Usage : %s <IP> <port>/n", argv[0]);
> exit(1);
> }
> if(WSAStartup(MAKEWORD(2, 2), &wsaData) !=0) /* load winsock 2.2
>dll*/
> ErrorHandling("WSAStartup() error!");
> hSocket=socket(PF_INET, SOCK_STREAM, 0);
> if(hSocket == INVALID_SOCKET)
> ErrorHandling("socket() error");
> memset(&servAddr, 0, sizeof(servAddr));
> servAddr.sin_family=AF_INET;
> servAddr.sin_addr.s_addr=inet_addr(argv[1]);
> servAddr.sin_port=htons(atoi(argv[2]));
> if(connect(hSocket, (SOCKADDR*)&servAddr,
> sizeof(servAddr))==SOCKET_ERROR)
> ErrorHandling("connect() error!");
> while(1)
> {
> fputs("hello world! (q to quit) : ", stdout);
> fgets(message, BUFSIZE, stdin); /* input from data console to be sent
>*/
> if(!strcmp(message, "q/n"))
> break;
> send(hSocket, message, strlen(message),0); /* send msg */
> strLen=recv(hSocket, message, BUFSIZE-1, 0); /*receive msg*/
> message[strLen]=0;
> printf("msg from server : %s /n", message);
> }
> closesocket(hSocket);
> WSACleanup();
> return 0;
>}
>void ErrorHandling(char *message)
>{
> fputs(message, stderr);
> fputc('/n', stderr);
> exit(1);
>}
Joseph M. Newcomer [MVP]
email: newcomer...flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm

joseph | Mon, 02 Jun 2008 12:07:00 GMT |

After adding the library, EXE file was generated.
Though there're things to be done further, I thank you for the help.

june | Mon, 02 Jun 2008 12:08:00 GMT |

Microsoft Visual C & C++ Hot Answers

Microsoft Visual C & C++ New questions

Microsoft Visual C & C++ Related Categories