CMake can't find IMPORTED library

匿名 (未验证) 提交于 2019-12-03 07:36:14

问题:

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) 

In bar/CMakeLists.txt I have this:

# 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) 

When I run make I get

/usr/bin/ld: cannot find -lavcodec-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?

回答1:

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).

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.

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) 


回答2:

As it mentioned above by Angew the visibility for imported library differs, though you can extend it using GLOBAL modifier. It might be enough for you to modify add_library call next way

ADD_LIBRARY(avcodec-debug STATIC IMPORTED GLOBAL)



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!