Winsock2 undefined reference Error -Linking??

Started by
8 comments, last by Erik Rufelt 15 years, 4 months ago
I am trying out some code I found in a MUD book to create a socket. I am getting errors about undefined references. Ex. undefined reference to `_socket@12' undefined reference to `_WSAStartup@8' I think it is a linking issue or maybe the library i am using. I am using Code:Blocks IDE Here are my includes: #include <iostream> #include "winsock2.h" #include "ws2tcpip.h" Does the order matter? And does it matter if I write them like this? #include <iostream> #include <winsock2.h> #include <ws2tcpip.h> Is there a library in Code:Blocks I should use instead of winsock2.h? Also I searched the forums but could not reply to retired threads... here is another thread I looked at for similar errors. http://www.gamedev.net/community/forums/topic.asp?topic_id=466034 Is there a better reference for this stuff? I am using th Mud Game Programming book... he states not to use int for sockets and use the typedef but his code uses int. =/.
3. D0n’t us3 l33t sp34k. 3v3r.
Advertisement
Yes it's a linking problem. Your #includes however are correct, but they are 'headers', not 'libraries', they are different things. I'm not sure how to do it in the Code:Blocks IDE, but you need to somehow link the library WS2_32.lib to your project. It will probably be under project settings and linker input files.
Quote:Original post by Erik Rufelt
Yes it's a linking problem. Your #includes however are correct, but they are 'headers', not 'libraries', they are different things. I'm not sure how to do it in the Code:Blocks IDE, but you need to somehow link the library WS2_32.lib to your project. It will probably be under project settings and linker input files.


would this be a pragma statement?
3. D0n’t us3 l33t sp34k. 3v3r.
They can be done like that I think.. I'm using Visual Studio 2008 and I just go into project settings and add them there instead though.
Try google for "code blocks add linker libraries" or similar. I tried it and found that you can rightclick your project in the workspace list, choose Build Options, and then the Linker tab, and add libraries there.
If you have more problems, someone who uses the same IDE will probably see your topic soon =)
Quote:Original post by Erik Rufelt
They can be done like that I think.. I'm using Visual Studio 2008 and I just go into project settings and add them there instead though.
Try google for "code blocks add linker libraries" or similar. I tried it and found that you can rightclick your project in the workspace list, choose Build Options, and then the Linker tab, and add libraries there.
If you have more problems, someone who uses the same IDE will probably see your topic soon =)


hmm i am still having problems linking in code blocks. I can edit the Global Variables theres an option to set up base, include, lib and obj variables. The problem here is that only one folder can be set up?? So if i am using irrlicht i cant use winsock2? Maybe i am missing the basics of linking is there a good reference for this? PS> been a while since I coded last I was using Dev ++.
3. D0n’t us3 l33t sp34k. 3v3r.
Winsock is in the standard library folder, the same as all windows libraries that are automatically linked in, so you shouldn't have to add a directory, just add WS2_32.lib to the link libraries.
I found this link, though for another library but the same rules should apply: http://wiki.codeblocks.org/index.php?title=Using_SDL_with_Code::Blocks.
If you scroll down to the screenshot you see two windows, you only need to worry about the project settings, the left one, since the library is in the standard folder. Where you see it says winmm, gdi32, etc. you should add ws2_32, in that Project Build options dialog on the Linker tab.
Quote:Original post by Erik Rufelt
Winsock is in the standard library folder, the same as all windows libraries that are automatically linked in, so you shouldn't have to add a directory, just add WS2_32.lib to the link libraries.
I found this link, though for another library but the same rules should apply: http://wiki.codeblocks.org/index.php?title=Using_SDL_with_Code::Blocks.
If you scroll down to the screenshot you see two windows, you only need to worry about the project settings, the left one, since the library is in the standard folder. Where you see it says winmm, gdi32, etc. you should add ws2_32, in that Project Build options dialog on the Linker tab.


I found libws2_32.a at C:\Program Files\CodeBlocks\MinGW\lib - Is this what you were referring to? Do you know what a "*.a" file is in this case... Is it an assembly file?

That link helped a bit at least I can add libraries now, maybe I have to set up a template. I will keep learning code blocks and try to post a solution in case anyone has the same issue.

Thanks for your help thus far.
3. D0n’t us3 l33t sp34k. 3v3r.
On UNIX, a ".a" is an "object achive" which is a static library, and a ".so" is a "shared object" which is a dynamic library.

On Windows, you always link against a static library, but for DLLs, the static library is a simple stub that causes the dynamic library to be loaded. In this case, the ".a" file is the static library (link library) for the system-supplied ws32_2.dll dynamic link library.

And this probably ain't the right forum to discuss how compilers and linkers work, but I'll let it slide this time :-)
enum Bool { True, False, FileNotFound };
Quote:Original post by hplus0603
On UNIX, a ".a" is an "object achive" which is a static library, and a ".so" is a "shared object" which is a dynamic library.

On Windows, you always link against a static library, but for DLLs, the static library is a simple stub that causes the dynamic library to be loaded. In this case, the ".a" file is the static library (link library) for the system-supplied ws32_2.dll dynamic link library.

And this probably ain't the right forum to discuss how compilers and linkers work, but I'll let it slide this time :-)


Cool thanks for the info... I am learning more everyday =). I was able to get the linking to work. In code blocks under the tab settings-> compiler and debugger you can set the global compiler settings for the linker and search directories.

So I had to add the .a file to the link libraries: under the linker settings tab.

And the include and lib files under the search directories tab.

One more question I asked previously...

If I #include <winsock2> vs. #include "winsock2.h" what is the difference?
3. D0n’t us3 l33t sp34k. 3v3r.
Usually with <> you search the system include directories, while with "" you search the project directories. So use <> for standard headers, and "" for your own headers.

This topic is closed to new replies.

Advertisement