Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to link wsock32 library through g++?

Tags:

c++

g++

linker

I'm using minGW on windows, trying to compile a c++ program. I've used sockets in there, so I'm trying to link (not include... I've already included winsock.h) the wsock32 library. I know that the -L switch is for linking but none of the following commands work:

g++ C:\program.cpp -Lwsock32.lib
g++ C:\program.cpp -LC:\windows\system32\wsock32.dll
g++ C:\program.cpp -lC:\windows\system32\wsock32.dll
g++ C:\program.cpp -LC:\windows\system32\wsock32.lib

what should I do??

like image 269
dsynkd Avatar asked Jan 24 '12 20:01

dsynkd


2 Answers

The -L option is for setting the directory where the linker should look for libraries/dlls.

The -l option is for naming the libraries/dlls you want to link with.

That would mean

g++ C:\Program.cpp -LC:\Windows\System32 -lwsock32

should be the command to compile your program from your regular windows command prompt.

I suspect your compiler may look in system32 automatically, so you may want to just try to skip the -L option.

like image 195
Joachim Isaksson Avatar answered Nov 09 '22 19:11

Joachim Isaksson


As @Joshua commented, you probably want ws2_32.dll.

The GNU Compiler Collection uses ranlib archives (A files) rather than Visual Studio LIB files.

The w32headers project provides gcc- and g++-compatible headers and archives for most standard Windows DLLs, including ws2_32.dll. The name of the archive is usually the name of the DLL minus the .dll extension, prefixed with lib and suffixed with .a (following the *nix archive naming convention). Thus, the symbols for ws2_32.dll are in libws2_32.a, which can be linked with using ‑lws2_32.

By default, the w32headers archives are in the GNU ld library path, so to be able to link with standard Windows DLLs, there is no need to add library directories with an ‑L option. In your case, the only option that you need is ‑lws2_32:

g++ C:\Program.cpp -lws2_32

Assuming that compilation and linking succeed, the output will be a.exe in the current directory. You can override the name of the output binary with the ‑o option.

NOTE: I used non-breaking hyphens in my answer. If you copy & paste the command line options, be sure to replace all hyphen-looking characters with regular hyphens.

like image 7
Daniel Trebbien Avatar answered Nov 09 '22 19:11

Daniel Trebbien