首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >macos中的fcntl出现链接错误

macos中的fcntl出现链接错误
EN

Stack Overflow用户
提问于 2011-07-01 15:13:58
回答 1查看 1K关注 0票数 1

有一个使用fcntl的代码sinppet,但它在链接时遇到了麻烦:

代码语言:javascript
复制
#include <poll.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <fcntl.h>

int main()
{
    struct addrinfo hints, *ai, *cur_ai;
    int port, fd = -1;
    memset(&hints, 0, sizeof(hints));
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;

    char portstr[10];
    char hostname[] = "www.google.com";
    snprintf(portstr, sizeof(portstr), "%d", port);
    int ret = getaddrinfo(hostname, portstr, &hints, &ai);
    if(ret)
    {
        printf("die");
        return ;
    }
    printf("getaddrinfo !!!");

    cur_ai = ai;
    fd = socket(cur_ai->ai_family, cur_ai->ai_socktype, cur_ai->ai_protocol);
    fcntl(fd, F_SETFL, fcnt(fd, F_GETFL) | O_NONBLOCK);
    ret = connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);

}

我用macos编译的:

代码语言:javascript
复制
qty:socket_lab qrtt1$ gcc main.c
Undefined symbols for architecture x86_64:
  "_fcnt", referenced from:
      _main in ccCndweM.o
     (maybe you meant: _fcntl)
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

如何配置才能成功通过编译?我的GCC版本是:

代码语言:javascript
复制
qty:socket_lab qrtt1$ gcc --version
i686-apple-darwin10-gcc-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5666) (dot 3)
Copyright (C) 2007 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-07-01 15:15:53

如果你查看链接器返回的错误,你应该会看到你的问题,在你的代码中,你使用的是fcnt,而你最有可能的意思是fcntl

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6544775

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档