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?

查看:3517
本文介绍了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<< Initialization OK。;

return 0;
}

我尝试运行项目时遇到问题:

 未定义引用`_WSAStartup @ 8'

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

  C:\cygwin\usr\include\w32api 

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

  C:\cygwin\lib\w32api 

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

  libws2_32.a //在Cygwin中

ws2_32.lib //在Windows中

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

  cd C:\\c ++ \\myProgram //转到dir 
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之间的区别< c $ c> .lib 文件和Cygwin .a 文件?是否最好安装Windows SDK,只是忘记那些 .a 文件?到目前为止,我在Cygwin网站上找到的所有东西都是这样的:


导入库是一个普通的类似UNIX的.a库,它只有
包含了一点点的信息,告诉操作系统你的
程序如何与(导入)dll交互。此信息已链接
到您的.exe。这也是由dlltool生成的。


3)是否可以使用 #pragma comment libws2_32.a)链接到 .a 文件?



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


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


  2. *。lib 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天全站免登陆