google / cld3

Apache License 2.0
776 stars 109 forks source link

Support for libcld3 installation #40

Open dczaretsky opened 3 years ago

dczaretsky commented 3 years ago

In order to include this library in another C++ program, the libcld3.a and include files need to be installed. However, the makefile does not include support for this right now. I was able to add that support by appending the following code to the CMakeLists.txt file:

install(DIRECTORY include/ DESTINATION include)
install(TARGETS ${PROJECT_NAME} 
    ARCHIVE DESTINATION lib
    LIBRARY DESTINATION lib
    RUNTIME DESTINATION bin)

You will need to create an /include directory with all the *.h files. Then you can compile and install the library as follows:

cd cld3
mkdir build.release
cd build.release
cmake .. -DCMAKE_BUILD_TYPE=Release
cmake --build . --target install

Using cld3 in your program is pretty simple:

#include <nnet_language_identifier.h>

string get_language(const string &str)
{
    if ( str.empty() ) return "";
    chrome_lang_id::NNetLanguageIdentifier lang_id;
    const chrome_lang_id::NNetLanguageIdentifier::Result result = lang_id.FindLanguage(str); 
    return result.is_reliable ? result.language : "";
}

Hope this helps anyone who might be stuck.

ekalchev commented 3 years ago

Thanks for the info. It was useful although I needed to build .lib for windows with visual studio. My first attempt was use GN build tool and modify BUILD.gn файл. It was very straight forward but unfortunately chrome uses clang and use_clang = false is not available. I used this setup

gn gen --args="is_debug=false is_component_build=false target_cpu=\"x64\" use_lld=false" out/x64.release

On theory using VS2019 with clang should work but I got many unresolved externals during the linking and I gave up.

My approach to this was to avoid clang and build all dependencies with VS2017

  1. Follow the steps for buidling cld_3

  2. Build protobuf from source with Visual Studio and cmake matching the chromium protobuf version (currently 3.9.0) Directory location of the source C:/protobuf/protobuf-3.9.0

Add these lines to C:\chromium\src\third_party\cld_3\src\CMakeList.txt with correct path pointing to your directory location

Add this as first line in the file cmake_policy(SET CMP0091 NEW) to allow usage of CMAKE_MSVC_RUNTIME_LIBRARY

set (Protobuf_INCLUDE_DIR "C:/protobuf/protobuf-3.9.0/src") set (Protobuf_LIBRARIES "C:/protobuf/protobuf-3.9.0/cmake/build/solution/Release") set (Protobuf_LITE_LIBRARIES "C:/protobuf/protobuf-3.9.0/cmake/build/solution/Release") set (Protobuf_PROTOC_EXECUTABLE "C:/protobuf/protobuf-3.9.0/cmake/build/solution/Release/protoc.exe") set (CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded")

I am not sure if this is needed but I added it because of the post above

install(DIRECTORY include/ DESTINATION include) install(TARGETS ${PROJECT_NAME} ARCHIVE DESTINATION lib LIBRARY DESTINATION lib RUNTIME DESTINATION bin)

replace add_executable(language_identifier_main src/language_identifier_main.cc) target_link_libraries(language_identifier_main cld3 ${Protobuf_LITE_LIBRARIES})

with link_directories(C:/protobuf/protobuf-3.9.0/cmake/build/solution/Release) add_executable(language_identifier_main src/language_identifier_main.cc) target_link_libraries(language_identifier_main cld3 ${Protobuf_LITE_LIBRARIES}) target_link_libraries(language_identifier_main cld3 libprotobuf.lib) target_link_libraries(language_identifier_main cld3 libprotobuf-lite.lib) target_link_libraries(language_identifier_main cld3 libprotoc.lib)

  1. Execute in cmd.exe

cd "C:\chromium\src\third_party\cld_3\src" mkdir build cd build

for x64 build

cmake -G "Visual Studio 15 2017 Win64" -Dprotobuf_BUILD_TESTS=OFF -DCMAKE_INSTALL_PREFIX=../../../../install ../..

You should be able to build language_identifier_main.vcproj.

This is my cmake file

https://gist.github.com/ekalchev/493c3790cdb7378dc7d1db48fabc8705

gnanaravindhan commented 3 years ago

@dczaretsky , Thanks for the steps able to successfully build libcld3.a but When am trying to execute the C++ code am getting following error in RHEL, any idea

cld3test.cc:(.text+0x6f): undefined reference tochrome_lang_id::NNetLanguageIdentifier::NNetLanguageIdentifier()' cld3test.cc:(.text+0x8f): undefined reference to chrome_lang_id::NNetLanguageIdentifier::FindLanguage(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)' cld3test.cc:(.text+0x111): undefined reference tochrome_lang_id::NNetLanguageIdentifier::~NNetLanguageIdentifier()' cld3test.cc:(.text+0x166): undefined reference to chrome_lang_id::NNetLanguageIdentifier::~NNetLanguageIdentifier()'

dczaretsky commented 3 years ago

@gnanaravindhan sounds like maybe the library was not linked to your program or include files were not found. You might want to check where the include and lib files were installed, then compile with -I and -L parameters to provide their paths respectively, like this:

g++ -I/usr/local/include -L/usr/local/lib -lcld3 cld3test.cc -o cld3test

gnanaravindhan commented 3 years ago

g++ -I/usr/local/include -L/usr/local/lib -lcld3 cld3test.cc -o cld3test

@dczaretsky Thanks for your response, as recommended i've tried the same, but no luck. following is the command which am executing

g++ -I/opt/rh/vcpkg/installed/x64-linux/include -I/usr/local/include -L/usr/local/lib -lcld3 cld3test.cc -o cld3test

gnanaravindhan commented 3 years ago

I've sorted out the issue, after changing the -D_GLIBCXX_USE_CXX11_ABI=0 to -D_GLIBCXX_USE_CXX11_ABI=1 it's started working fine. @dczaretsky Thanks for your help

dczaretsky commented 3 years ago

According to the CMake configuration, you should include the -std=c++11 option for g++. I'm using -std=c++14 in my program compilation, and that also works.

mickeyzo12 commented 3 years ago

I have error as below, could you please help me how to solve it?

cmake .. -DCMAKE_BUILD_TYPE=Release cmake --build . --target install

output is: `C:\Users\HardW\Downloads\cld3-master\build.release>cmake .. -DCMAKE_BUILD_TYPE=Release CMake Error at CMakeLists.txt:3 (project): Running

'nmake' '-?'

failed with:

The system cannot find the file specified

CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage -- Configuring incomplete, errors occurred! See also "C:/Users/HardW/Downloads/cld3-master/build.release/CMakeFiles/CMakeOutput.log"`