从 Cmake 创建 Xcode 项目包含不需要的编译器标志

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37550199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:51:01  来源:igfitidea点击:

Creating Xcode project from Cmake contains unwanted compiler flags

c++xcodec++11cmake

提问by P. J. Jones.

I've been unable to find a definitive answer, so apologies if the answer is out there.

我一直无法找到一个明确的答案,所以如果答案在那里,我深表歉意。

I use cmake to generate project files as I tend to work cross-platform. On OSX/Xcode, I'm seeing unwanted compiler flags being used, specifically warning enable/disable. I like to compile with as many warnings on as possible, and set warnings as errors.

我使用 cmake 来生成项目文件,因为我倾向于跨平台工作。在 OSX/Xcode 上,我看到使用了不需要的编译器标志,特别是警告启用/禁用。我喜欢编译时带有尽可能多的警告,并将警告设置为错误。

Consider the following:

考虑以下:

File: CMakeLists.txt

文件:CMakeLists.txt

cmake_minimum_required( VERSION 3.0 )
project( test )
set( CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} -std=c++14 )
add_compile_options( "-Werror" )
add_compile_options( "-Weverything" )
add_compile_options( "-Wno-c++98-compat-pedantic" )
add_compile_options( "-Wno-padded" )
add_executable( Test main.cpp )
target_link_libraries( Test libstdc++.dylib libc++.dylib )

File: main.cpp

文件:main.cpp

#include <iostream>

int main( int argc, char * const argv[] )
{
    (void)argc;
    (void)argv;
    std::cout << "Kittens." << std::endl;
    return 0;
}

If I then run cmake:

如果我然后运行 ​​cmake:

cmake -G Xcode .

(normally I'd use a builds directory, but not here for simplicity).

(通常我会使用 builds 目录,但为了简单起见不在这里)。

Then build (from the command line):

然后构建(从命令行):

cmake --build . --config Debug --target ALL_BUILD

The simple sample will compile. If I now look at the command used to compile main.cpp I see the following:

将编译简单示例。如果我现在查看用于编译 main.cpp 的命令,我会看到以下内容:

CompileC test.build/Debug/Test.build/Objects-normal/x86_64/main.o main.cpp normal x86_64 c++ com.apple.compilers.llvm.clang.1_0.compiler
    cd /Users/username/Development/test
    export LANG=en_US.US-ASCII
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -x c++ -arch x86_64 -fmessage-length=101 -fdiagnostics-show-note-include-stack -fmacro-backtrace-limit=0 -fcolor-diagnostics -Wno-trigraphs -fpascal-strings -O0 -Wno-missing-field-initializers -Wno-missing-prototypes -Wno-return-type -Wno-non-virtual-dtor -Wno-overloaded-virtual -Wno-exit-time-destructors -Wno-missing-braces -Wparentheses -Wswitch -Wno-unused-function -Wno-unused-label -Wno-unused-parameter -Wno-unused-variable -Wunused-value -Wno-empty-body -Wno-uninitialized -Wno-unknown-pragmas -Wno-shadow -Wno-four-char-constants -Wno-conversion -Wno-constant-conversion -Wno-int-conversion -Wno-bool-conversion -Wno-enum-conversion -Wno-shorten-64-to-32 -Wno-newline-eof -Wno-c++11-extensions -DCMAKE_INTDIR=\"Debug\" -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk -fasm-blocks -fstrict-aliasing -Wdeprecated-declarations -Winvalid-offsetof -mmacosx-version-min=10.11 -g -Wno-sign-conversion -I/Users/username/Development/test/Debug/include -I/Users/username/Development/test/test.build/Debug/Test.build/DerivedSources/x86_64 -I/Users/username/Development/test/test.build/Debug/Test.build/DerivedSources -Wmost -Wno-four-char-constants -Wno-unknown-pragmas -F/Users/username/Development/test/Debug -std=c++14 -Werror -Weverything -Wno-c++98-compat-pedantic -Wno-padded -MMD -MT dependencies -MF /Users/username/Development/test/test.build/Debug/Test.build/Objects-normal/x86_64/main.d --serialize-diagnostics /Users/username/Development/test/test.build/Debug/Test.build/Objects-normal/x86_64/main.dia -c /Users/username/Development/test/main.cpp -o /Users/username/Development/test/test.build/Debug/Test.build/Objects-normal/x86_64/main.o

The really annoying thing here is any of the -W options. In my CMakeLists.txt I've clearly said I want all warnings, warnings treated as errors, and whitelisted a couple of warnings.

这里真正令人讨厌的是任何 -W 选项。在我的 CMakeLists.txt 中,我已经明确表示我想要所有警告,将警告视为错误,并将几个警告列入白名单。

However, if I strip out all the -W options in the compile command, I see (in addition to what I asked for):

但是,如果我去掉 compile 命令中的所有 -W 选项,我会看到(除了我要求的):

-Wno-trigraphs
-Wno-missing-field-initializers
-Wno-missing-prototypes
-Wno-return-type
-Wno-non-virtual-dtor
-Wno-overloaded-virtual
-Wno-exit-time-destructors
-Wno-missing-braces
-Wparentheses
-Wswitch
-Wno-unused-function
-Wno-unused-label
-Wno-unused-parameter
-Wno-unused-variable
-Wunused-value
-Wno-empty-body
-Wno-uninitialized
-Wno-unknown-pragmas
-Wno-shadow
-Wno-four-char-constants
-Wno-conversion
-Wno-constant-conversion
-Wno-int-conversion
-Wno-bool-conversion
-Wno-enum-conversion
-Wno-shorten-64-to-32
-Wno-newline-eof
-Wno-c++11-extensions
-Winvalid-offsetof
-Wno-sign-conversion
-Wmost
-Wno-four-char-constants
-Wno-unknown-pragmas

Some of those things listed I really do not want disabled. I really want just the -W options I specified in the cmake file being used for compilation, but I can't see who is injecting all these extra things and from where.

列出的一些东西我真的不想被禁用。我真的只想要我在用于编译的 cmake 文件中指定的 -W 选项,但我看不到谁在从哪里注入所有这些额外的东西。

Is there any way I can get cmake to tell xcode not to include all these extra -W options from compilation?

有什么办法可以让 cmake 告诉 xcode 不要在编译中包含所有这些额外的 -W 选项?

Note: using:

注意:使用:

cmake 3.5.2

cmake 3.5.2

Apple LLVM version 7.3.0 (clang-703.0.31)

Apple LLVM 版本 7.3.0 (clang-703.0.31)

回答by Tsyvarev

At project()call, CMake detects compiler (you may see corresponded messages when cmakeis executed) and sets default flags for it. These flags are stored in variables CMAKE_CXX_FLAGS(common) and CMAKE_CXX_FLAGS_<CONFIG>(configuration-specific).

project()调用时,CMake 检测编译器(cmake执行时您可能会看到相应的消息)并为其设置默认标志。这些标志存储在变量CMAKE_CXX_FLAGS(公共)和CMAKE_CXX_FLAGS_<CONFIG>(特定于配置的)中。

You may modify these variables as you want:

您可以根据需要修改这些变量:

1) completely rewrite them

1)完全重写它们

set(CMAKE_CXX_FLAGS "<desired-value>")

2) append new flags to them

2) 向它们附加新标志

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} <additional-flags>")

3) remove specific values from them

3)从中删除特定值

string(REPLACE "<unwanted-flag>" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
# or
string(REGEX REPLACE "<unwanted-flag-regex>" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})

and so on.

等等。