2013-08-17 165 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)。因此你得到一个隐含的声明,并且由于编译器知道这些函数是标准的内置函数,所以在警告中也提到了这一点。