ws2_32.lib vs. libws2_32.a, what's the difference and how to link libws2_32 to NB project?

一世执手 提交于 2020-01-13 06:58:11

问题


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:

  • Add Library...
  • Add Library File...
  • Add Option...

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?


回答1:


  1. It seems to me that you've added the -lws2_32 switch to the C/C++ compiler options. When netbeans invokes the compiler, it passes on the -c switch and it'll ignore the linker options, such as -l. In the linker options section, Netbeans has an apropriate place to add external libraries. Or you can add the -l as an extra option to the linker. That might solve the problem.

  2. *.lib files are used by the Microsoft toolchain (cl.exe) and lib*.a are used by GNU toolchain (that's the piece of adivse you've found). If you're going to use Cygwin, you'll need the lib*.a files. In this context, having Microsoft SDK won't help you at all. Also, if you need a file that only exists in .lib format, you can convert to.a` with a tool called reimp.

Hope this helps.




回答2:


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

  • Add C:\MinGW\lib to library paths tab
  • Add ws2_32 to libraries tab

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.



来源:https://stackoverflow.com/questions/12314779/ws2-32-lib-vs-libws2-32-a-whats-the-difference-and-how-to-link-libws2-32-to-n

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!