C++ Compile Winsock Problem
this last one week I got problem compile C++ code from MSDN Library about winsock.
the code :
#include
#include "winsock2.h"
void main() {
//----------------------
// Initialize Winsock
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != NO_ERROR)
printf("Error at WSAStartup()\n");
//----------------------
// Create a SOCKET for connecting to server
SOCKET ConnectSocket;
ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (ConnectSocket == INVALID_SOCKET) {
printf("Error at socket(): %ld\n", WSAGetLastError());
WSACleanup();
return;
}
//----------------------
// The sockaddr_in structure specifies the address family,
// IP address, and port of the server to be connected to.
sockaddr_in clientService;
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr( "127.0.0.1" );
clientService.sin_port = htons( 27015 );
//----------------------
// Connect to server.
if ( connect( ConnectSocket, (SOCKADDR*) &clientService, sizeof(clientService) ) == SOCKET_ERROR) {
printf( "Failed to connect.\n" );
WSACleanup();
return;
}
printf("Connected to server.\n");
WSACleanup();
return;
}
but when I compile it using Visual C++ 2008 express it give linker error :
1>------ Build started: Project: soketmenyoket, Configuration: Debug Win32 ------
1>Linking...
1>program.obj : error LNK2019: unresolved external symbol __imp__connect@12 referenced in function _main
1>program.obj : error LNK2019: unresolved external symbol __imp__htons@4 referenced in function _main
1>program.obj : error LNK2019: unresolved external symbol __imp__inet_addr@4 referenced in function _main
1>program.obj : error LNK2019: unresolved external symbol __imp__WSACleanup@0 referenced in function _main
1>program.obj : error LNK2019: unresolved external symbol __imp__WSAGetLastError@0 referenced in function _main
1>program.obj : error LNK2019: unresolved external symbol __imp__socket@12 referenced in function _main
1>program.obj : error LNK2019: unresolved external symbol __imp__WSAStartup@8 referenced in function _main
1>C:\Users\Sagi\Documents\Visual Studio 2008\Projects\soketmenyoket\Debug\soketmenyoket.exe : fatal error LNK1120: 7 unresolved externals
1>Build log was saved at "file://c:\Users\Sagi\Documents\Visual Studio 2008\Projects\soketmenyoket\soketmenyoket\Debug\BuildLog.htm"
1>soketmenyoket - 8 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
what should I do ?
first, you can scream out loud.... (just kidding)
* right click on project name in solution explorer and chose properties
* on Configuration Properties > C/C++ > Advance
at properies "Compile As" make sure it set as "Compile as C++ Code (/TP)" even when your code extension is .c
* on Configuration Properties > Linker > Input
at properties "Additional Depedencies", type "WS2_32.Lib" (without quotes)
what we do is it give reference to C++ linker to include WS2_32.lib in folder
C:\Program Files\Microsoft SDKs\Windows\v6.1\Lib
note : I using the latest windows SDK
*compile it..
1>------ Build started: Project: soketmenyoket, Configuration: Debug Win32 ------
1>Compiling...
1>program.c
1>Linking...
1>Embedding manifest...
1>Microsoft (R) Windows (R) Resource Compiler Version 6.0.5724.0
1>Copyright (C) Microsoft Corporation. All rights reserved.
1>Build log was saved at "file://c:\Users\Sagi\Documents\Visual Studio 2008\Projects\soketmenyoket\soketmenyoket\Debug\BuildLog.htm"
1>soketmenyoket - 0 error(s), 0 warning(s)
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
it's compile..
See you next Deculture
S.A.