由于 "no option -Wunused-command-line-argument" 错误,CMakeLists.txt 生成的 makefile 适用于 MacO,但不适用于 linux

CMakeLists.txt's generated makefile works on MacOs but not on linux due to "no option -Wunused-command-line-argument" error

我正在使用以下 CMakeLists.txt 生成 Makefile 来编译我正在编写的库:

cmake_minimum_required(VERSION 3.10)

# set the project name and version
project(PCA    VERSION 0.1
               DESCRIPTION "framework for building Cellular Automata"
               LANGUAGES CXX)

# specify the C++ standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)

find_package(OpenMP REQUIRED)


# compile options
if (MSVC)
    # warning level 4 and all warnings as errors
    add_compile_options(/W4 /WX)
    # speed optimization
    add_compile_options(/Ox)
    # if the compiler supports OpenMP, use the right flags
    if (${OPENMP_FOUND})
        add_compile_options(${OpenMP_CXX_FLAGS})
    endif()
else()
    # lots of warnings and all warnings as errors
    add_compile_options(-Wall -Wextra -pedantic -Werror -Wno-error=unused-command-line-argument) # Here may be the problem
    add_compile_options(-g -O3)
    # if the compiler supports OpenMP, use the right flags
    if (${OPENMP_FOUND})
        add_compile_options(${OpenMP_CXX_FLAGS})
    endif()
endif()

add_library(parallelcellularautomata STATIC <all the needed .cpp and .hpp files here> )
target_include_directories(parallelcellularautomata PUBLIC include)

此 CMakeFile 在 MacOS 上运行良好,实际上使用以下命令

mkdir build
cd build
cmake ..
make

我得到我的图书馆没有错误也没有警告。

当我尝试在 Ubuntu 上编译项目时,由于以下错误编译失败:

cc1plus: error: ‘-Werror=unused-command-line-argument’: no option -Wunused-command-line-argument
make[2]: *** [CMakeFiles/bench_omp_automaton.dir/build.make:63: CMakeFiles/bench_omp_automaton.dir/bench_omp_automaton.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:78: CMakeFiles/bench_omp_automaton.dir/all] Error 2
make: *** [Makefile:84: all] Error 2

在编译选项部分的else分支中可以看到,我使用了标志 -Werror 所以每个警告都被视为错误,但我想从导致错误的警告中排除未使用的命令行参数,因为库的某些部分使用 OpenMP(并将使用一些命令行参数) 而其他人则没有。

我想避免的解决方案

我想到但不喜欢的一个解决方案是删除 -Werror 并因此删除 -Wno-error=unused-command-line-argument.

关于如何解决这个问题有什么建议吗?

一些 google 搜索

我已经尝试过谷歌搜索:

cc1plus: error: ‘-Werror=unused-command-line-argument’: no option -Wunused-command-line-argument

但找不到任何针对我的情况的具体内容,只有 github 问题涉及其他错误。虽然阅读它们,但在某些情况下,问题是编译器不支持该特定选项。

在 Ubuntu 上,编译器是: c++ (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0 而在 MacOs 上是:

Homebrew clang version 12.0.1
Target: x86_64-apple-darwin19.3.0
Thread model: posix
InstalledDir: /usr/local/opt/llvm/bin

如果问题是由不同的编译器引起的,我该如何调整我的 CMakeLists.txt 以使库可移植并在使用不同编译器的机器上工作? (或者至少是最常见的 clang++ 和 g++)。 是否有一些 CMake 技巧可以抽象出编译器并获得相同的结果而无需指定所需的文字标志?

由于Ubuntu使用gcc,它似乎不支持未使用的命令行参数警告:https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html

所以你应该更新你的 CMakeLists.txt 为:

if (NOT CMAKE_CXX_COMPILER_ID MATCHES "GNU")
  add_compile_options(-Wno-error=unused-command-line-argument)
endif()