-
Using WinSock2
Dear Friends:
I am trying to use WinSock2 API to create a simple server. However, I encountered a strange linking problem:
------ Build started: Project: Sock, Configuration: Debug Win32 ------
Compiling...
Sock.cpp
Linking...
Sock.obj : error LNK2001: unresolved external symbol "unsigned int __stdcall socket(int,int,int)" (?socket@@$$J212YGIHHH@Z)
Debug/Sock.exe : fatal error LNK1120: 1 unresolved externals
Build log was saved at "file://c:\Documents and Settings\Chi Chen\My Documents\Visual Studio Projects\Sock\Debug\BuildLog.htm"
Sock - 2 error(s), 0 warning(s)
---------------------- Done ----------------------
Build: 0 succeeded, 1 failed, 0 skipped
The source is as the following:
<code>
// This is the main project file for VC++ application project
// generated using an Application Wizard.
#include "stdafx.h"
#include
#using
#include
using namespace System;
// This is the entry point for this application
int _tmain(void)
{
// create a TCP socket
//
// see WinSock2.h for related macros
SOCKET server = 0;
server = socket(AF_INET, SOCK_STREAM, 0/*IPPROTO_TCP*/);
if(server == INVALID_SOCKET)
{
Console::WriteLine(S"*** Oops... ***");
}
return 0;
}
</code>
The stdafx.h file is empty.
My include path:
C:\Program Files\Microsoft SDK\include;C:\Program Files\Microsoft Visual Studio .NET\FrameworkSDK\include\;C:\Program Files\Microsoft Visual Studio\VC98\atl\include;C:\Program Files\Microsoft Visual Studio\VC98\mfc\include;C:\Program Files\Microsoft Visual Studio\VC98\include
My lib path:
C:\Program Files\Microsoft SDK\Lib;C:\Program Files\Microsoft Visual Studio .NET\FrameworkSDK\Lib\;C:\Program Files\Microsoft Visual Studio\VC98\mfc\lib;C:\Program Files\Microsoft Visual Studio\VC98\lib
Does anyone know what the problem is? Thank you very much in advance.
Thanks,
Chi
-
I believe....
By what you have given me, that the error is a linker error. It's looking for the symbol of the function socket(). But what's weird is I believe socket() call is 'C' named, so it's name shouldn't be decorated. (But it is in the linker error). First of all, the problem is that the WS2_32.LIB file is NOT a default library. You have to either #pragma it in, or add it to the list of LIB files included by your application.
As for the name decoration - maybe for some reason, it thinks that there is a socket() call somewhere in a namespace or something. (I see a using namespace directive)
-
Thanks very much for your reply JamesSchumacher.
Could you please elaborate the following points? Thanks! :-)
1. WS2_32.LIB file is NOT a default library. You have to either #pragma it in, or add it to the list of LIB files included by your application.
2. As for the name decoration - maybe for some reason, it thinks that there is a socket() call somewhere in a namespace or something. (I see a using namespace directive)
-
Add Ws2_32.lib in the project settings->link General category, Object/Library text entry line.
-
Thanks very much for your kind response Mick_2002.
I am using Visual Studio .NET, however, and I can' seem to find where I can add ws2_32.lib the the lib list. I know that that's how it's done in VC6.
ws2_32.lib is in Microsoft SDK/lib directory.
Thanks,
Chi
-
Yea I haven't messed with .NET. But it's got to have some setting where you can tell it what lib you want to link to, I'd think.
-
VC6 #pragma method....
should probably work in VC7.
Code:
#pragma comment(lib,"WS2_32.LIB")
Put it in StdAfx.h or any .cpp file of your project. This embeds into the object file that the object requires linking with WS2_32.LIB (which is the import library for Winsock 2 implementation).
-
It Works!
Thanks James.
It works!
I also found another solution to it:
1) Right click on the project root within "Solution Explorer" window.
2) Click on Properties
3) Under Linker/Input, add ws2_32.lib to "Additional Dependencies" entry
4) Click "Apply" and then "OK"
5) Build it
Thanks again for your help. It's highly appreciated. :-D
Chi