Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined reference to 'dlsym' and 'dlopen'

I am compiling using arm-linux-gnueabi-g++ version 4.7.3.

I have the arm-linux-gnueabi libraries installed at location:

/usr/arm-linux-gnueabi/lib, it contains libdl.a, libdl.so, libdl.so.2, and libdl-2.19.so.

libdl.so links to libdl.so.2 which links to libdl-2.19.so.

I am trying to link against the dl library (see command string below), but I always get the undefined reference errors.

arm-linux-gnueabi-g++ -I. -I../ -I../Comms/Linux  -Wall -DLINUX -fpic -o ../../work/MyProgram main.o
-L../../work -L/usr/arm-linux-gnueabi/lib -lComms -lConsole -lUtilities -ldl
../../work/libUtilities.so: undefined reference to `dlsym'
../../work/libUtilities.so: undefined reference to `dlopen'
collect2: error: ld returned 1 exit status

If I compile using g++ 4.8.2 using the following commend then my program compiles, links, and executes fine.

g++ -I. -I../ -I../Comms/Linux  -Wall -DLINUX -fpic -o ../../work/MyProgram main.o
-L../../work -lComms -lConsole -lUtilities -ldl

Obviously it can't find the libdl.so library; I thought that by adding the path to the location of the appropriate library by using the -L flag would fix the problem, but it didn't.

What am I missing with the ARM compiler command?

like image 706
Fred Avatar asked Dec 22 '14 17:12

Fred


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.

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.

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.


1 Answers

Well, I found the answer, I needed -Wl,--no-as-needed flag before the -ldl. I had run across this flag before I asked the question, but apparently mistyped it because it hadn't worked for me.

I don't understand why the flag is needed, but the code does finish linking now.

A SO user here says that it has to do with recent (2013 as of the user's post) versions of gcc linking to --as-needed.

like image 198
Fred Avatar answered Oct 17 '22 21:10

Fred