Search code examples
posixgnuglibc

Different versions of function with the same name


From man 3 strerror:

int strerror_r(int errnum, char *buf, size_t buflen);
        /* XSI-compliant */

char *strerror_r(int errnum, char *buf, size_t buflen);
        /* GNU-specific */

...

This function is available in two versions: an XSI-compliant version specified in POSIX.1-2001 (available since glibc 2.3.4, but not POSIX-compliant until glibc 2.13), and a GNU-specific version (available since glibc 2.0). The XSI-compliant version is provided with the feature test macros settings shown in the SYNOPSIS; otherwise the GNU-specific version is provided.

Does glibc really provide both these functions? If so, how can the linker distinguish between them. When it comes to linking, all preprocessor defines are gone so what is going on here?


Solution

  • Take a look in <string.h>. When using the XSI-compliant version, it redefines strerror_r using a macro:

    #   define strerror_r __xpg_strerror_r
    

    There's no confusion in the linker, because xpg_strerror_r is a different function.