CMake每个文件的优化 [英] CMake per file optimizations

查看:173
本文介绍了CMake每个文件的优化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在其他地方已问过问题:如何关闭一个文件的优化?答案通常是这样的:

Elsewhere the question has been asked, "How do I turn off optimizations on one file?" The answer is usually something like this:

cmake_minimum_required( VERSION 3.8 )

project( Hello )

add_executable( hello hello.c foo.c bar.c )

set( CMAKE_C_FLAGS_RELEASE "" )
set( CMAKE_CXX_FLAGS_RELEASE "" )

set_source_files_properties( hello.c 
                PROPERTIES
                COMPILE_FLAGS -O0 )

此除非您这样调用cmake,否则它会起作用:

This works unless you invoke cmake like this:

cmake -GNinja -DCMAKE_BUILD_TYPE=Release ../hello

您可以在您的 build.ninja

FLAGS = -O3 -DNDEBUG   -O0

检查 COMPILE_FLAGS


在编译此源文件时要添加的附加标志。

Additional flags to be added when compiling this source file.

这很有意义,它被添加到COMPILE_FLAGS列表中,不会覆盖现有的编译器标志。

This makes sense, it is added to the list of COMPILE_FLAGS, it does not override existing compiler flags.

因此,在CMake中,您如何是否在单个文件上覆盖了优化级别,并能够在 Release 中编译项目的其余部分?否则,您可以强制编译为 CMAKE_BUILD_TYPE = ,这是默认行为,但这在一定程度上不利于Cmake的卖点。

So, within CMake how can you override the optimisation level on a single file and being able to compile the rest of the project in Release? Otherwise you can force the compile to CMAKE_BUILD_TYPE="" which is the default behavior, but that somewhat defeats a selling point of Cmake.

推荐答案

您不能在源文件级别用 makefile CMake生成器覆盖编译器选项。选项始终会附加(请参见我的答案)。 is-cmake-set-variable-recursive> Cmake是否设置了递归变量?表示完整的公式)。

You can't overwrite compiler options with the makefile CMake generators on source file level. Options are always appended (see my answer at Is Cmake set variable recursive? for the complete formula).

这是-据我所知-仅受 Visual Studio 解决方案/项目生成器支持。这些生成器具有标志表来标识位于同一组/确实覆盖了先前定义的标志。

This is - as far as I know - only supported with the Visual Studio solution/project generators. These generators have flag tables to identify flags that are in the same group/that does overwrite a previous defined flag.

因此,您的请求更像是一个功能请求,它还向CMake的 makefile 生成器中添加了编译器选项表。

So yours is more like a feature request to also add compiler option tables to CMake's makefile generators.

替代方案

我只是想添加一些我想出的疯狂CMake魔术作为解决方法。在 project()命令之后,将以下内容添加到主 CMakeLists.txt 中:

I just wanted to add some crazy CMake magic I came up with as a workaround. Add the following to your main CMakeLists.txt after the project() command:

if (CMAKE_BUILD_TYPE)
    define_property(
        SOURCE
        PROPERTY COMPILE_FLAGS 
        INHERITED 
        BRIEF_DOCS "brief-doc"
        FULL_DOCS  "full-doc"
    )
    string(TOUPPER ${CMAKE_BUILD_TYPE} _build_type)
    set_directory_properties(PROPERTIES COMPILE_FLAGS "${CMAKE_CXX_FLAGS_${_build_type}}")
    set(CMAKE_CXX_FLAGS_${_build_type} "")
endif()    

此示例将 CMAKE_CXX_FLAGS_<构建类型> 内容移动到新的 COMPILE_FLAGS 目录属性中然后通过COMPILE_FLAGS 源文件属性noreferrer> define_property(... INHERITED ...)

This example moves the CMAKE_CXX_FLAGS_<build type> content into an new COMPILE_FLAGS directory property that is then linked to COMPILE_FLAGS source file property via define_property(... INHERITED ...).

现在,仅在 COMPILE_FLAGS 中为每个源文件定义特定于构建类型的标志,您可以覆盖/更改它们,例如并使用示例中的代码段:

Now the build type specific flags are only defined in COMPILE_FLAGS for each source file and you can overwrite/change them e.g. with the code snippet from your example:

set_source_files_properties( 
    hello.c 
    PROPERTIES
    COMPILE_FLAGS -O0 
)






>参考

  • Directory properties and subdirectories
  • CMake: How do I change properties on subdirectory project targets?

这篇关于CMake每个文件的优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆