简体   繁体   中英

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

I'm using the following CMakeLists.txt to generate the Makefile to compile a library I'm writing:

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)

This CMakeFile works well on MacOS , in fact with the following commands

mkdir build
cd build
cmake ..
make

I get my library without errors nor warnings.

When I try to compile the project on Ubuntu the compilation fails due to the following error:

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

As it can be seen in the else branch of the compile options section, I'm using the flag -Werror so each warning is treated as an error, but I want to exclude the unused-command line-argument from the warnings that cause an error, since some parts of the library use OpenMP (and will use some command line arguments) and others do not.

Solution I'd like to avoid

One solution that crossed my mind, but which I don't like, would be to remove the -Werror and consequently the -Wno-error=unused-command-line-argument .

Any suggestion on how to fix this problem?

Some google searches

I already tried googling:

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

but could not find anything specific for my case, only github issues referring to other errors. Reading them though, in some cases the problem was that the compilers didn't support that specific option.

On Ubuntu the compiler is: c++ (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0 while on MacOs it is:

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?

As Ubuntu uses gcc, it doesn't seem to support unused-command-line-argument warning: https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html

So you should update your CMakeLists.txt with:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM