Member Avatar for Member #25180

Hey everyone, I'm working on my first program using "Winsock2.h".

I'm getting back linker errors "error LNK2001: unresolved external symbol ..."
9 unresolved externals total

The program came from an MSDN tutorial: "A simple winsock program", its a simple ping program that sends packets to a loopback address.

when I include winsock2.h I use

#include <stdio.h>         // for printf
#include "winsock2.h"

I'm sure its got something to do with define, but I've never worked with that in my programs before. If someone can point me to a tutorial, I'd be very happy. Thanks for viewing.

Dani AI

Generated

Brief summary and a few practical corrections tied to the replies: was right that the linker was missing the socket library, and confirmed the problem went away by forcing a link from the source. That works because unresolved externals mean the compiler saw declarations (from the headers) but the linker couldn't find the actual function implementations in any linked library.

If you are using winsock2.h prefer linking the Winsock 2 import library (Ws2_32.lib) rather than the older Winsock 1.x library. In Visual Studio: open the Project Properties → Configuration Properties → Linker → Input and add Ws2_32.lib to Additional Dependencies. If the linker cannot find the file, point Linker → General → Additional Library Directories to your Windows/Platform SDK lib folder. With MinGW/GCC the equivalent is -lws2_32. Using a source-level pragma to force a link is a valid quick fix, but setting the project link input keeps the build configuration explicit.

Common pitfalls and quick troubleshooting:

  • Make sure winsock2.h is included before windows.h, or define WIN32_LEAN_AND_MEAN before including windows.h so the old winsock.h is not pulled in and cause conflicts.
  • Verify you call WSAStartup at runtime (link errors are separate from runtime initialization, but missing startup is still a frequent socket bug).
  • Check target architecture: linking x86 libs into an x64 build (or vice versa) produces unresolved symbols.
  • If errors persist, enable verbose linker output to see which libraries the linker searched and confirm the missing symbol names — that often points to either the wrong import library or C++ name-mangling (extern "C") issues.

This expands on the fixes already shown in the thread while addressing the include-order and project-configuration gaps that commonly cause LNK2001 for Winsock code.

Recommended Answers

All 4 Replies

Did you link to wsock32.lib?

Member Avatar for Member #25180

I googled for how to link a library and haven't found anything yet.
I'm using Visual Studio .NET for my C++ compiler. Some webpages that google turned up say that you can go into "Project" --> "Settings" --> "Linker" but there isn't a tab for settings under "Project". I guess thats a 6.0 thing and not a .NET thing.

I will be searching and google'n some more on how to link. I will also be checking up on here every now and then if you, Narue, have any info on how to link using .NET. Thanks for the help.

Member Avatar for Member #25180

I searched on how to link Wsock32.LIB and found out how.

#include <stdio.h>        // for printf
#include "winsock2.h"

#pragma comment( lib, "wsock32.lib" )     // This will link to wsock32.lib

void main()
{
       // Winsock stuff
}

I just typed in that little line and bam... everything a...o...k.

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.