簡體   English   中英

在 CMake 中啟用更多警告時出錯

[英]Error when enabling more warnings in CMake

我正在嘗試啟用更多警告並將警告視為項目中的錯誤。 我在我的 CMakeLists.txt 中加入了以下代碼:

target_compile_definitions(hyper PRIVATE
                           $<$<CXX_COMPILER_ID:MSVC>:/W4 /WX>
                           $<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wall -Wextra -Wpedantic -Werror>
                           )

我使用 Clang 14.0.6 或 GCC 12.1.1 在 Linux 上收到以下錯誤:

Cannot get compiler information:
    Compiler exited with error code 1: /usr/bin/c++ -xc++ -D-Wall -D-Werror -D-Wextra -D-Wpedantic -DFMT_LOCALE -DFMT_SHARED -DHL_DEBUG -DSPDLOG_COMPILED_LIB -DSPDLOG_FMT_EXTERNAL -DSPDLOG_SHARED_LIB -Dhyper_EXPORTS -g -fPIC -Wall -Wextra -std=gnu++20 -fpch-preprocess -v -dD -E
    Using built-in specs.
    COLLECT_GCC=/usr/bin/c++
    Target: x86_64-pc-linux-gnu
    Configured with: /build/gcc/src/gcc/configure --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++,d --enable-bootstrap --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --with-build-config=bootstrap-lto --with-linker-hash-style=gnu --with-system-zlib --enable-__cxa_atexit --enable-cet=auto --enable-checking=release --enable-clocale=gnu --enable-default-pie --enable-default-ssp --enable-gnu-indirect-function --enable-gnu-unique-object --enable-libstdcxx-backtrace --enable-link-serialization=1 --enable-linker-build-id --enable-lto --enable-multilib --enable-plugin --enable-shared --enable-threads=posix --disable-libssp --disable-libstdcxx-pch --disable-werror
    Thread model: posix
    Supported LTO compression algorithms: zlib zstd
    gcc version 12.1.1 20220730 (GCC) 
    COLLECT_GCC_OPTIONS='-D' '-Wall' '-D' '-Werror' '-D' '-Wextra' '-D' '-Wpedantic' '-D' 'FMT_LOCALE' '-D' 'FMT_SHARED' '-D' 'HL_DEBUG' '-D' 'SPDLOG_COMPILED_LIB' '-D' 'SPDLOG_FMT_EXTERNAL' '-D' 'SPDLOG_SHARED_LIB' '-D' 'hyper_EXPORTS' '-g' '-fPIC' '-Wall' '-Wextra' '-std=gnu++20' '-fpch-preprocess' '-v' '-dD' '-E' '-D' '___CIDR_DEFINITIONS_END' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
     /usr/lib/gcc/x86_64-pc-linux-gnu/12.1.1/cc1plus -E -quiet -v -D_GNU_SOURCE -D -Wall -D -Werror -D -Wextra -D -Wpedantic -D FMT_LOCALE -D FMT_SHARED -D HL_DEBUG -D SPDLOG_COMPILED_LIB -D SPDLOG_FMT_EXTERNAL -D SPDLOG_SHARED_LIB -D hyper_EXPORTS -D ___CIDR_DEFINITIONS_END /tmp/compiler-file9852660908641365533 -mtune=generic -march=x86-64 -std=gnu++20 -Wall -Wextra -fPIC -fpch-preprocess -g -fworking-directory -dD -dumpbase compiler-file9852660908641365533
    ignoring nonexistent directory "/usr/lib/gcc/x86_64-pc-linux-gnu/12.1.1/../../../../x86_64-pc-linux-gnu/include"
    #include "..." search starts here:
    #include <...> search starts here:
     /usr/lib/gcc/x86_64-pc-linux-gnu/12.1.1/../../../../include/c++/12.1.1
     /usr/lib/gcc/x86_64-pc-linux-gnu/12.1.1/../../../../include/c++/12.1.1/x86_64-pc-linux-gnu
     /usr/lib/gcc/x86_64-pc-linux-gnu/12.1.1/../../../../include/c++/12.1.1/backward
     /usr/lib/gcc/x86_64-pc-linux-gnu/12.1.1/include
     /usr/local/include
     /usr/lib/gcc/x86_64-pc-linux-gnu/12.1.1/include-fixed
     /usr/include
    End of search list.
    <command-line>: error: macro names must be identifiers
    <command-line>: error: macro names must be identifiers
    <command-line>: error: macro names must be identifiers
    <command-line>: error: macro names must be identifiers
    
    
    Compiler exited with error code 1: /usr/bin/c++ -xc++ -D-Wall -D-Werror -D-Wextra -D-Wpedantic -DFMT_LOCALE -DFMT_SHARED -DHL_DEBUG -DSPDLOG_COMPILED_LIB -DSPDLOG_FMT_EXTERNAL -DSPDLOG_SHARED_LIB -Dhyper_EXPORTS -I/home/developer/projects/hyper_engine/hyper/include -I/home/developer/projects/hyper_engine/hyper/src -I/home/developer/projects/hyper_engine/hyper/src/base -g -fPIC -Wall -Wextra -std=gnu++20 -Winvalid-pch -x c++-header -include /home/developer/projects/hyper_engine/cmake-build-debug/hyper/CMakeFiles/hyper.dir/cmake_pch.hxx -fpch-preprocess -v -dD -E
    Using built-in specs.
    COLLECT_GCC=/usr/bin/c++
    Target: x86_64-pc-linux-gnu
    Configured with: /build/gcc/src/gcc/configure --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++,d --enable-bootstrap --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --with-build-config=bootstrap-lto --with-linker-hash-style=gnu --with-system-zlib --enable-__cxa_atexit --enable-cet=auto --enable-checking=release --enable-clocale=gnu --enable-default-pie --enable-default-ssp --enable-gnu-indirect-function --enable-gnu-unique-object --enable-libstdcxx-backtrace --enable-link-serialization=1 --enable-linker-build-id --enable-lto --enable-multilib --enable-plugin --enable-shared --enable-threads=posix --disable-libssp --disable-libstdcxx-pch --disable-werror
    Thread model: posix
    Supported LTO compression algorithms: zlib zstd
    gcc version 12.1.1 20220730 (GCC) 
    COLLECT_GCC_OPTIONS='-D' '-Wall' '-D' '-Werror' '-D' '-Wextra' '-D' '-Wpedantic' '-D' 'FMT_LOCALE' '-D' 'FMT_SHARED' '-D' 'HL_DEBUG' '-D' 'SPDLOG_COMPILED_LIB' '-D' 'SPDLOG_FMT_EXTERNAL' '-D' 'SPDLOG_SHARED_LIB' '-D' 'hyper_EXPORTS' '-I' '/home/developer/projects/hyper_engine/hyper/include' '-I' '/home/developer/projects/hyper_engine/hyper/src' '-I' '/home/developer/projects/hyper_engine/hyper/src/base' '-g' '-fPIC' '-Wall' '-Wextra' '-std=gnu++20' '-Winvalid-pch' '-include' '/home/developer/projects/hyper_engine/cmake-build-debug/hyper/CMakeFiles/hyper.dir/cmake_pch.hxx' '-fpch-preprocess' '-v' '-dD' '-E' '-D' '___CIDR_DEFINITIONS_END' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
     /usr/lib/gcc/x86_64-pc-linux-gnu/12.1.1/cc1plus -E -quiet -v @/tmp/ccT7j88G -D_GNU_SOURCE -D -Wall -D -Werror -D -Wextra -D -Wpedantic -D FMT_LOCALE -D FMT_SHARED -D HL_DEBUG -D SPDLOG_COMPILED_LIB -D SPDLOG_FMT_EXTERNAL -D SPDLOG_SHARED_LIB -D hyper_EXPORTS -D ___CIDR_DEFINITIONS_END -include /home/developer/projects/hyper_engine/cmake-build-debug/hyper/CMakeFiles/hyper.dir/cmake_pch.hxx /tmp/compiler-file9852660908641365533 -mtune=generic -march=x86-64 -std=gnu++20 -Wall -Wextra -Winvalid-pch -fPIC -fpch-preprocess -g -fworking-directory -dD -dumpbase compiler-file9852660908641365533
    ignoring nonexistent directory "/usr/lib/gcc/x86_64-pc-linux-gnu/12.1.1/../../../../x86_64-pc-linux-gnu/include"
    #include "..." search starts here:
    #include <...> search starts here:
     /home/developer/projects/hyper_engine/hyper/include
     /home/developer/projects/hyper_engine/hyper/src
     /home/developer/projects/hyper_engine/hyper/src/base
     /usr/lib/gcc/x86_64-pc-linux-gnu/12.1.1/../../../../include/c++/12.1.1
     /usr/lib/gcc/x86_64-pc-linux-gnu/12.1.1/../../../../include/c++/12.1.1/x86_64-pc-linux-gnu
     /usr/lib/gcc/x86_64-pc-linux-gnu/12.1.1/../../../../include/c++/12.1.1/backward
     /usr/lib/gcc/x86_64-pc-linux-gnu/12.1.1/include
     /usr/local/include
     /usr/lib/gcc/x86_64-pc-linux-gnu/12.1.1/include-fixed
     /usr/include
    End of search list.
    <command-line>: error: macro names must be identifiers
    <command-line>: error: macro names must be identifiers
    <command-line>: error: macro names must be identifiers
    <command-line>: error: macro names must be identifiers
    
    
    Compiler exited with error code 1: /usr/bin/c++ -xc++ -D-Wall -D-Werror -D-Wextra -D-Wpedantic -DFMT_LOCALE -DFMT_SHARED -DHL_DEBUG -DSPDLOG_COMPILED_LIB -DSPDLOG_FMT_EXTERNAL -DSPDLOG_SHARED_LIB -Dhyper_EXPORTS -I/home/developer/projects/hyper_engine/hyper/include -I/home/developer/projects/hyper_engine/hyper/src -I/home/developer/projects/hyper_engine/hyper/src/base -g -fPIC -Wall -Wextra -std=gnu++20 -Winvalid-pch -include /home/developer/projects/hyper_engine/cmake-build-debug/hyper/CMakeFiles/hyper.dir/cmake_pch.hxx -fpch-preprocess -v -dD -E
    Using built-in specs.
    COLLECT_GCC=/usr/bin/c++
    Target: x86_64-pc-linux-gnu
    Configured with: /build/gcc/src/gcc/configure --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++,d --enable-bootstrap --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --with-build-config=bootstrap-lto --with-linker-hash-style=gnu --with-system-zlib --enable-__cxa_atexit --enable-cet=auto --enable-checking=release --enable-clocale=gnu --enable-default-pie --enable-default-ssp --enable-gnu-indirect-function --enable-gnu-unique-object --enable-libstdcxx-backtrace --enable-link-serialization=1 --enable-linker-build-id --enable-lto --enable-multilib --enable-plugin --enable-shared --enable-threads=posix --disable-libssp --disable-libstdcxx-pch --disable-werror
    Thread model: posix
    Supported LTO compression algorithms: zlib zstd
    gcc version 12.1.1 20220730 (GCC) 
    COLLECT_GCC_OPTIONS='-D' '-Wall' '-D' '-Werror' '-D' '-Wextra' '-D' '-Wpedantic' '-D' 'FMT_LOCALE' '-D' 'FMT_SHARED' '-D' 'HL_DEBUG' '-D' 'SPDLOG_COMPILED_LIB' '-D' 'SPDLOG_FMT_EXTERNAL' '-D' 'SPDLOG_SHARED_LIB' '-D' 'hyper_EXPORTS' '-I' '/home/developer/projects/hyper_engine/hyper/include' '-I' '/home/developer/projects/hyper_engine/hyper/src' '-I' '/home/developer/projects/hyper_engine/hyper/src/base' '-g' '-fPIC' '-Wall' '-Wextra' '-std=gnu++20' '-Winvalid-pch' '-include' '/home/developer/projects/hyper_engine/cmake-build-debug/hyper/CMakeFiles/hyper.dir/cmake_pch.hxx' '-fpch-preprocess' '-v' '-dD' '-E' '-D' '___CIDR_DEFINITIONS_END' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
     /usr/lib/gcc/x86_64-pc-linux-gnu/12.1.1/cc1plus -E -quiet -v @/tmp/ccfDgBhj -D_GNU_SOURCE -D -Wall -D -Werror -D -Wextra -D -Wpedantic -D FMT_LOCALE -D FMT_SHARED -D HL_DEBUG -D SPDLOG_COMPILED_LIB -D SPDLOG_FMT_EXTERNAL -D SPDLOG_SHARED_LIB -D hyper_EXPORTS -D ___CIDR_DEFINITIONS_END -include /home/developer/projects/hyper_engine/cmake-build-debug/hyper/CMakeFiles/hyper.dir/cmake_pch.hxx /tmp/compiler-file9852660908641365533 -mtune=generic -march=x86-64 -std=gnu++20 -Wall -Wextra -Winvalid-pch -fPIC -fpch-preprocess -g -fworking-directory -dD -dumpbase compiler-file9852660908641365533
    ignoring nonexistent directory "/usr/lib/gcc/x86_64-pc-linux-gnu/12.1.1/../../../../x86_64-pc-linux-gnu/include"
    #include "..." search starts here:
    #include <...> search starts here:
     /home/developer/projects/hyper_engine/hyper/include
     /home/developer/projects/hyper_engine/hyper/src
     /home/developer/projects/hyper_engine/hyper/src/base
     /usr/lib/gcc/x86_64-pc-linux-gnu/12.1.1/../../../../include/c++/12.1.1
     /usr/lib/gcc/x86_64-pc-linux-gnu/12.1.1/../../../../include/c++/12.1.1/x86_64-pc-linux-gnu
     /usr/lib/gcc/x86_64-pc-linux-gnu/12.1.1/../../../../include/c++/12.1.1/backward
     /usr/lib/gcc/x86_64-pc-linux-gnu/12.1.1/include
     /usr/local/include
     /usr/lib/gcc/x86_64-pc-linux-gnu/12.1.1/include-fixed
     /usr/include
    End of search list.
    <command-line>: error: macro names must be identifiers
    <command-line>: error: macro names must be identifiers
    <command-line>: error: macro names must be identifiers
    <command-line>: error: macro names must be identifiers
    
    

[Failed to reload]

當我輸入以下代碼時,我沒有收到任何錯誤(據我所知,此代碼與前面的代碼完全相同,減去它的生成能力取決於所使用的編譯器):

target_compile_options(hyper PRIVATE
                       -Werror -Wall -Wextra
                       )

IIRC 這曾經在我以前的項目上工作過,那么有什么用呢?

正如 Stepthen Newell 在評論中正確指出的那樣, target_compile_definitions() 添加了編譯器定義,因此將target_compile_definitions()替換為target_compile_options()就可以了, derp

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM