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!

Dani AI

Generated

A short, practical summary for (and echoing ): the undefined-reference messages mean the linker never pulled in the Winsock import library and Dev-C++ (MinGW/GCC) will ignore MSVC-style pragmas. Fixes are: add the MinGW import library to the link step, make sure your includes are ordered correctly, and if you target Windows XP avoid APIs that XP does not implement (for example, some inet_pton variants). (stackoverflow.com)

How to add the library in Dev-C++ (two common ways):

  • No project: Tools → Compiler Options → check “Add the following commands when calling the compiler” and add the linker flag -lws2_32.
  • With a project: Project → Project Options → Parameters → Linker → “Add Library or Object” and pick libws2_32.a from your Dev-C++/MinGW lib folder.
    Or from the command line: g++ main.c -o main.exe -lws2_32. (studylib.net)

Why the pragmas don’t help: #pragma comment(lib, "...") is a Visual C++/MSVC mechanism. MinGW/GCC does not honor it, so the pragma in the source will be ignored — the linker needs the -l flag or the .a import library on its command line. (stackoverflow.com)

One extra pitfall on XP: inet_pton (InetPton) is supported starting with Windows Vista; calling/linking it on XP leads to unresolved symbols even if you link ws2_32. Use inet_addr/getaddrinfo/WSAStringToAddress or provide a small compatibility implementation when targeting XP. Also remember to call WSAStartup before socket calls and link the Winsock import library. (learn.microsoft.com)

Quick checklist to resolve the thread’s errors:

  • Remove or guard MSVC-only pragmas; add -lws2_32 (or the libws2_32.a) in Dev-C++ project or global compiler options. (studylib.net)
  • Include winsock2.h before windows.h (or define WIN32_LEAN_AND_MEAN) to avoid header conflicts. (sourceforge.net)
  • If inet_pton is used and target is XP, replace it or add a compatibility routine. (learn.microsoft.com)

References:

Recommended Answers

All 4 Replies

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.