【问题标题】:clang: warning: argument unused during compilation: '-rdynamic'叮当声:警告:编译期间未使用的参数:'-rdynamic'
【发布时间】:2021-02-03 12:27:06
【问题描述】:

我尝试在我的 CMakeLists.txt 文件中使用 -rdynamic 选项,如下所示:

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
 )
...

当我使用cmake .. -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_C_COMPILER=clangmake VERBOSE=1 时,我收到如下消息:

正如您所见,-rdynamic 编译选项确实出现在 clang++ 命令中,编译器还抱怨该参数未被使用。但是当我使用下面的命令时,发生了一些奇怪的事情。

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

一切顺利。这一次,-rdynamic 选项有效。这真的让我很困惑。谁能告诉我这里发生了什么?为什么cmake失败时clang++命令有效?

【问题讨论】:

  • -rdynamic 是一个链接器选项,在add_link_option 中使用它。
  • 好的,谢谢您的回答。我将我的 cmake 版本从 3.8 升级到 3.18 并使用 add_link_options(-rdynamic) 来解决我的问题。 ????

标签: c++ cmake clang compiler-warnings


【解决方案1】:

因为-rdynamic是一个链接器选项,所以如果你在将源文件编译成对象时使用*.o它什么都不做,这里没有链接阶段。

当将所有*.o 和库链接到finally 可执行文件中时,才会实际使用它。

来自man gcc(但clang使用相同):

        -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.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-25
    • 2017-02-13
    • 1970-01-01
    • 1970-01-01
    • 2015-08-19
    • 1970-01-01
    相关资源
    最近更新 更多