3

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

我确实有一个名为libcord.soin的文件/usr/lib(这是一个 Linux 系统),我告诉 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
4

2 回答 2

2

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

#include <cord.h>您可能需要类似以下内容的源代码中:

extern "C" {
#include "cord.h"
}
于 2011-05-08T22:10:46.550 回答
0

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

set_target_properties(<your target>
  PROPERTIES
  LINKER_LANGUAGE C)
于 2011-05-09T06:52:28.043 回答