Boehm GC 附带的“cord”库导致未定义的引用错误

Posted

技术标签:

【中文标题】Boehm GC 附带的“cord”库导致未定义的引用错误【英文标题】:"cord" library that comes with Boehm GC causes undefined reference errors 【发布时间】:2011-05-08 20:33:07 【问题描述】:

我有一个使用Boehm GC的项目,所以我想我可能会使用它附带的cord字符串库。问题是我对cord 函数的所有调用都会导致“未定义引用”错误。

我在/usr/lib(这是一个Linux系统)中有一个名为libcord.so的文件,我告诉CMake将目标与gccord链接起来。


根据评论 #1 进行编辑:

来自CMakeLists.txt

target_link_libraries(lang gc)
target_link_libraries(lang cord)

来自错误消息:

../lib/liblang.so: undefined reference to `CORD_substr(char const*, unsigned long, unsigned long)'
../lib/liblang.so: undefined reference to `CORD_len(char const*)'
../lib/liblang.so: undefined reference to `CORD_cat(char const*, char const*)'
../lib/liblang.so: undefined reference to `CORD_cmp(char const*, char const*)'
../lib/liblang.so: undefined reference to `CORD_to_char_star(char const*)'

根据评论 #2 进行编辑:

$ make -j4 VERBOSE=1
/usr/bin/cmake -H/home/michael/Projects/lang -B/home/michael/Projects/lang/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /home/michael/Projects/lang/build/CMakeFiles /home/michael/Projects/lang/build/CMakeFiles/progress.marks
/usr/bin/make -f CMakeFiles/Makefile2 all
make[1]: Entering directory `/home/michael/Projects/lang/build'
/usr/bin/make -f lib/CMakeFiles/lang.dir/build.make lib/CMakeFiles/lang.dir/depend
make[2]: Entering directory `/home/michael/Projects/lang/build'
cd /home/michael/Projects/lang/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/michael/Projects/lang /home/michael/Projects/lang/lib /home/michael/Projects/lang/build /home/michael/Projects/lang/build/lib /home/michael/Projects/lang/build/lib/CMakeFiles/lang.dir/DependInfo.cmake --color=
make[2]: Leaving directory `/home/michael/Projects/lang/build'
/usr/bin/make -f lib/CMakeFiles/lang.dir/build.make lib/CMakeFiles/lang.dir/build
make[2]: Entering directory `/home/michael/Projects/lang/build'
make[2]: Nothing to be done for `lib/CMakeFiles/lang.dir/build'.
make[2]: Leaving directory `/home/michael/Projects/lang/build'
/usr/bin/cmake -E cmake_progress_report /home/michael/Projects/lang/build/CMakeFiles  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
[ 94%] Built target lang
/usr/bin/make -f src/CMakeFiles/main.dir/build.make src/CMakeFiles/main.dir/depend
make[2]: Entering directory `/home/michael/Projects/lang/build'
cd /home/michael/Projects/lang/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/michael/Projects/lang /home/michael/Projects/lang/src /home/michael/Projects/lang/build /home/michael/Projects/lang/build/src /home/michael/Projects/lang/build/src/CMakeFiles/main.dir/DependInfo.cmake --color=
make[2]: Leaving directory `/home/michael/Projects/lang/build'
/usr/bin/make -f src/CMakeFiles/main.dir/build.make src/CMakeFiles/main.dir/build
make[2]: Entering directory `/home/michael/Projects/lang/build'
Linking CXX executable lang
cd /home/michael/Projects/lang/build/src && /usr/bin/cmake -E cmake_link_script CMakeFiles/main.dir/link.txt --verbose=1
/usr/bin/c++      CMakeFiles/main.dir/main.cpp.o  -o lang -rdynamic ../lib/liblang.so -lgc -lcord -lgmp -Wl,-rpath,/home/michael/Projects/lang/build/lib 
../lib/liblang.so: undefined reference to `CORD_substr(char const*, unsigned long, unsigned long)'
../lib/liblang.so: undefined reference to `CORD_len(char const*)'
../lib/liblang.so: undefined reference to `CORD_cat(char const*, char const*)'
../lib/liblang.so: undefined reference to `CORD_cmp(char const*, char const*)'
make[2]: Leaving directory `/home/michael/Projects/lang/build'
make[1]: Leaving directory `/home/michael/Projects/lang/build'
../lib/liblang.so: undefined reference to `CORD_to_char_star(char const*)'
collect2: ld returned 1 exit status
make[2]: *** [src/lang] Error 1
make[1]: *** [src/CMakeFiles/main.dir/all] Error 2
make: *** [all] Error 2

【问题讨论】:

请粘贴链接时CMake正在运行的命令行,以及实际输出。 对,现在您可以在详细模式下运行 CMake 或其他方式来获取对 gcc 或它生成的任何内容的实际调用吗?我想查看正在运行的命令,而不是 CMake 规则文件。 【参考方案1】:

据我所见,cord.h 文件中似乎没有包含 extern "C" 语句,因此当您使用 C++ 编译器编译 liblang.so 时,名称修改不正确。

在您#include <cord.h> 的来源中,您可能需要以下内容:

extern "C" 
#include "cord.h"

【讨论】:

我实际上是在我的源文件中编写了原型,因为我的系统由于某种原因没有cord.h。尽管如此,extern "C"'ing 原型修复了它。【参考方案2】:

这可能会帮助您解决 CMake 中的问题:

set_target_properties(<your target>
  PROPERTIES
  LINKER_LANGUAGE C)

【讨论】:

以上是关于Boehm GC 附带的“cord”库导致未定义的引用错误的主要内容,如果未能解决你的问题,请参考以下文章

<Cord​​ova/CDVLocalStorage.h> 未找到

C++ 垃圾回收

System.gc()调用 - 适用的场景

链接器忽略库链接

未定义的参考链接 yaml-cpp 程序与 mingw-w64 + cmake

如何从 nib 中修复此“在托管对象上调用选择器 ... 已被 GC'ed”,其中包含未使用的自定义 UITableViewCell?