C++ linker flags in CMake

Viewed 3467

I'm compiling a C++ program and have been getting the following error message:

undefined reference to 'pthread_mutexattr_init'
undefined reference to 'dlopen'
undefined reference to 'dlerror'
undefined reference to 'dlsym'
undefined reference to 'dlclose'

To resolve the error for pthread I added the following linker flag to my CMakeLists.txt.

if (UNIX)
     set(CMAKE_CXX_FLAGS "-pthread")
endif (UNIX)

This resolved the pthread error. To resolve the libdl error I went ahead and modified it to the following.

if (UNIX)
     set(CMAKE_CXX_FLAGS "-pthread -dl")
endif (UNIX)

This gave me a warning

unrecognized gcc debugging option: l

I modified it to the following

if (UNIX) 
     set(CMAKE_CXX_FLAGS "-pthread")
     set(CMAKE_CXX_FLAGS "-dl")
endif (UNIX)

And got back all the error messages together with

unrecognized gcc debugging option: l.

Do I miss how to set linker flags in CMake? What exactly am I doing wrong? I'm on Ubuntu 17.04 x64.

2 Answers
Related