CMake中的整体程序优化 [英] WholeProgramOptimization in CMake

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

问题描述

我该怎么做才能从CMake启用WholeProgramOptimization?

What can I do to enable WholeProgramOptimization from CMake ?

这是我尝试过的:

  • 我已安装CMake 3.10.2
  • 我已安装Visual Studio 2017 15.5.4
  • 我创建了一个目录C:\ Wpo
  • 我创建了一个空目录C:\ Wpo \ Build
  • 我创建了一个包含int main(){return 0;}
  • 的C:\ Wpo \ Wpo.cpp文件
  • 我创建了一个C:\ Wpo \ CMakeLists.txt文件,其中包含以下内容:

  • I have CMake 3.10.2 installed
  • I have Visual Studio 2017 15.5.4 installed
  • I created a directory C:\Wpo
  • I created an empty directory C:\Wpo\Build
  • I created a C:\Wpo\Wpo.cpp file containing int main(){return 0;}
  • I created a C:\Wpo\CMakeLists.txt file containing the following:

CMAKE_MINIMUM_REQUIRED (VERSION 3.10)
PROJECT(Wpo)
ADD_EXECUTABLE(Wpo "../Wpo.cpp")
TARGET_COMPILE_OPTIONS(Wpo PRIVATE "$<$<CONFIG:Release>:/GL>")
SET_TARGET_PROPERTIES(Wpo PROPERTIES LINK_FLAGS_RELEASE "/LTCG")

  • 我打开了一个命令行并创建了我的Visual Studio解决方案:

  • I openned a command line and created my Visual Studio solution:

    cd C:\Wpo\Build
    cmake ..
    

  • 但是,当我在Visual Studio中打开解决方案时,未设置整个程序的优化.有趣的是,vcxproj文件中有一个WholeProgramOptimization:

    But when I open my solution in Visual Studio, Whole program Optimization is not set. Interestingly enough, there is a WholeProgramOptimization in the vcxproj file:

          <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
            <ClCompile>
              <AdditionalIncludeDirectories>%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
              <AssemblerListingLocation>Release/</AssemblerListingLocation>
              <CompileAs>CompileAsCpp</CompileAs>
              <ExceptionHandling>Sync</ExceptionHandling>
              <InlineFunctionExpansion>AnySuitable</InlineFunctionExpansion>
              <Optimization>MaxSpeed</Optimization>
              <PrecompiledHeader>NotUsing</PrecompiledHeader>
              <RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
              <RuntimeTypeInfo>true</RuntimeTypeInfo>
              <WarningLevel>Level3</WarningLevel>
    
              <WholeProgramOptimization>true</WholeProgramOptimization>
    
              <PreprocessorDefinitions>WIN32;_WINDOWS;NDEBUG;CMAKE_INTDIR="Release";%(PreprocessorDefinitions)</PreprocessorDefinitions>
              <ObjectFileName>$(IntDir)</ObjectFileName>
              <DebugInformationFormat></DebugInformationFormat>
            </ClCompile>
          </ItemDefinitionGroup>
    

    如果我从项目的属性中手动选择整个程序优化",则会在vcxproj文件的另一部分中添加一个条目:

    If I manually select Whole Program Optimizations from the Properties of the Project, an entry is added in another part of the vcxproj file:

          <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
            <ConfigurationType>Application</ConfigurationType>
            <CharacterSet>MultiByte</CharacterSet>
            <PlatformToolset>v141</PlatformToolset>
    
            <WholeProgramOptimization>true</WholeProgramOptimization>
    
          </PropertyGroup>
    

    • 这是CMake的错误吗?似乎是在错误的位置添加了WholeProgramOptimization部分.
    • 这是Visual Studio的错误吗?也可能是回归分析.
    • 我是否采用不赞成使用的方式? CMake经常发生这种情况;-)
      • Is it a bug from CMake ? It looks like it is adding this WholeProgramOptimization section in the wrong place.
      • Is it a bug from Visual Studio ? It could be a regression too.
      • Am I employing a deprecated way of doing this ? This happens a lot with CMake ;-)
      • 任何帮助将不胜感激.

        推荐答案

        在Visual Studio项目中,有 3 个地方显示了整个程序优化"设置-

        There are 3 places in a Visual Studio project where Whole Program Optimization settings come into picture -

        1)在项目级别(项目→常规"选项卡)

        这是启用整体程序优化的便捷元设置.

        This is a convenience meta-setting to enable Whole Program Optimization.

        在项目XML中,它位于<PropertyGroup>/<WholeProgramOptimization>

        In the project XML it's located at<PropertyGroup>/<WholeProgramOptimization>

        2)在编译器级别(C/C ++&rarr;优化"标签)

        这是实际的/GL设置,默认为项目级设置.

        This is the actual /GL setting, it defaults to the project-level setting.

        在项目XML中,它位于<ItemDefinitionGroup>/<ClCompile>/<WholeProgramOptimization>

        In the project XML it's located at<ItemDefinitionGroup>/<ClCompile>/<WholeProgramOptimization>

        3)在链接器级别(链接器→优化"选项卡)

        这是实际的/LTCG设置,默认为项目级设置.

        This is the actual /LTCG setting, it defaults to the project-level setting.

        在项目XML中,它位于<ItemDefinitionGroup>/<Link>/<LinkTimeCodeGeneration>

        In the project XML it's located at <ItemDefinitionGroup>/<Link>/<LinkTimeCodeGeneration>

        以下CMake命令不会在项目级别设置WholeProgramOptimization,而是在编译器和链接器级别设置.因此,常规"选项卡中的便利"设置为空白.但是,最终效果是相同的. WholeProgramOptimization已打开.

        The following CMake commands won't set WholeProgramOptimization at project level, but at compiler and linker level. That's why the "convenience" setting in the General tab is blank. The net effect, however, is the same. WholeProgramOptimization is on.

        set_target_properties(Wpo PROPERTIES COMPILE_FLAGS "$<$<CONFIG:Release>:/GL>")
        set_target_properties(Wpo PROPERTIES LINK_FLAGS "$<$<CONFIG:Release>:/LTCG>")
        

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

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