Hello all. I'm trying to compile a socket program in DevC++ but everytime I do it I get a lot of linker errors like these:

[Linker error] undefined reference to `WSAStartup@8'
[Linker error] undefined reference to `htons@4' 
[Linker error] undefined reference to `inet_pton' 
[Linker error] undefined reference to `socket@12'

I have previously run the socket program successfully in Linux but I have to export it to WindowsXP, and I must admit I'm relatively new to programming in WindowsXP. These are the header files I used in the WindowsXP version:

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
//#include <winsock.h>
#include <winsock2.h>
#include <stdio.h>
#include <string.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
// Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")

I just found those header files in sample socket programs in the internet and I don't know if I really need all of them, and whether I should use winsock.h or winsock2.h. I also learned that to solve these linker problems I need to link the libraries to the program and that's my main problem now. How do I link the libraries in WindowsXP and make the program in DevC++ work? By the way I'm using C programming language. Please help me solve this problem. Thanks!

Check the compiler output screen to see if it recognizes thse pragmas. I know those functions are in ws2_32.lib. Its possible the pragmas were ignored.

Edited 6 Years Ago by Ancient Dragon: n/a

Your program must link to ws2_32.lib

Hey Ancient Dragon thanks for the quick reply. First off, I'd like to say that I tried downloading code::blocks and vc++ as you previously suggested but my OS and its SP isn't supported by vc++ so I resorted to devc++ instead.
Back to the topic, yes I know I need to link the program to some libraries, but how do I actually link it? It sounds silly but I really have no clue on how to do it. Hope you could explain a little bit further how. Thanks again.

I don't know how to do it with dev-c++ because I don't use it any more. Look around the options, there should be a place where you can enter the library names.

What OS/SP are you using?

I don't know how to do it with dev-c++ because I don't use it any more. Look around the options, there should be a place where you can enter the library names.

What OS/SP are you using?

I'm using WindowsXP service pack 2. When I tried to download the vc++ I read that it requires service pack 3 or higher. I can't download service pack 3 for winxp so I decided to use devc++.

I added in the setting of devc++'s environment the directory where winsock.dll is located but I still got the same linker errors. I read somewhere that maybe I need to download microsoft sdk and add them to the compiler settings but I'm already thinking about downloading Cygwin instead. I think this will make the compilation of my Linux program in windows much much simpler. What's your take on this? Thanks

This article has been dead for over six months. Start a new discussion instead.