macos 中 fcntl 的链接错误

发布于 2024-11-18 06:55:20 字数 1665 浏览 2 评论 0原文

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

#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中编译了它:

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

如何配置它才能成功通过编译? 我的海湾合作委员会版本是:

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.

There is a code sinppet using fcntl, but it goes into trouble when linking:

#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);

}

I compiled it in macos:

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

How do I configure it to pass compile successfully?
My GCC version is:

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼趣 2024-11-25 06:55:20

如果您查看链接器返回的错误,您应该会看到您的问题,在您的代码中,您正在使用 fcnt,而您很可能意味着 fcntl

If you look at the error returned by the linker you should see your problem, in your code you are using fcnt when you most likely meant fcntl.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文