If you wanna use cld-3 in your project but don't want to keep depending on Ninja/gn build system then here's how I did it - on Linux (Arch 4.16.13-1) and g++.
I'm not an expert on this topic, this is just how I (finally) managed to do it, not necessarily the best way to do it (esp. with the STD compatibility). if you have any suggestions about how to do it better let me know.
The benefit of a shared library is that you can copy it for wherever you want and use if from there, as for the static - I found out you can't move it, unless you move the whole out/Debug
folder (or maybe just the out/Debug/obj
folder, I'm not quite sure). There might be some differences in the performence, but they say it isn't significant.
- Check out the Chromium repository.
- Add the file language_identifier_lib.cc to
/PATH/TO/chromium/src/third_party/cld_3/src/src/
- For a shared library add the following to
/PATH/TO/chromium/src/third_party/cld_3/src/src/BUILD.gn
:
shared_library("lang_identifier_so"){
sources = [
"language_identifier_lib.cc"
]
deps = [
":cld_3"
]
}
- For a static library add:
static_library("static_lang_identifier"){
sources = [
"language_identifier_lib.cc"
]
deps = [
":cld_3",
]
}
- Run
gn args out/Debug && ninja -C out/Debug third_party/cld_3/src/src:lang_identifier_so
, in the document that opened add the following arg:use_custom_libcxx=false
, save and close.- For Release (no debug symbols) add the argument
is_debug = false
- For static library replace
:lang_identifier_so
with:static_lang_identifier
.
- For Release (no debug symbols) add the argument
- Create a folder for your own project and put main.cpp and lIdentifier.h in it.
- From
/chromium/src/third_party/cld_3/src/src/out/Debug
copylibprotobuf_lite.so
andliblang_identifier_so.so
(for a static library) to your project folder. - For a shared library - from your project folder run
clang++ -g -std=c++17 -o a.out main.cpp -L . -l lang_identifier_so -l protobuf_lite
- For Release remove the
-g
option - For a static library run
clang++ -g -std=c++17 -o a.out main.cpp -L /PATH/TO/chromium/src/third_party/cld_3/src/src/out/Debug/obj/third_party/cld_3/src/src/ -L . -l static_lang_identifier -l cld_3 -l protos -l protobuf_lite
- For Release remove the
- Then run
export LD_LIBRARY_PATH=``pwd``
(telling the linker to look here for the shared library). - Run
./a.out "This piece of text is in English. Този текст е на Български." 3
- The output:
language code: bg
language name: Bulgarian
probability: 0.917387
reliable: 1
proportion: 0.585366
language code: en
language name: English
probability: 0.999979
reliable: 1
proportion: 0.414634
language code: und
language name:
probability: 0
reliable: 0
proportion: 0
There were 2 issues with std - the first was with the function names (without adding use_custom_libcxx=false
it was called std:__1
rather than std::__cxx11
, which caused undefined reference error), but that was solved with the use_custom_libcxx=false
, as said.
The second issue is the vector, which was returned empty, that I solved by copying the vector into an array and returning it. There's a similar issue in stackoverflow , with an answer which requires to load a bunch of headers and configure a class or something. I decided it was just simpler for now to copy and return a vector, if you come up with a better solution - let me know.
It's worth mentioning that Chromium's build-system uses it's own clang compiler (at /PATH/TO//chromium/src/third_party/llvm-build/Release+Asserts/bin/clang++
) which was one vesion ahead of mine (6.0 vs 7.0), but using their compiler didn't solve the std::vector compatibility problem.