“cord” library that comes with Boehm GC causes undefined reference errors-Record and share programming errors

I have a project that uses Boehm GC, so I thought that I might use the cord string library that comes with it. The problem is that all my calls to the cord functions cause “undefined reference” errors.

I do have a file named libcord.so in /usr/lib (this is a Linux system), and I told CMake to link the target with gc and cord.

Edit in response to comment #1:

from CMakeLists.txt

target_link_libraries(lang gc)
target_link_libraries(lang cord)

from error message:

../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*)'

Edit in response to comment #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
  1. From what I can see, the cord.h file does not seem to include extern "C" statements in it, and so the name mangling is being done incorrectly when you are compiling liblang.so with a C++ compiler.

    In the source where you #include you probably need something like:

    extern "C" {
    #include "cord.h"
    }
    
  2. This mau help you to solve your problem in CMake:

    set_target_properties( PROPERTIES LINKER_LANGUAGE C)

Originally posted 2013-08-31 05:31:58.