13

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?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
subhash kumar singh
  • 2,716
  • 8
  • 31
  • 43
  • 3
    Never heard of `err_sys` (so I've no idea how `getsockopt.cpp` compiles). Use `perror` or `strerror` instead. Also, you really need to start accepting some previous answers please. It's not hard! – Lightness Races in Orbit Aug 14 '11 at 18:28
  • @Tomalak: It probably compiles because warnings aren't turned up high enough, and the C standard allows you to not have prototypes declared for functions. – Oliver Charlesworth Aug 14 '11 at 18:36
  • @Oli: ORLY? I don't use C, so I didn't know. That's horrid! (Which reminds me... I did of course mean `getsockopt.c` above) – Lightness Races in Orbit Aug 14 '11 at 18:37
  • @Tomalak: Indeed! It is grim. See the accepted answer here, for instance: http://stackoverflow.com/questions/2575153/must-declare-function-prototype-in-c. – Oliver Charlesworth Aug 14 '11 at 18:38

7 Answers7

27

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

Patrik
  • 2,695
  • 1
  • 21
  • 36
7

Is this from TCP/IP Illustrated? I remember seeing this and the definition is provided in the appendix:

#include <errno.h>
#include <stdarg.h>
/*
 * Print a message and return to caller.
 * Caller specifies "errnoflag".
 */
static void
err_doit(int errnoflag, int error, const char *fmt, va_list ap)
{
    char    buf[MAXLINE];
    vsnprintf(buf, MAXLINE, fmt, ap);
    if (errnoflag)
        snprintf(buf+strlen(buf), MAXLINE-strlen(buf), ": %s",
    strerror(error));
    strcat(buf, "\n");
    fflush(stdout);     /* in case stdout and stderr are the same */
    fputs(buf, stderr);
    fflush(NULL);       /* flushes all stdio output streams */
}


/*
 * Fatal error related to a system call.
 * Print a message and terminate.
 */
void
err_sys(const char *fmt, ...)
{
    va_list     ap;
    va_start(ap, fmt);
    err_doit(1, errno, fmt, ap);
    va_end(ap);
    exit(1);
}
Foo Bah
  • 25,660
  • 5
  • 55
  • 79
3

err_sys() is a function used in several books written by W. Richard Stevens. This function is used to print what type of error occurred.

The function is used in programs in the texts with a custom header file "ourhdr.h" (or something else). Check the appendix for the header listing or the function definition.

Frozen Crayon
  • 5,172
  • 8
  • 36
  • 71
2

The source for this function (from Advanced Programming in the UNIX® Environment, by W. Richard Stevens) can be found on the book's website: http://www.apuebook.com/.

Annie B
  • 637
  • 1
  • 9
  • 14
1

Download the source code folder of the book you will find it in: http://www.unpbook.com/src.html

Open the source code folder and choose the lib folder. Next open the file called error.c. You will find the sys_err() defined in it.

Mahmoud Abdel-Rahman
  • 497
  • 2
  • 10
  • 27
1

As others have said, this code looks like examples from Stevens: Unix Network Programming. If you have the book, look in appendix D: miscellaneous source code for the text book definition (as well as the unp.h header that is used pervasively in examples). The file is too much to type into a SO answer, but a quick internet search for Stevens "standard error functions" returns a lot of google books results with implementations.

Jeremy
  • 1,397
  • 2
  • 13
  • 20
1

You have to specify lib file which contains implementation for this function. That is common problem when compiling c code.

michael nesterenko
  • 14,222
  • 25
  • 114
  • 182