我正在使用以下 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
if the problem is caused by the different compilers, how can I adjust my CMakeLists.txt in order to make the library portable and work on machines using different compilers? (or at least clang and g which are the most common). Is there some CMake trick to abstract away the compiler and achieve the same results without having to specify the literal flags needed?
uj5u.com熱心網友回復:
由于 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()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/371777.html
標籤:c linux macos cmake compiler-errors
