2013-02-26 93 views
0

以下是示例代码部分来自http://curl.haxx.se/libcurl/c/sendrecv.html工作:堆栈select()函数没有在Visual Studio

static int wait_on_socket(curl_socket_t sockfd, int for_recv, long timeout_ms) 
{ 
    struct timeval tv; 
    fd_set infd, outfd, errfd; 
    int res; 

    tv.tv_sec = timeout_ms/1000; 
    tv.tv_usec= (timeout_ms % 1000) * 1000; 

    FD_ZERO(&infd); 
    FD_ZERO(&outfd); 
    FD_ZERO(&errfd); 

    FD_SET(sockfd, &errfd); /* always check for error */ 

    if(for_recv) 
    { 
    FD_SET(sockfd, &infd); 
    } 
    else 
    { 
    FD_SET(sockfd, &outfd); 
    } 

    ///* select() returns the number of signalled sockets or -1 */ 
    res = select(sockfd + 1, &infd, &outfd, &errfd, &tv); 
    return res; 
} 

它好工作在Linux上,但是当我把它在Visual Studio 2010中,它抱怨select()函数。我正确地编译和链接了所有的cURL库/ DLL,甚至我还以http请求的形式下载了一些东西。

但是,当我尝试使用套接字select()函数,它抱怨,出现以下错误:

error LNK2028: unresolved token (0A00034C) "extern "C" int __stdcall select(int,struct fd_set  *,struct fd_set *,struct fd_set *,struct timeval const *)" ([email protected]@[email protected]@[email protected]@@Z) referenced in function "int __cdecl `anonymous namespace'::wait_on_socket(unsigned int,int,long)" ([email protected][email protected]@[email protected]) 

error LNK2019: unresolved external symbol "extern "C" int __stdcall select(int,struct fd_set *,struct fd_set *,struct fd_set *,struct timeval const *)" ([email protected]@[email protected]@[email protected]@@Z) referenced in function "int __cdecl `anonymous namespace'::wait_on_socket(unsigned int,int,long)" ([email protected][email protected]@[email protected]) 

而且,似乎select()函数与Windows兼容: http://msdn.microsoft.com/en-us/library/ms740141(VS.85).aspx

我错过了什么吗?有人能告诉我如何解决这个问题吗?

谢谢,--Rudy

回答

0

您必须链接到Microsoft套接字库。告诉你的链接器在你的项目中使用Ws2_32.lib。 在Visual Studio 2010中,将此lib添加到Project-> Properties-> Linker-> Input-> Add。依赖。

或用添加代码这里面的lib:

#pragma comment(lib, "Ws2_32.lib")