【问题标题】:Apply C-specific gcc options to C/C++ mixed library将 C 特定的 gcc 选项应用于 C/C++ 混合库
【发布时间】:2021-04-01 11:25:43
【问题描述】:

我有一个库,其中包含一个围绕自动生成的 C 代码的 C++ 包装器,使用 CMake 和 gcc 构建。当我编译它时,我会收到这些我想禁止的警告:

src/ssp/autogenerated.c: In function ‘x1111’:
src/ssp/autogenerated.c:185:1: warning: implicit declaration of function ‘x1111’; did you mean ‘x1110’? [-Wimplicit-function-declaration]
  185 | x1111();
      | ^~~~~~~~~~

我应该能够使用-Wno-implicit-function-declaration warning option 来禁止这些警告。我将它添加到我的 CMakeLists.txt 中,如下所示:

file(GLOB SRCS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} src/ssp/*.c src/ssp/*.cpp)
add_library(mylib SHARED ${SRCS})
target_compile_options(mylib PRIVATE "-Wno-implicit-function-declaration")

但是,那里仍然有一个 *.cpp 源,所以编译给了我:

cc1plus: error: command-line option ‘-Wno-implicit-function-declaration’ is valid for C/ObjC but not for C++ [-Werror]
cc1plus: all warnings being treated as errors

有没有办法抑制这些警告?我在想可能只将-Wno-implicit-function-declaration 应用于C 源代码或让g++ 忽略它?

【问题讨论】:

    标签: gcc cmake g++


    【解决方案1】:

    仅将选项应用于 C 源代码。见cmake generator expressions:

    target_compile_options(mylib PRIVATE 
         $<$<COMPILE_LANGUAGE:C>:-Wno-implicit-function-declaration>
    )
    

    【讨论】:

    • 是的,这行得通!我不得不将$&lt;LANGUAGE:C&gt; 更改为$&lt;COMPILE_LANGUAGE:C&gt;,但这正是我所需要的。
    猜你喜欢
    • 2010-09-07
    • 1970-01-01
    • 2021-06-09
    • 1970-01-01
    • 2017-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-06
    相关资源
    最近更新 更多