如何修复 C 错误 'RTLD_NEXT undeclared'

问题:

你有类似这样的 C 代码

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

但当你尝试编译它时,你看到类似这样的错误消息

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

解决方案

添加

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

在错误发生的源文件顶部

为了声明 RTLD_NEXT#define _GNU_SOURCE 必须出现在第一个 #include 语句之前!


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