How to fix C error 'RTLD_NEXT undeclared'

Problem:

You have C code like

dlsym_example.c
dlsym(RTLD_NEXT, 'myfunc');

but when you try to compile it you see an error message like

dlsym_error.txt
main.c:3:11: error: ‘RTLD_NEXT’ undeclared (first use in this function)
     dlsym(RTLD_NEXT, 'myfunc');
           ^~~~~~~~~

Solution

Add

fix_rtld_next.c
#define _GNU_SOURCE
#include <dlfcn.h>

at the top of the source file where the error occured.

In order for RTLD_NEXT to be declared, #define _GNU_SOURCE must occur before the first #include statement!


Check out similar posts by category: C/C++, GCC Errors