CMake找不到导入的库 [英] CMake can't find IMPORTED library
问题描述
在 foo / CMakeLists.txt
中,基于此和此,我拥有以下
In foo/CMakeLists.txt
, based on this and this, I have the following
SET (EXTERNAL_LIB_ROOT "../../external_libs/")
ADD_LIBRARY (avcodec-debug STATIC IMPORTED)
SET_PROPERTY (
TARGET avcodec-debug PROPERTY IMPORTED_LOCATION
${EXTERNAL_LIB_ROOT}/libavcodec-0.8.10.a)
在 bar / CMakeLists.txt
中我有这个:
# old way uses system libraries
#TARGET_LINK_LIBRARIES (bar avformat avcodec avutil)
# new way uses local debug builds
TARGET_LINK_LIBRARIES (bar avformat avcodec-debug avutil)
当我运行 make
我得到
/usr/bin/ld: cannot find -lavcodec-debug
如果我使用旧方法,请进行构建,触摸 foo / CMakeLists.txt
并进行重新构建,CMake的配置输出表明构建系统已找到avcodec-debug 。
If I revert to the old way, build, touch foo/CMakeLists.txt
and rebuild, CMake's configuration output indicates that avcodec-debug is being found by the build system.
那为什么我不能将其添加为依赖项?
So why can't I add it as a dependency?
推荐答案
导入的目标与未导入的目标没有遵循相同的可见性规则。虽然非导入目标是全局的(定义后即可在任何地方可见和访问),但是导入目标仅在定义它们的 CMakeLists.txt
中可见(在由 add_subdirectory()
在此定义的CMakeList中添加的目录中。)
Imported targets do not follow the same visibility rules as non-imported targets. While non-imported targets are global (visible and accessible from anywhere after they're defined), imported targets are only visible in the CMakeLists.txt
where they are defined and below (in directories added by add_subdirectory()
in this defining CMakeList).
由于 foo
是 bar
的兄弟姐妹,目标名称 avcodec-debug
不可见在 bar / CMakeLists.txt
内部,因此将其视为普通库名称。
Since foo
is a sibling of bar
in your case, the target name avcodec-debug
is not visible inside bar/CMakeLists.txt
, so it's treated as a normal library name.
通常首选定义导入目标包含在您包含的文件中,而不是它们自己的项目中。因此,将 foo / CMakeLists.txt
更改(或提取其中的相关部分)为 foo / avcodec.cmake
,然后在顶层CMakeList,替换
It's generally preferred to define imported targets in files you include rather than in their own projects. So change (or extract the relevant parts of) foo/CMakeLists.txt
into foo/avcodec.cmake
and then in the top-level CMakeList, replace
add_subdirectory(foo)
with
include(foo/avcodec.cmake)
这篇关于CMake找不到导入的库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!