Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unknown reference to __dlopen in dlopen

dlopen is located in libdl.a but when I link my application against libdl.a , gcc linker throw this error : unknow reference to __dlopen called in dlopen

Should I import another .a?

like image 412
Guillaume Paris Avatar asked Sep 23 '11 08:09

Guillaume Paris


People also ask

What is Dlopen in C++?

dlopen() The function dlopen() loads the dynamic library file named by the null-terminated string filename and returns an opaque "handle" for the dynamic library. If filename is NULL, then the returned handle is for the main program.

How do you fix undefined references to Main?

To fix this error, correct the spelling of the main() function.

What is undefined reference?

An “Undefined Reference” error occurs when we have a reference to object name (class, function, variable, etc.) in our program and the linker cannot find its definition when it tries to search for it in all the linked object files and libraries.

What is Dlsym in C?

DESCRIPTION. The dlsym() function shall obtain the address of a symbol defined within an object made accessible through a dlopen() call. The handle argument is the value returned from a call to dlopen() (and which has not since been released via a call to dlclose()), and name is the symbol's name as a character string.


1 Answers

When I try to compile statically a dlopen mockup program, gcc (Archlinux/gcc version 4.6.1 20110819 (prerelease)) tells me:

$ gcc test.c  -ldl -static  
/tmp/ccsWe4RN.o: In function `main': test.c:(.text+0x13): 
warning: Using 'dlopen' in statically linked applications requires 
at runtime the shared libraries from the glibc version used for linking

and indeed, when I ran this script in /usr/lib/

for i in *.a; 
do 
    echo $i; 
    readelf -a $i | grep __dlopen;
done

I saw:

libc.a
16: 0000000000000080    69 FUNC    GLOBAL DEFAULT    1 __dlopen
000000000000  002300000001 R_X86_64_64       0000000000000000 __dlopen + 0
35: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND __dlopen

so as per the first line, libc.a does define your missing symbole.

In my system, gcc test.c -ldl -static is enough to make the application run, but

gcc <file> -static -ldl -lc

should fix the problem in your system.

like image 92
Kevin Avatar answered Sep 18 '22 17:09

Kevin