gpt4 book ai didi

C 代码结构引发与 CMake 的链接循环

转载 作者:行者123 更新时间:2023-11-30 16:55:49 25 4
gpt4 key购买 nike

我正在尝试使用 CMake 从 NIST 生物识别图像软件构建 bozorth3,但在链接时遇到问题。该软件一方面有一个可执行文件:

//bin/bozorth3.c:
int min_comp_minutiae = MIN_COMP_BOZORTH_MINUTIAE; // defines what libbozorth needs
//...
getopt_spec = malloc_or_exit( (int)strlen(default_getopt_spec) + 1,
"getopt() string" ); // uses a function from libbozorth

...另一边是一个图书馆:

//lib/bozorth3.c:
if ( pstruct->nrows < min_computable_minutiae ) { //uses a variable defined in bin

//lib/bz_alloc.c:
char * malloc_or_exit( int nbytes,
const char * what ) { // implements a function used in bin
//code
}

...和一个通用 header :

//include/bozorth3.h
extern int min_comp_minutiae;
extern char *malloc_or_exit(int, const char *);

使用以下文件集(CMakeLists.txt 除外)

set(LIB_SOURCE_FILES
src/lib/bozorth3/bozorth3.c
src/lib/bozorth3/bz_alloc.c #[[etc...]])
add_library(libbozorth3 ${LIB_SOURCE_FILES})
add_executable(bozorth3 src/bin/bozorth3/bozorth3.c)

由于可执行文件使用该库,我必须添加一个链接:target_link_libraries(bozorth3 libbozorth3)。但这会导致错误:

Linking C shared library ..\..\bin\liblibbozorth3.dll
CMakeFiles\libbozorth3.dir/objects.a(bozorth3.c.obj):bozorth3.c:(.rdata$.refptr.min_computable_minutiae[.refptr.min_computable_minutiae]+0x0): undefined reference to `min_computable_minutiae'

它迫使我创建一个链接,反之亦然:target_link_libraries(libbozorth3 bozorth3) 并覆盖 ENABLE_EXPORTS ,这当然也会导致错误:

Linking C executable ..\..\bin\bozorth3.exe
CMakeFiles\bozorth3.dir/objects.a(bozorth3.c.obj): In function `main':
D:/git/ba-phmf/NBIS/bozorth3/src/bin/bozorth3/bozorth3.c:174: undefined reference to `malloc_or_exit'

我不能两者兼得,因为这显然会导致一个循环:

CMake Error: The inter-target dependency graph contains the following strongly connected component (cycle):
"libbozorth3" of type SHARED_LIBRARY
depends on "bozorth3" (weak)
"bozorth3" of type EXECUTABLE
depends on "libbozorth3" (weak)
At least one of these targets is not a STATIC_LIBRARY. Cyclic dependencies are allowed only among static libraries.

我可以使用原始 makefile 编译该包,但需要将其“翻译”为 CMake 才能集成到我的项目中。我尝试分析 makefile 但找不到解决方案。整个包可以在 github 找到。 .

我在 Windows 7 Professional N x64 上使用 CLion 2016.2.3、CMake 3.7.0-rc2、Msys2 20160921 以及 GCC 6.2.0 和 ld 2.27

最佳答案

这样解决:

add_library(libbozorth3 STATIC ${LIB_SOURCE_FILES})
set_property(TARGET libbozorth3 PROPERTY OUTPUT_NAME bozorth3)
target_include_directories(libbozorth3 PRIVATE
include)

add_executable(bozorth3 ${BIN_SOURCE_FILEs})
target_include_directories(bozorth3 PRIVATE
include)
target_link_libraries(bozorth3
PRIVATE
libbozorth3
commonnbis)

关于C 代码结构引发与 CMake 的链接循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40187214/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com