How to fix C error ‘RTLD_NEXT undeclared’

Problem:

You have C code like

dlsym(RTLD_NEXT, 'myfunc');

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

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

Solution:

Add

#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!