【问题标题】:Instead of linking with lws2_32 , which library is missing?不是与 lws2_32 链接,而是缺少哪个库?
【发布时间】:2016-10-25 13:02:31
【问题描述】:

我从套接字编程开始,我能够正确编译我的程序而没有任何错误,即命令

gcc -c file_name.c

没有错误,但是当我尝试将它与库链接并尝试创建一个可执行文件来运行我的代码时,即通过

gcc file_namme.o -o file_name -lws2_32

它会引发对 inet_pton 未定义引用的错误,我想知道我缺少哪个链接库以及为什么会出现此错误

我的代码如下

#include<stdio.h>
#include<stdint.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<winsock2.h>
#include<windows.h>
#include<ws2tcpip.h>
#define WIN32_LEAN_AND_MEAN
#pragma comment(lib, "Ws2_32.lib")
WSADATA wsaData;
void diewithusermsg(const char* msg , const char* detail);
void diewithsysmsg(const char* msg);
int main(int argc,char* argv[])
 {

if(argc<3 || argc>4)
{
    diewithusermsg("Parameters","<server address> <Echo String> <port      number>");
}
char* servip=argv[1];
char* echostring=argv[2];
 uint16_t servport=(argc==4)?atoi(argv[3]):7;
 int sock = socket(AF_INET, SOCK_STREAM , IPPROTO_TCP);
    if(sock<0){
        diewithsysmsg("socket() failed");
    }
    struct sockaddr_in servAddr;
    memset(&servAddr,0,sizeof(servAddr));
    servAddr.sin_family=AF_INET;
    int rtnVal = inet_pton(AF_INET,servip,&servAddr.sin_addr.S_un);
}
void diewithusermsg(const char* msg, const char* detail)
{
fputs(msg,stderr);
fputs(":",stderr);
fputs(detail,stderr);
fputs("\n",stderr);
exit(1);
}
void diewithsysmsg(const char*msg)
{perror(msg);
exit(1);
}

【问题讨论】:

  • @MichaelWalz 即使我提到了同样的事情,因为 file_name.c 将其加密为可理解格式的可编辑格式,即机器代码,但我正在寻找我缺少的库?
  • 尝试用InetPton替换inet_pton
  • 我已经试过了,然后它说对 InetPton 的未定义引用,即使是 inet_pton 的微软纪录片也没有说明我没有提到的任何其他内容
  • 您应该使用“-Wall -Wextra”选项进行编译以查看是否未声明某些函数
  • @purplepsycho 没有效果,同样的错误仍然存​​在

标签: c sockets unix cygwin tcp-ip


【解决方案1】:

您正在编写的程序不是 cygwin (unix) 程序,而是纯 Windows 程序。因此,您不能使用 gcc 编译器,而是使用 mingw64 交叉编译器(cygwin to mingw64)

安装 mingw64-x86_64-gcc-core(或 mingw64-i686-gcc-core),具体取决于您的架构,并使用 x86_64-w64-mingw32-gcc.exe(或 i686-w64-mingw32-gcc.exe)作为编译器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-22
    • 2015-08-03
    • 1970-01-01
    • 2013-02-17
    • 2017-07-11
    • 2016-06-21
    • 1970-01-01
    • 2019-05-19
    相关资源
    最近更新 更多