cmake
type_error
ai_generated
true
CMake 错误:目标 "mylib" 具有 INTERFACE_LIBRARY 类型,但 target_link_libraries 使用了非 INTERFACE 关键字。
CMake Error: Target "mylib" has INTERFACE_LIBRARY type but target_link_libraries was called with non-INTERFACE keyword.
ID: cmake/target-link-libraries-interface-mismatch
90%修复率
88%置信度
1证据数
2023-11-20首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| CMake 3.12 | active | — | — | — |
| CMake 3.18 | active | — | — | — |
| CMake 3.27 | active | — | — | — |
根因分析
INTERFACE 库目标只能使用 INTERFACE 关键字在 target_link_libraries 中链接,但使用了 PUBLIC 或 PRIVATE。
English
An INTERFACE library target can only be linked using INTERFACE keyword in target_link_libraries, but PUBLIC or PRIVATE was used instead.
官方文档
https://cmake.org/cmake/help/latest/command/target_link_libraries.html解决方案
-
将 target_link_libraries(mylib PUBLIC some_dep) 替换为 target_link_libraries(mylib INTERFACE some_dep)。示例:target_link_libraries(mylib INTERFACE some_dep)。
-
如果目标应有源文件,将其类型更改为 STATIC 或 SHARED:add_library(mylib STATIC source.cpp)。然后根据需要使用 PUBLIC/PRIVATE。
无效尝试
常见但无效的做法:
-
85% 失败
INTERFACE libraries are meant for header-only or pure-interface targets; changing type alters build semantics and may require source files.
-
95% 失败
The dependency is required for consuming targets to find headers or propagate compile definitions.