CMP0086 cmake build_error ai_generated true

CMake错误:目标'mylib'需要设置C++标准才能导出

CMake Error: export requires C++ standard to be set for target 'mylib'

ID: cmake/export-requires-cpp-standard

其他格式: JSON · Markdown 中文 · English
85%修复率
85%置信度
1证据数
2024-03-15首次发现

版本兼容性

版本状态引入弃用备注
CMake 3.24 active
CMake 3.28 active
CMake 3.30 active

根因分析

EXPORT命令要求目标具有CXX_STANDARD属性,但目标缺少该属性,通常是因为缺少target_compile_features或CMAKE_CXX_STANDARD。

English

The EXPORT command requires that the target has a CXX_STANDARD property set, but the target lacks it, often due to missing target_compile_features or CMAKE_CXX_STANDARD.

generic

官方文档

https://cmake.org/cmake/help/latest/policy/CMP0086.html

解决方案

  1. Add target_compile_features(mylib PUBLIC cxx_std_17) before export command
  2. Set CXX_STANDARD property explicitly on the target: set_target_properties(mylib PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON)
  3. Use cmake_policy(SET CMP0086 NEW) to relax the check if the target already has C++ standard from dependencies

无效尝试

常见但无效的做法:

  1. Set CMAKE_CXX_STANDARD in CMakeLists.txt after add_library 50% 失败

    Setting CMAKE_CXX_STANDARD globally may not propagate to exported targets if not set before the target definition.

  2. Add 'set(CMAKE_CXX_STANDARD 17)' without target_compile_features 60% 失败

    The export check specifically looks for target_compile_features or explicit CXX_STANDARD property on the target; global variable may be ignored.

  3. Use INTERFACE property instead of PUBLIC for compile features 70% 失败

    INTERFACE properties are not sufficient for export; they must be PUBLIC or PRIVATE to be visible.