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

- **ID:** `cmake/export-requires-cpp-standard`
- **领域:** cmake
- **类别:** build_error
- **错误码:** `CMP0086`
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

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

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| CMake 3.24 | active | — | — |
| CMake 3.28 | active | — | — |
| CMake 3.30 | active | — | — |

## 解决方案

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
   ```

## 无效尝试

- **Set CMAKE_CXX_STANDARD in CMakeLists.txt after add_library** — Setting CMAKE_CXX_STANDARD globally may not propagate to exported targets if not set before the target definition. (50% 失败率)
- **Add 'set(CMAKE_CXX_STANDARD 17)' without target_compile_features** — The export check specifically looks for target_compile_features or explicit CXX_STANDARD property on the target; global variable may be ignored. (60% 失败率)
- **Use INTERFACE property instead of PUBLIC for compile features** — INTERFACE properties are not sufficient for export; they must be PUBLIC or PRIVATE to be visible. (70% 失败率)
