我正在编写一个 CMakeLists.txt 文件来构建我的 C++ 项目,该项目由以下部分组成
- libhybris.so:带有一些导出函数的共享库。
- hybris:链接到 libhybris.so 的可执行文件
- 一组链接到 libhybris.so 的各种共享库
问题是,libhybris.so依赖于libpcre(用于正则表达式功能),所以我有以下语句:
# libhybris.so generation
add_library( libhybris
SHARED
${LIB_SOURCES} )
...
# Needed libraries
target_link_libraries( libhybris
dl
pcre
pthread
readline )
第 3 点中的共享库之一称为 pcre.so,因此我也有以下内容:
add_library( pcre SHARED ${PCRE_SOURCES} )
...
target_link_libraries( pcre
dl
pcre
curl
pthread
readline
ffi
libhybris )
因此,当我运行“cmake .”时,出现以下错误:
-- Configuring done
CMake Error: The inter-target dependency graph contains the following strongly connected component (cycle):
"libhybris" of type SHARED_LIBRARY
depends on "pcre"
"pcre" of type SHARED_LIBRARY
depends on "libhybris"
At least one of these targets is not a STATIC_LIBRARY. Cyclic dependencies are allowed only among static libraries.
因为 CMake 认为 libhybris.so pcre 依赖项(系统 libpcre.so)与我的 pcre.so 相同,但显然不是。
如何在不更改 pcre.so 名称的情况下解决此问题?
最佳答案
在 CMake 中,推荐的方法是使用完整路径指定任何链接库。要获取系统库的完整路径,您可以使用 FIND_PACKAGE(...)
(如果支持)或简单地使用 FIND_LIBRARY(...)
例如,
FIND_LIBRARY( PCRE_SYSTEM_LIB pcre )
ADD_LIBRARY( libhybris SHARED ${LIB_SOURCES} )
TARGET_LINK_LIBRARIES( libhybris
${PCRE_SYSTEM_LIB}
......
)
这将阻止 CMake 将其识别为目标的内容 (nameley pcre
) 扩展到该目标的完整路径。
关于c++ - 当自定义库名称与系统库名称相同时,CMake出现循环依赖错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2977989/