subh. singh subh. singh - 3 months ago 16
C Question

Where is function err_sys() defined?

I am getting an error related to

err_sys()
in this code:

#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main()
{
int sockfd;

if ((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
err_sys("can't create socket");

close(sockfd);
return 0;
}


I am getting the linker error:

/tmp/cciuUVhz.o: In function `main':
getsockopt.c:(.text+0x38): undefined reference to `err_sys'
collect2: ld returned 1 exit status


Where is the function
err_sys()
defined?

Answer

Place this on top of your code:

void err_sys(const char* x) 
{ 
    perror(x); 
    exit(1); 
}

perror is defined in stdio.h

err_sys is used in the book "UNIX Network Programming: The sockets networking API" by Richard Stevens. It's not something common, as far as I know.

edit:fixed code error