Am trying to build xinetd on my solaris system am using sun workshop c++/c to compile the code but I keep getting the following errors

error: `sys_nerr` underclared (first use in this function)
error: (each undeclared identifier is reported only once
error: for each function is appears in.)
error: `sys_errlist undeclared (first use in this function)

here the full code.

#ifndef lint
static const char rcsid[] =
  "$FreeBSD: src/lib/libc/string/strerror.c,v 1.3 2001/05/24 08:47:41 obrien Exp $";

#include <stdio.h>
#include <string.h>

char *strerror(num) int num;
#define UPREFIX "Unknown error: "
    static char ebuf[40] = UPREFIX;     /* 64-bit number + slop */
    register unsigned int errnum;
    register char *p, *t;
    char tmp[40];

    errnum = num;               /* convert to unsigned */

    if (errnum < sys_nerr)
        return ((char *)sys_errlist[errnum]);

    /* Do this by hand, so we don't link to stdio(3). */
    t = tmp;
    if (num < 0)
        errnum = -errnum;
    do {
        *t++ = "0123456789"[errnum % 10];
    } while (errnum /= 10);
    if (num < 0)
        *t++ = '-';
    for (p = ebuf + sizeof(UPREFIX) - 1;;) {
        *p++ = *--t;
        if (t <= tmp)
    *p = '\0';
    return (ebuf);
#endif /* HAVE_STRERROR */

Thanks alot :)

2 Years
Discussion Span
Last Post by NathanOliver

At line 19 and 20 you are using the symbols sys_nerr and sys_errlist but this is the first time the compiler has seen them, you have not previously declared them anywhere hence they are "undeclared".

You may have missed out a header file in which they are declared or you may have failed to write their declarations.


The use of sys_nerr and sys_errlist is deprecated, in favor of the strerror function. The code you posted (which is not complete) appears to attempt to provide a version of strerror function that relies on sys_nerr and sys_errlist for systems that do not provide the more modern strerror function. This fall-back version of strerror should be enabled only if your system does not provide it (which is what the HAVE_STRERROR macro is used to mark). So, you should first make sure that that macro is correctly set for your system, i.e., test whether you have the strerror function or not on your system.

If you don't, then you can update your version of libc (standard C library), and probably the compiler too. This function is a standard C functions since C89, and a standard POSIX function since 1992. Your system should have it, or should be able to be updated to make it have it.

Otherwise, you'll have to replace those lines of code with something else that either (1) achieves the same behavior without using sys_nerr and sys_errlist, or (2) returns a dummy null-string in all cases (not really a good thing, but it might be the only option). To replace sys_nerr and sys_errlist, you will have to look into Solaris documentation, because at this point, you are beyond any established standard (the C / C++ standard is the strerror function; one layer deeper, the POSIX standard is strerror or the older deprecated sys_nerr / sys_errlist; and one layer deeper, there are no more standards, it's just whatever you can dig up in the documentation of your operating system's API).


Banfa is correct that you might simply be missing a header. In this case, for sys_nerr and sys_errlist, you should have the #include <errno.h> inclusion somewhere before their first use. Most systems probably have errno.h included by other headers, especially string.h and stdio.h, but it is possible that Solaris' libraries don't have that, which would explain why this code works most of the time, except for arcane systems like Solaris.

Edited by mike_2000_17: note


Can I ask what char *strerror(num) int num; means?

I had never seen this before either. Since the OP did not report an error besides the sys_nerr-related stuff, I assumed that this line was some sort of very archaic form of C. This was a good guess since this code was clearly written when "strerror" was something new and not widely supported, which means that it's from the 80s or early 90s at best, meaning that this code is probably written in "K&R C", i.e., pre-standard C (1978-1989). As it so happens, I dug around a bit, and it appears that this is indeed the old school K&R C syntax for declaring functions. Even though this is no longer standard (AFAIK), even in C, compilers support it for legacy reasons, in C, but not in C++ (at least, I tested on GCC, and that's how it is, it accepts that code in C but not in C++).

I guess this is what one could call code archeology: digging up some ancient code and re-discovering the bizarre practices of our ancestors.

Votes + Comments
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.