【发布时间】:2022-01-09 02:22:40
【问题描述】:
我正在使用以下 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。
关于如何解决此问题的任何建议?
一些谷歌搜索
我已经尝试过谷歌搜索:
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 技巧可以抽象出编译器并获得相同的结果,而无需指定所需的文字标志?
【问题讨论】:
-
我在this 问题上发现我可以(可能)将
#pragma clang diagnostic ignored "something here"添加到正确的源文件(我必须找到)但我不喜欢这个解决方案,我'更喜欢不需要更改源文件的文件。
标签: c++ linux macos cmake compiler-errors