CTranslate library example code compile fail

I’m not a C++ programmer by any stretch, but I wanted to try my hand at the sample code provided in the README.md for CTranslate. So I fired up a docker ubuntu:16.04 container, installed git, g++, libboost-all-dev, and libeigen3-dev. Then I cloned CTranslate and ran git submodule update --init.

Finally, I set the CPLUS_INCLUDE_PATH env var to:
/CTranslate/include:/CTranslate/include/onmt:/CTranslate/lib/TH:/CTranslate/lib/tokenizer/include:/usr/include/eigen3

Now, attempting to compile test.c (copy-pasted from the README) failed spectacularly:

/tmp/ccXYswIf.o: In function 'onmt::ids_to_words(onmt::Dictionary const&, std::vector<unsigned long, std::allocator<unsigned long> > const&)': test.c:(.text+0xaf): undefined reference to 'onmt::Dictionary::get_id_word[abi:cxx11](unsigned long) const' /tmp/ccXYswIf.o: In function 'onmt::ids_to_words_replace(onmt::Dictionary const&, onmt::PhraseTable const&, std::vector<unsigned long, std::allocator<unsigned long> > const&, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&, std::vector<std::vector<float, std::allocator<float> >, std::allocator<std::vector<float, std::allocator<float> > > > const&)': test.c:(.text+0x1c7): undefined reference to 'onmt::Dictionary::unk_id' test.c:(.text+0x1fc): undefined reference to 'onmt::Dictionary::get_id_word[abi:cxx11](unsigned long) const' test.c:(.text+0x2cf): undefined reference to 'onmt::PhraseTable::is_empty() const' test.c:(.text+0x2f0): undefined reference to 'onmt::PhraseTable::lookup(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) const' /tmp/ccXYswIf.o: In function 'onmt::words_to_ids(onmt::Dictionary const&, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&)': test.c:(.text+0x463): undefined reference to 'onmt::Dictionary::get_word_id(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) const' /tmp/ccXYswIf.o: In function 'main': test.c:(.text+0x7bb): undefined reference to 'onmt::TranslatorFactory::build(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool, unsigned long, unsigned long)' collect2: error: ld returned 1 exit status

What am I missing?

How did you compile it exactly?

The easiest is to move test.c to CTranslate/cli/test.cc and add:

add_executable(testing
  test.cc
  )
target_link_libraries(testing
  ${PROJECT_NAME}
  )

to CTranslate/cli/CMakeLists.txt and then simply follow the compilation steps from the README.

Thanks for the quick reply, @guillaumekln! I used:
g++ -std=c++11 -o foo test.c

I’ll check the Boost_INCLUDE_DIRS and Boost_LIBRARIES to make sure I didn’t miss anything there. I’d like to try compiling without make/cmake initially as I try to learn how the pieces fit together, unless that’s the only way to get it to compile.

You need CMake to at least compile CTranslate itself, without it is unnecessarily complicated.

If you want to manually compile your binary after that:

g++ -O3 -std=c++11 test.c -o foo -I/CTranslate/include -I/CTranslate/lib/TH -I/CTranslate/lib/tokenizer/include -I/usr/include/eigen3 -L/CTranslate/build -lonmt
LD_PRELOAD=/CTranslate/build/libonmt.so ./test
1 Like