Undefined reference to 'dlsym'

evinje picture evinje · Dec 4, 2013 · Viewed 46.7k times · Source

I have seen a lot of similar posts, but tried every trick in the book and am still struggling. Everything was working fine, but after installing/removing wireshark with some components/disselectors it all got messed up. I don't remember exactly which libraries/packages got uninstalled, but probably a lot more than I noticed.

If I create a simple main.cpp file like this one:

#include <SQLAPI.h>
int main()
{
  SAConnection con;
  return 0;
}

and try

g++ main.cpp -lsqlapi -ldl

it gives me the following error messages:

/usr/local/lib/libsqlapi.so: undefined reference to `dlsym'
/usr/local/lib/libsqlapi.so: undefined reference to `dlerror'
/usr/local/lib/libsqlapi.so: undefined reference to `dlopen'
/usr/local/lib/libsqlapi.so: undefined reference to `dlclose'
collect2: error: ld returned 1 exit status

I have tried to put -ldl before -lsqlapi as some have suggested that the order is important. If I use gcc instead of g++ the error is:

/usr/bin/ld: /tmp/ccwBI4tj.o: undefined reference to symbol '__gxx_personality_v0@@CXXABI_1.3'
/usr/lib/x86_64-linux-gnu/libstdc++.so.6: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status

I am able to compile and run the file if SAConnection is removed.

I don't think it has anything to do with SQLAPI, because I experience similar problems with libboost. I don't have a small code example, but when I compile a project that was successfully compiled last week, I get the error:

/usr/bin/ld: debug/components/helloworld/HelloWorld.o: undefined reference to symbol '_ZN5boost6system15system_categoryEv'
/usr/lib/x86_64-linux-gnu/libboost_system.so.1.53.0: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status

This project is using a Makefile that has been unchanged, so it has to be something on my system that is not correct. I have tried to reinstall build-essential.

Using Ubuntu 64 bit 13.10 with g++ version 4.8.1.

Answer

evinje picture evinje · Dec 6, 2013

I have found the solution; setting the -Wl,--no-as-needed before -ldl The new compile command is

gcc main.cpp -lsqlapi -lstdc++ -Wl,--no-as-needed -ldl

Apparently it has something to do with recent versions of gcc/ld default to linking with --as-needed.