2013-08-17 162 views
0

我嘗試編譯Cygwin中GCC一個C套接字程序,但是當我編譯客戶端程序,它給了我下面的錯誤Cygwin的GCC編譯錯誤

client.h: In function ‘error’: 
client.h:11:5: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default] 
    exit(1); 
    ^
client.h: In function ‘main’: 
client.h:30:9: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default] 
     exit(0); 
     ^
client.h:36:5: warning: passing argument 2 of ‘connect’ from incompatible pointer type [enabled by default] 
    if(connect(sockfd,&serv_addr,sizeof(serv_addr)) < 0) 
    ^
In file included from client.h:3:0: 
/usr/include/sys/socket.h:28:7: note: expected ‘const struct sockaddr *’ but argument is of type ‘struct sockaddr_in *’ 
    int connect (int, const struct sockaddr *, socklen_t); 
    ^

當我試圖編譯服務器程序它給我下面的錯誤

server.h: In function ‘error’: 
server.h:8:5: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default] 
    exit(1); 
    ^
server.h: In function ‘main’: 
server.h:18:9: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default] 
     exit(1); 
     ^
server.h:23:5: warning: incompatible implicit declaration of built-in function ‘bzero’ [enabled by default] 
    bzero((char *) &serv_addr, sizeof(serv_addr)); 
    ^
server.h:32:64: error: ‘client’ undeclared (first use in this function) 
    newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &client); 
                   ^
server.h:32:64: note: each undeclared identifier is reported only once for each function it appears in 

那麼什麼是解決這個

+2

對於初學者,不要將代碼放在頭文件中。只是函數聲明。 .c文件是實際應用的地方。這可能是問題的一部分。它看到頭文件中使用了「exit()」。此外,還要展示一個最低限度的例子(即向我們展示您的代碼),瞭解哪些功能無法使用 – selbie

+1

你可以在這裏找到我的代碼http://stackoverflow.com/questions/17971513/socket-program-in-c-cannot-compile – mega6382

+0

你是否包含'stdlib.h'?然後你可以看到退出已經被聲明,你不應該再次聲明它。它是一個不應該重新定義的函數構建。 – hetepeperfan

回答

0

那麼問題是,我試圖在Windows中編譯Linux程序。

3

在客戶端代碼中的錯誤是因爲你傳遞一個指針struct sockaddr_in到PARAMET呃,它期望它是一個指向struct sockaddr。錯誤消息基本上說明了這一切。您的服務器代碼中的錯誤是因爲變量client沒有在任何地方聲明。

的警告是由不包括適當引起包括含有的exit的聲明文件(包括stdlib.h)和bzero(包括strings.h)。因此你得到一個隱含的聲明,並且由於編譯器知道這些函數是標準的內置函數,所以在警告中也提到了這一點。