I use NetBeans, Windows and Cygwin with g++ compiler.
I'm examining Windows Sockets 2. I do everything that is written in MS manual. I have a code (mostly from this manual):
#include <winsock2.h>
#include <ws2tcpip.h>
#include <cstdlib>
#include <iostream>
#pragma comment(lib, "Ws2_32.lib")
int main() {
WSADATA wsaData;
int iResult;
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed: %d\n", iResult);
return 1;
}
else cout << "Initialization OK.";
return 0;
}
And I have a problem when I try to run the project:
undefined reference to `_WSAStartup@8'
I understand that Ws2_32.lib
is missing. This is because I do not have Windows SDK installed. But before installing it I want to try out tools that Cygwin offers. It has all the w32api
header files, I have them in:
C:\cygwin\usr\include\w32api
And it has some w32api
almost .lib
files in the directory:
C:\cygwin\lib\w32api
But all these lib files are different, they have .a
extension and a little bit different name, like:
libws2_32.a // in Cygwin
vs.
ws2_32.lib // in Windows
When I use Cygwin terminal to create an .exe
file, everything works fine. The commands I input are:
cd C:\\c++\\myProgram // go to the dir
g++ myProgram.cpp -lws2_32 // compile using -l option to link libws2_32.a
And after it I get a.exe
file. I run it and it works:
./a.exe // Initialization OK.
But as I said I use NetBeans. And if I try to run the project from NB ([F6]
button) I always have this error undefined reference to '_WSAStartup@8'
.
I've tried already everything I could find on NB forums. I've tried to link libws2_32.a
to my project this way. I go to:
File -> Project Properties -> Linker -> Libraries
And there are three options:
I've tried them all. I've tried to link both just Library and Library File. I've also tried
to add such an option in the Add Option...
button:
Add Option... -> Other option -> // and I input here "-lws2_32"
But whatever I do I can't run the project from NB, I get error undefined reference to '_WSAStartup@8'
.
So my questions are:
1) What do I do wrong? How may I run the project right from NB? I didn't try to install Windows SDK, I want to try with Cygwin tools as it has such kind of tools.
2) What is the difference between Windows .lib
files and Cygwin .a
files? Is it better to install Windows SDK and just forget about those .a
files? Everything I could find so far about them on Cygwin site is this:
The import library is a regular UNIX-like .a library, but it only contains the tiny bit of information needed to tell the OS how your program interacts with ("imports") the dll. This information is linked into your .exe. This is also generated by dlltool.
3) Is it possible to use #pragma comment(lib, "libws2_32.a")
to link .a
files? I've tried but didn't get success results.
UPD:
The answer for the 3rd question -> #pragma comment(lib, "xxx.lib") equivalent under Linux?
I had this problem Eclipse/CDT/Windows. This is my build command
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o source\Sever_B.o ..\source\Sever_B.cpp
g++ -LC:\MinGW\lib -o Sever_B.exe source\Sever_B.o -lws2_32
So on the Eclipse project properties, C/C++ General, Paths & Symbols
This links libws2_32.a to my project and it now builds OK.
I tried using the Windows ws2_32.dll and ws2_32.lib and got nothing but pain.