cmake
type_error
ai_generated
true
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%Fix Rate
88%Confidence
1Evidence
2023-11-20First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| CMake 3.12 | active | — | — | — |
| CMake 3.18 | active | — | — | — |
| CMake 3.27 | active | — | — | — |
Root Cause
An INTERFACE library target can only be linked using INTERFACE keyword in target_link_libraries, but PUBLIC or PRIVATE was used instead.
generic中文
INTERFACE 库目标只能使用 INTERFACE 关键字在 target_link_libraries 中链接,但使用了 PUBLIC 或 PRIVATE。
Official Documentation
https://cmake.org/cmake/help/latest/command/target_link_libraries.htmlWorkarounds
-
95% success Replace target_link_libraries(mylib PUBLIC some_dep) with target_link_libraries(mylib INTERFACE some_dep). Example: target_link_libraries(mylib INTERFACE some_dep).
Replace target_link_libraries(mylib PUBLIC some_dep) with target_link_libraries(mylib INTERFACE some_dep). Example: target_link_libraries(mylib INTERFACE some_dep).
-
80% success If the target should have source files, change its type to STATIC or SHARED: add_library(mylib STATIC source.cpp). Then use PUBLIC/PRIVATE as needed.
If the target should have source files, change its type to STATIC or SHARED: add_library(mylib STATIC source.cpp). Then use PUBLIC/PRIVATE as needed.
中文步骤
将 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。
Dead Ends
Common approaches that don't work:
-
85% fail
INTERFACE libraries are meant for header-only or pure-interface targets; changing type alters build semantics and may require source files.
-
95% fail
The dependency is required for consuming targets to find headers or propagate compile definitions.