1

I tried to use -rdynamic option in my CMakeLists.txt file, like this:

cmake_minimum_required(VERSION 3.5)
project(Tmuduo CXX)
...
set(CMAKE_CXX_STANDARD 11)
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)

if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
    add_compile_options(-Wthread-safety )
endif()

add_compile_options(
 # -DVALGRIND
 -Wall
 -Wno-unused-parameter
 -Woverloaded-virtual
 -Wpointer-arith
 -Wwrite-strings
 -O3
 -rdynamic
 )
...

When I use cmake .. -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_C_COMPILER=clang and make VERBOSE=1, I got some message as follow: enter image description here

Just as you can see, the -rdynamic compile option did appear in the clang++ command and the compiler also complainted that the argument was unused. But when I used the command below, something strange happended.

clang++ -I/home/phoenix/MyProject/Tmuduo -g -Wthread-safety -Wall -Wno-unused-parameter -Woverloaded-virtual -Wpointer-arith -Wwrite-strings -rdynamic -std=gnu++11 test/Exception_test.cc base/Exception.cc base/CurrentThread.cc -o exception_test -O3

Everything goes well. This time, the -rdynamic option works. That reall make me confuse. Can anyone tell me what's going on here? Why the clang++ command works while the cmake failed?

Phoenix Chao
  • 390
  • 3
  • 19

1 Answers1

5

Because -rdynamic is a linker option, so if you use when compiling a source file into an object *.o it does nothing, there is no link phase here.

When linking all the *.o and libraries into the finally executable, then it is actually used.

From man gcc (but clang uses the same):

        -rdynamic
           Pass the flag -export-dynamic to the ELF linker, on targets that support it.
           This instructs the linker to add all symbols, not only used ones, to the
           dynamic symbol table. This option is needed for some uses of "dlopen" or to
           allow obtaining backtraces from within a program.
rodrigo
  • 94,151
  • 12
  • 143
  • 190