link error with fcntl in macos
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 addr开发者_如何学编程info 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.
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
.
精彩评论