2011-06-02 136 views
0

我正在寫一個C代碼,其中我使用套接字。我在Windows和Linux上使用它,所以我嘗試動態編寫它。我寫了兩個函數來獲取服務器/客戶端套接字。在那裏,我對編譯錯誤:無效的函數參數

debian:~/cross_socket# make 
gcc *.c -Wall 
i586-mingw32msvc-gcc *.c -Wall -lws2_32 
socket.c: In function 'get_client_socket': 
socket.c:28: warning: passing argument 3 of 'get_socket' from incompatible pointer type 
socket.c:13: note: expected 'int (*)(SOCKET, const struct sockaddr *, int)' but argument is of type 'int (*)(SOCKET, const struct sockaddr *, int)' 
socket.c: In function 'get_server_socket_addr': 
socket.c:43: warning: passing argument 3 of 'get_socket' from incompatible pointer type 
socket.c:13: note: expected 'int (*)(SOCKET, const struct sockaddr *, int)' but argument is of type 'int (*)(SOCKET, const struct sockaddr *, int)' 

GCC對我的本地系統的偉大工程,但如果我使用的MinGW我得到這個錯誤?!? 它說: 我用int (*)(SOCKET, const struct sockaddr *, int)而不是int (*)(SOCKET, const struct sockaddr *, int) ...?

這裏的相關代碼:

inline SOCKET get_socket(uint16_t port, in_addr_t socket_in_addr, int (*create)(SOCKET, 
const struct sockaddr*, socklen_t)) { 
     SOCKADDR_IN addr; 
     SOCKET result_socket = GET_SOCKET; 
     if (SOCKET_NOK(result_socket)) 
       return SOCKET_INIT_ERROR; 
     memset(&addr, 0, sizeof(SOCKADDR_IN)); 
     addr.sin_family = AF_INET; 
     addr.sin_port = htons(port); 
     addr.sin_addr.s_addr = socket_in_addr; 
     if (CONNECT_NOK(create(result_socket, (SOCKADDR*)&addr, sizeof(SOCKADDR)))) 
       return SOCKET_INIT_ERROR; 
     return result_socket; 
} 

SOCKET get_client_socket(char* target, uint16_t port) { 
     return get_socket(port, inet_addr(target), &connect); 
/*  SOCKADDR_IN addr; 
     SOCKET client_socket = GET_SOCKET; 
     if (SOCKET_NOK(client_socket)) 
       return SOCKET_INIT_ERROR; 
     memset(&addr,0,sizeof(SOCKADDR_IN)); 
     addr.sin_family = AF_INET; 
     addr.sin_port = htons(port); 
     addr.sin_addr.s_addr = inet_addr(target); 
     if (CONNECT_NOK(connect(client_socket, (SOCKADDR*)&addr, sizeof(SOCKADDR)))) 
       return SOCKET_INIT_ERROR; 
     return client_socket;*/ 
} 

SOCKET get_server_socket_addr(uint16_t port, in_addr_t socket_in_addr) { 
     return get_socket(port, socket_in_addr, &bind); 
/*  SOCKADDR_IN addr; 
     SOCKET server_socket = GET_SOCKET; 
     if (SOCKET_NOK(server_socket)) 
       return SOCKET_INIT_ERROR; 
     memset(&addr, 0, sizeof(SOCKADDR_IN)); 
     addr.sin_family = AF_INET; 
     addr.sin_port = htons(port); 
     addr.sin_addr.s_addr = socket_in_addr; 
     if (CONNECT_NOK(bind(server_socket, (SOCKADDR*)&addr, sizeof(SOCKADDR_IN)))) 
       return SOCKET_INIT_ERROR; 
     return server_socket;*/ 
} 

有沒有人的想法?

回答

0

可能調用了錯誤的約定。

在Windows上,大部分API函數被定義爲WINAPI,即__stdcall,也叫做PASCAL。即從左到右傳遞參數,堆棧由被調用者清理。

OTOH默認編譯調用約定是__cdecl

P.S.如果mingw打印出更詳細的錯誤信息,那就好了。

+0

好的,這工作。感謝:D – 2011-06-02 16:22:49

0

如果「連接」是一個功能,您應該將其作爲「連接」而不是「&連接」傳遞。該函數的名稱已經是它的指針。

+0

好吧,我試過這個,但它創建了相同的輸出。我的本地gcc編譯它,但不是。 – 2011-06-02 16:16:02