ws2_32.lib与libws2_32.a有什么区别,以及如何将libws2_32链接到NB项目? [英] ws2_32.lib vs. libws2_32.a, what's the difference and how to link libws2_32 to NB project?

查看:702
本文介绍了ws2_32.lib与libws2_32.a有什么区别,以及如何将libws2_32链接到NB项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用NetBeans,Windows和Cygwin与g ++编译器。



我正在检查Windows Sockets 2.我做了所有写在MS中的所有内容手册。我有一个代码(主要来自本手册):

  #include< winsock2.h> 
#include< ws2tcpip.h>

#include< cstdlib>
#include< iostream>

#pragma comment(lib,Ws2_32.lib)

int main(){

WSADATA wsaData;

int iResult;

//初始化Winsock
iResult = WSAStartup(MAKEWORD(2,2),& wsaData);
if(iResult!= 0){
printf(WSAStartup failed:%d\\\
,iResult);
return 1;
}
else cout<<< 初始化好了。

return 0;
}

当我尝试运行该项目时,我有一个问题:

 未定义的引用_WSAStartup @ 8

我明白,缺少 Ws2_32.lib 。这是因为我没有安装Windows SDK。但是在安装之前,我想尝试Cygwin提供的工具。它有所有的 w32api 头文件,我有他们在:

  C:\cygwin\usr\include\w32api 

它有一些 w32api 几乎 .lib 目录中的文件:

  C:\cygwin\lib\w32api 

但是这些lib文件是不同的,他们有 .a 扩展名和一些不同的名称,如:

 code> libws2_32.a // in Cygwin 
vs.
ws2_32.lib // in Windows

当我使用Cygwin终端创建一个 .exe 文件时,一切正常。我输入的命令是:

  cd C:\\c ++ \\myProgram //转到目录
g ++ myProgram.cpp -lws2_32 //编译使用-l选项链接libws2_32.a

之后,我得到 a.exe 文件。我运行它,它的工作原理:

  ./ a.exe //初始化确定。 

但正如我所说,我使用NetBeans。如果我尝试从NB( [F6] 按钮)运行项目,我总是有这个错误未定义的引用'_WSAStartup @ 8' code>。



我已经尝试了在NB论坛上可以找到的一切。我试图通过这种方式将 libws2_32.a 链接到我的项目。我去:

 文件 - >项目属性 - >链接器 - >图书馆

有三个选项:




  • 添加库...

  • 添加库文件...

  • 添加选项...



我已经尝试过了。我试图链接库和库文件。我还尝试
添加选项... 按钮中添加这样一个选项:

 添加选项...  - >其他选项 - > //我在这里输入-lws2_32

但无论如何,我无法运行项目从NB,我得到错误未定义引用'_WSAStartup @ 8'



所以我的问题是:



1)我做错了什么?如何从NB运行项目?我没有尝试安装Windows SDK,我想尝试使用Cygwin工具,因为它有这样的工具。



2)Windows .lib 文件和Cygwin .a 文件?最好安装Windows SDK,只是忘记那些 .a 文件?我可以在Cygwin网站上找到关于他们的一切,这是:


导入库是常规的类UNIX的.a库,但是它只有
包含告诉操作系统您的
程序如何与(进口)dll交互所需的微小信息。这个信息链接到
到你的.exe。这也是由dlltool生成的。


3)是否可以使用 #pragma comment(lib, libws2_32.a)链接 .a 文件?我试过但没有获得成功。



UPD:



第三个问题的答案 - > #pragma comment(lib,xxx .lib)相当于Linux?

解决方案


  1. 我已经将 -lws2_32 切换到C / C ++编译器选项。当netbeans调用编译器时,它会传递 -c 开关,它将忽略链接器选项,例如 -l <​​/ code>。在链接器选项部分中,Netbeans具有添加外部库的适当位置。或者您可以将 -l <​​/ code>添加为链接器的额外选项。这可能会解决问题。


  2. *。lib 文件由Microsoft工具链(cl。 exe)和 lib * .a 由GNU工具链(这是您发现的adivse)使用。如果要使用Cygwin,您将需要 lib * .a 文件。在这种情况下,拥有Microsoft SDK将无法帮助您。另外,如果您需要的文件仅存在于 .lib格式中,您可以使用一个名为 .a。 mingw.org/wiki/MSVC_and_MinGW_DLLsrel =nofollow> reimp


希望这有帮助。


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. 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.

这篇关于ws2_32.lib与libws2_32.a有什么区别,以及如何将libws2_32链接到NB项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆