Linux c++ error: undefined reference to 'dlopen'
this doesn't work:
gcc -ldl dlopentest.c
But this does:
gcc dlopentest.c -ldl
That's one annoying "feature" for sure
I was struggling with it when writing heredoc syntax and found some interesting facts. With CC=Clang
, this works:
$CC -ldl -x c -o app.exe - << EOF#include <dlfcn.h>#include <stdio.h>int main(void){ if(dlopen("libc.so.6", RTLD_LAZY | RTLD_GLOBAL)) printf("libc.so.6 loading succeeded\n"); else printf("libc.so.6 loading failed\n"); return 0;}EOF./app.exe
as well as all of these:
$CC -ldl -x c -o app.exe - << EOF
$CC -x c -ldl -o app.exe - << EOF
$CC -x c -o app.exe -ldl - << EOF
$CC -x c -o app.exe - -ldl << EOF
However, with CC=gcc
, only the last variant works; -ldl
after -
(the stdin argument symbol).