Search code examples
c++cmakelinux-mint

Linker error while building from source code


I am trying to build an application from source. I am able to configure it using 'cmake .'. However, when I run 'make' it gives me this:

Linking CXX executable ../../bin/lux64/Release/od_batch_launcher
../../bin/lux64/Release/libBasic.so: undefined reference to `dlopen'
../../bin/lux64/Release/libBasic.so: undefined reference to `dlclose'
../../bin/lux64/Release/libBasic.so: undefined reference to `dlerror'
../../bin/lux64/Release/libBasic.so: undefined reference to `dlsym'
../../bin/lux64/Release/libBasic.so: undefined reference to `pthread_sigmask'
collect2: error: ld returned 1 exit status
make[2]: *** [bin/lux64/Release/od_batch_launcher] Error 1
make[1]: *** [src/Basic/CMakeFiles/od_batch_launcher.dir/all] Error 2
make: *** [all] Error 2

I understand that its is unable to dynamically link to a c++ library. I don't quite know how to make the necessary changes to cmake. I am running gcc version: 4.9.2 on Linux Mint 17. I would be grateful for any help. Thank you!


Solution

  • Try passing -DCMAKE_EXE_LINKER_FLAGS=-ldl to the CMake executable. To change the CMake build scripts, add something like:

    target_link_libraries(target_name dl)
    

    where target_name is basically the executable name without any extensions (e.g. .exe).

    EDIT: Actually, I just reread you question, and I'm putting this in the wrong place. You really want:

    target_link_libraries(Basic dl)
    

    Apparently, there were also pthread-related errors, so you'd also have to add:

    target_compile_options(Basic PUBLIC -pthread)
    

    Both of these go in whichever file contains add_library(Basic) (usually CMakeLists.txt).

    EDIT 2: Instead of target_compile_options, try:

    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")