CMP0086 cmake build_error ai_generated true

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

ID: cmake/export-requires-cpp-standard

Also available as: JSON · Markdown · 中文
85%Fix Rate
85%Confidence
1Evidence
2024-03-15First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
CMake 3.24 active
CMake 3.28 active
CMake 3.30 active

Root Cause

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

中文

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

Official Documentation

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

Workarounds

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

中文步骤

  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

Dead Ends

Common approaches that don't work:

  1. Set CMAKE_CXX_STANDARD in CMakeLists.txt after add_library 50% fail

    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% fail

    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% fail

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