Search code examples
clinuxgccdynamic-linkingc-standard-library

Linux - Feature Test Macros and Dynamic Linking


The C standard library includes a method, strerror_r (https://linux.die.net/man/3/strerror_r).

Depending on the "feature test macros" defined at compilation time, and compiling vs the GNU standard headers, one of two definitions gets included:

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

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

The XSI-compliant version of strerror_r() is provided if: (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && ! _GNU_SOURCE Otherwise, the GNU-specific version is provided.

Assuming I'm dynamically linking my application vs. the standard library, how does the linker correctly link vs. the proper definition of the function?


Solution

  • One of them is actually called __xpg_strerror_r and is redirected to be used as strerror_r if needed, see: https://code.woboq.org/userspace/glibc/string/string.h.html#409