You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何通过CMake配置清单工具选项?能否用CMake或脚本配置DPI Awareness值?

如何用CMake配置清单工具选项

Hey there! Let's start with configuring manifest tool options in CMake—this is mostly relevant for Windows targets, so I'll focus on that platform.

Here are the most common, practical approaches:

  • Embed a custom manifest file (the most straightforward and flexible method):
    If you’ve already created a custom app.manifest with your desired settings, you can attach it directly to your executable target using set_target_properties:

    add_executable(MyApp main.cpp)
    
    if(WIN32)
      set_target_properties(MyApp PROPERTIES
          WIN32_EXECUTABLE TRUE  # Mark as a Windows GUI app (optional but common for manifest use)
          MANIFEST_INPUT "${CMAKE_CURRENT_SOURCE_DIR}/app.manifest"
      )
    endif()
    

    CMake will automatically use the Windows SDK's mt.exe (manifest tool) to embed this into your final executable.

  • Pass direct flags to the manifest tool via CMake variables:
    If you don’t want a full custom manifest, you can pass flags directly to the manifest tool through CMAKE_RC_FLAGS (for all targets) or per-target compile options:

    # Apply to all Windows targets
    if(WIN32)
      set(CMAKE_RC_FLAGS "${CMAKE_RC_FLAGS} /manifest:embed /manifestuac:\"level='asInvoker' uiAccess='false'\"")
    endif()
    
    # Or apply to a specific target only
    target_compile_options(MyApp PRIVATE "/manifest:embed")
    

    This is great for quick tweaks like UAC settings without writing a full manifest file.

  • Pro tip: Always wrap these configurations in an if(WIN32) check to avoid breaking builds on non-Windows platforms.


能否通过CMake或脚本配置DPI Awareness值

Absolutely! DPI awareness is critical for making Windows apps look sharp on high-resolution displays, and you can configure it both through CMake and post-build scripts. Here's how:

The most reliable way is to define DPI awareness in a custom manifest file and embed it via CMake (as shown in the first section). Here's the key part of your app.manifest:

<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
  <application xmlns="urn:schemas-microsoft-com:asm.v3">
    <windowsSettings>
      <!-- For Windows 8.1 and earlier -->
      <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true/pm</dpiAware>
      <!-- For Windows 10 1607+ (recommended for modern apps) -->
      <dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2, PerMonitor</dpiAwareness>
    </windowsSettings>
  </application>
</assembly>

Then just embed this manifest into your target using the CMake code from the first question.

If you prefer not to use a separate manifest file, you can also pass manifest settings via linker flags:

if(WIN32)
  target_link_options(MyApp PRIVATE 
    "/manifest:embed"
    "/manifestinput:${CMAKE_CURRENT_SOURCE_DIR}/dpi_manifest.xml"
  )
endif()

Using Post-Build Scripts

If you need to modify DPI awareness after compiling the executable (e.g., for existing binaries), you can use a PowerShell script with the Windows SDK's mt.exe tool. You can even automate this in CMake as a post-build step:

add_custom_command(TARGET MyApp POST_BUILD
  COMMAND powershell -Command "mt.exe -inputresource:'$<TARGET_FILE:MyApp>;#1' -out:'temp.manifest'"
  COMMAND powershell -Command "(Get-Content temp.manifest) -replace '</assembly>', '<application xmlns=\"urn:schemas-microsoft-com:asm.v3\"><windowsSettings><dpiAwareness xmlns=\"http://schemas.microsoft.com/SMI/2016/WindowsSettings\">PerMonitorV2, PerMonitor</dpiAwareness></windowsSettings></application></assembly>' | Set-Content temp.manifest"
  COMMAND powershell -Command "mt.exe -outputresource:'$<TARGET_FILE:MyApp>;#1' -manifest:'temp.manifest'"
  COMMAND del temp.manifest
  WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
  COMMENT "Setting DPI awareness for MyApp"
)

This script modifies the manifest in-place without needing a separate file.


内容的提问来源于stack exchange,提问作者Oleksii Hordiienko

火山引擎 最新活动