简体   繁体   中英

Cygwin gcc compiling error

I was trying to compile a C socket program in CYGWIN gcc but when I compile the client program it gives me the following error

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);
       ^

And when I tries to compile the server program it gives me the following error

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

So what is the solution to this

The error in your client code is because you pass a pointer to a struct sockaddr_in to a parameter that expects it to be a pointer to a struct sockaddr . The error message basically says it all. The error in your server code is because the variable client is not declared anywhere.

The warnings are caused by not including the appropriate include files containing the declarations of exit (include stdlib.h ) and bzero (include strings.h ). You thus get an implicit declaration and since the compiler knows these functions as standardly built-in functions it mentions that in the warning as well.

好了,问题是我试图在Windows中编译Linux程序。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM