Toolchain.cmake to cross-compile kenlm for Android - c++

I try to make kenlm binaries usable on Android. Kenlm is written in c++ and uses cmake, so I tried to do a toolchain file to crosscompile with cmake.
My toolchain file looks like that :
set(CMAKE_SYSTEM_NAME Android)
set(CMAKE_SYSTEM_VERSION 26)
set(CMAKE_ANDROID_ARCH_ABI arm64-v8a)
set(CMAKE_ANDROID_NDK "/home/marie/Android/Sdk/ndk/16.1.4479499")
set(BZIP2_LIBRARIES "/usr/lib/x86_64-linux-gnu")
set(LIBLZMA_LIBRARY "/usr/lib/x86_64-linux-gnu/liblzma.so")
set(LIBLZMA_LIBRARIES "/lib/x86_64-linux-gnu")
set(LIBLZMA_HAS_AUTO_DECODER TRUE)
set(LIBLZMA_HAS_EASY_ENCODER TRUE)
set(LIBLZMA_HAS_LZMA_PRESET TRUE)
set(ZLIB_LIBRARIES "/home/marie/Android/Sdk/ndk/16.1.4479499/platforms/android-26/arch-arm64/usr/lib")
and the CMakeLists.txt from kenlm
cmake_minimum_required(VERSION 2.6)
# Define a single cmake project
project(kenlm)
option(FORCE_STATIC "Build static executables" OFF)
if (FORCE_STATIC)
#presumably overkill, is there a better way?
#http://cmake.3232098.n2.nabble.com/Howto-compile-static-executable-td5580269.html
set(Boost_USE_STATIC_LIBS ON)
set_property(GLOBAL PROPERTY LINK_SEARCH_START_STATIC ON)
set_property(GLOBAL PROPERTY LINK_SEARCH_END_STATIC ON)
set(BUILD_SHARED_LIBRARIES OFF)
if (MSVC)
set(flag_vars
CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO
CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE
CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO)
foreach(flag_var ${flag_vars})
if(${flag_var} MATCHES "/MD")
string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
endif(${flag_var} MATCHES "/MD")
endforeach(flag_var)
else (MSVC)
if (NOT CMAKE_C_COMPILER_ID MATCHES ".*Clang")
set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++ -static")
endif ()
set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
endif ()
#Annoyingly the exectuables say "File not found" unless these are set
set(CMAKE_EXE_LINK_DYNAMIC_C_FLAGS)
set(CMAKE_EXE_LINK_DYNAMIC_CXX_FLAGS)
set(CMAKE_SHARED_LIBRARY_C_FLAGS)
set(CMAKE_SHARED_LIBRARY_CXX_FLAGS)
set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS)
set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS)
endif ()
# Compile all executables into bin/
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
# Compile all libraries into lib/
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()
option(COMPILE_TESTS "Compile tests" OFF)
if (COMPILE_TESTS)
# Tell cmake that we want unit tests to be compiled
include(CTest)
enable_testing()
endif()
# Add our CMake helper functions
include(cmake/KenLMFunctions.cmake)
if(MSVC)
set(CMAKE_C_FLAGS "${CMAKE_CXX_FLAGS} /w34716")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /w34716")
endif()
# And our helper modules
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/modules)
# We need boost
find_package(Boost 1.41.0 REQUIRED COMPONENTS
program_options
system
thread
unit_test_framework
)
# Define where include files live
include_directories(
${PROJECT_SOURCE_DIR}
${Boost_INCLUDE_DIRS}
)
# Process subdirectories
add_subdirectory(util)
add_subdirectory(lm)
When i try to
cmake -DCMAKE_TOOLCHAIN_FILE="./cmake_toolchain.cmake" ..
I get that
...
Unable to find the requested Boost libraries.
Boost version: 1.65.1
Boost include path: /usr/include
Could not find the following Boost libraries:
boost_program_options
boost_system
boost_thread
boost_unit_test_framework
...
So i tried to add the boost dir in the toolchain file :
set(BOOST_LIBRARYDIR "/usr/lib/x86_64-linux-gnu")
And it workd for the boost libraries but another error pops
-- Boost version: 1.65.1
-- Found the following Boost libraries:
-- program_options
-- system
-- thread
-- unit_test_framework
-- chrono
-- date_time
-- atomic
...
-- Configuring done
WARNING: Target "kenlm_util" requests linking to directory "/usr/lib/x86_64-linux-gnu". Targets may link only to libraries. CMake is dropping the item.
WARNING: Target "probing_hash_table_benchmark" requests linking to directory "/usr/lib/x86_64-linux-gnu". Targets may link only to libraries. CMake is dropping the item.
WARNING: Target "query" requests linking to directory "/usr/lib/x86_64-linux-gnu". Targets may link only to libraries. CMake is dropping the item.
WARNING: Target "fragment" requests linking to directory "/usr/lib/x86_64-linux-gnu". Targets may link only to libraries. CMake is dropping the item.
WARNING: Target "kenlm" requests linking to directory "/usr/lib/x86_64-linux-gnu". Targets may link only to libraries. CMake is dropping the item.
WARNING: Target "build_binary" requests linking to directory "/usr/lib/x86_64-linux-gnu". Targets may link only to libraries. CMake is dropping the item.
WARNING: Target "kenlm_benchmark" requests linking to directory "/usr/lib/x86_64-linux-gnu". Targets may link only to libraries. CMake is dropping the item.
WARNING: Target "lmplz" requests linking to directory "/usr/lib/x86_64-linux-gnu". Targets may link only to libraries. CMake is dropping the item.
WARNING: Target "count_ngrams" requests linking to directory "/usr/lib/x86_64-linux-gnu". Targets may link only to libraries. CMake is dropping the item.
WARNING: Target "kenlm_builder" requests linking to directory "/usr/lib/x86_64-linux-gnu". Targets may link only to libraries. CMake is dropping the item.
WARNING: Target "filter" requests linking to directory "/usr/lib/x86_64-linux-gnu". Targets may link only to libraries. CMake is dropping the item.
WARNING: Target "phrase_table_vocab" requests linking to directory "/usr/lib/x86_64-linux-gnu". Targets may link only to libraries. CMake is dropping the item.
WARNING: Target "streaming_example" requests linking to directory "/usr/lib/x86_64-linux-gnu". Targets may link only to libraries. CMake is dropping the item.
WARNING: Target "interpolate" requests linking to directory "/usr/lib/x86_64-linux-gnu". Targets may link only to libraries. CMake is dropping the item.
-- Generating done
I've been trying many things to make it work like set(LINK_DIRECTORIES), set(CMAKE_INCLUDE_PATH), set(Boost_LIBRARY_DIRS) (to add multiple boost dirs), set(KENLM_ROOT_DIR), set(KENLM_LIB), set(KENLM_UTIL_LIB), set(KENLM_INC)
Nothing worked, and i've been searching for a while know, so if you have some hints it would be great
-----------------------------------------------------------------------------------------------------------------------------------
**EDIT : I compiled boost for android but I dont succeed to do that with bzip2, I know have an error of this style **
-- Found BZip2: /usr/local/lib/libbz2.a (found version "1.0.8")
-- Looking for BZ2_bzCompressInit
-- Looking for BZ2_bzCompressInit - not found

Related

CMake can not find SFML lib

I have a project depends on SFML lib on C++. I trying to make it with CMake.
CMakeLists.txt is:
cmake_minimum_required(VERSION 3.16.3)
project(3D_Renderer_from_scratch)
set(CMAKE_CXX_STANDARD 17)
include_directories(headers source)
set(SFML_STATIC_LIBRARIES TRUE)
find_package(SFML COMPONENTS window graphics system)
set(SOURCES
Main.cpp
source/Application.cpp
source/Box.cpp
source/Camera.cpp
source/FileReader.cpp
source/KeyboardHandler.cpp
source/Sphere.cpp
source/Triangle.cpp
source/Window.cpp
source/World.cpp
)
add_executable(executable ${SOURCES})
target_link_libraries(executable ${SFML_LIBRARIES} ${SFML_DEPENDENCIES})
After running cmake . I have the following error:
$ cmake .
-- Requested SFML configuration (Static) was not found
CMake Warning at CMakeLists.txt:10 (find_package):
Found package configuration file:
/usr/lib/x86_64-linux-gnu/cmake/SFML/SFMLConfig.cmake
but it set SFML_FOUND to FALSE so package "SFML" is considered to be NOT
FOUND.
-- Configuring done
-- Generating done
-- Build files have been written to: /home/mcjohn974/3D_Renderer_from_scratch
How can I fix it ? (sfml lib is already installed)
As already mentioned in the comments (why not write it as an answer?), CMake was looking for static SFML libraries and it most likely just found shared libraries.
Either make sure that static libraries (the ones with the -s suffix) are provided.
Or don't request static libraries, which you can achieve be not setting SFML_STATIC_LIBRARIES.

AirSim/Unity build on macOS ('boost/filesystem.hpp' file not found)

I am trying to install AirSim/Unity on MacOS Catalina. When I run Unity/build.sh I get a fatal error:
In file included from /Users/nfirbas/Documents/AirSim/Unity/AirLibWrapper/AirsimWrapper/Source/Logger.cpp:3:
/Users/nfirbas/Documents/AirSim/Unity/AirLibWrapper/AirsimWrapper/Source/Logger.h:6:11: fatal error: 'boost/filesystem.hpp' file not found
#include <boost/filesystem.hpp>
^~~~~~~~~~~~~~~~~~~~~~
1 warning and 1 error generated.
make[2]: *** [CMakeFiles/AirsimWrapper.dir/Source/Logger.cpp.o] Error 1
make[2]: *** Waiting for unfinished jobs....
I have installed boost with brew. It's my first time working with brew so I assume that I need to edit my CMakeList.txt. I tried changing it myself but I didn't get it working.
My CMakeLists.txt:
cmake_minimum_required(VERSION 3.5.0)
if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
set(MACOSX TRUE)
endif()
find_path(AIRSIM_ROOT NAMES AirSim.sln PATHS ".." "../.." "../../.." "../../../.." "../../../../.." "../../../../../..")
message(AirSim Root directory: ${AIRSIM_ROOT})
LIST(APPEND CMAKE_MODULE_PATH "${AIRSIM_ROOT}/cmake/cmake-modules")
LIST(APPEND CMAKE_MODULE_PATH "${RPC_SOURCE_DIR}/cmake")
LIST(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
INCLUDE("${AIRSIM_ROOT}/cmake/cmake-modules/CommonSetup.cmake")
INCLUDE("${CMAKE_CURRENT_SOURCE_DIR}/cmake/rpc-setup.cmake")
INCLUDE("${CMAKE_CURRENT_SOURCE_DIR}/cmake/mav-setup.cmake")
INCLUDE("${CMAKE_CURRENT_SOURCE_DIR}/cmake/airlib-setup.cmake")
INCLUDE("${CMAKE_CURRENT_SOURCE_DIR}/cmake/airsimwrapper-setup.cmake")
IncludeEigen()
project(AirsimWrapper VERSION 0)
# RPC includes & source files
BuildRpc()
# MavLink source files
BuildMavLink()
#AirLib source files
BuildAirlib()
#AirsimWrapper source files
BuildAirsimWrapper()
###################### Link source files to library ######################################
if (${APPLE})
add_library(
${PROJECT_NAME} MODULE
${RPC_LIBRARY_SOURCE_FILES}
${MAVLINK_LIBRARY_SOURCE_FILES}
${AIRLIB_LIBRARY_SOURCE_FILES}
${AIRSIMWRAPPER_LIBRARY_SOURCE_FILES}
)
set_target_properties(${PROJECT_NAME} PROPERTIES BUNDLE TRUE)
else ()
add_library(
${PROJECT_NAME} SHARED
${RPC_LIBRARY_SOURCE_FILES}
${MAVLINK_LIBRARY_SOURCE_FILES}
${AIRLIB_LIBRARY_SOURCE_FILES}
${AIRSIMWRAPPER_LIBRARY_SOURCE_FILES}
)
endif ()
target_link_libraries(${PROJECT_NAME} ${CMAKE_THREAD_LIBS_INIT} -lstdc++ -lpthread -lboost_filesystem)
##################### Build Options #############################3
# Rpc
RpcCheckMSVN()
RpcCmakePkg()
RpcMSVNConfig()
You have told your library target to link against boost_filesystem, but you did not convey in your CMake where to find the Boost header files.
The idiomatic way to find Boost using CMake is to use the configuration files that now ship with Boost (e.g. BoostConfig.cmake), as of Boost version 1.70 and greater. You can make use of these by calling find_package(Boost ...), then linking with the imported target Boost::filesystem:
# Tell CMake to locate Boost on your machine, specifically
# looking for the filesystem library.
find_package(Boost REQUIRED COMPONENTS filesystem)
...
# Link the Boost::filesystem target, which includes the Boost headers.
target_link_libraries(${PROJECT_NAME} PUBLIC
${CMAKE_THREAD_LIBS_INIT}
-lstdc++
-lpthread
Boost::filesystem
)
This will pull in the Boost headers as well, so you don't need an explicit call to target_include_directories() to specify where the Boost headers are.
Note: To ensure the headers are installed on your system, you may need to install boost-devel, in addition to your boost installation.

How to make ExternalProject_Add acting like ADD_SUBDIRECTORY command with cmake?

I'm creating a native library under Android Studio, and I'm hurting the following problem.
In my AndroidStudio project CMakeLists.txt, I have this:
cmake_minimum_required(VERSION 3.7.0)
add_library(native-lib
SHARED
src/main/cpp/native-lib.cpp )
IF(USE_EXTERNAL)
include(ExternalProject)
ExternalProject_Add(
project_mylib
DOWNLOAD_COMMAND ""
SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/src/main/cpp/mylib/"
CMAKE_ARGS
-DCMAKE_INSTALL_PREFIX=${PROJECT_BINARY_DIR}
)
add_dependencies(native-lib project_mylib)
ELSE()
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/src/main/cpp/mylib/)
ENDIF()
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
target_link_libraries(native-lib
${log-lib}
mylib
)
A self-made library is located in src/main/cpp/mylib/, with a CMakeLists.txt:
cmake_minimum_required(VERSION 3.7.0)
project(lib)
add_library(mylib SHARED lib.cpp)
INSTALL(TARGETS mylib
RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/bin
LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib
)
When I use the "traditional" add_subdirectory(...) everything goes well. But, if I use the ExternalProject_Add(...) version, linker is skipping the compiled libmylib.so library and so cannot link mylib to native-lib.
I have the following message: skipping incompatible /home/.../app/.externalNativeBuild/cmake/debug/arm64-v8a/lib/libmylib.so when searching for -lmylib
My guess is that all the flags set by AndroidStudio for the root CMakeLists.txt are not set when the ExternalProject is compile leading to an incompatible shared library.
So, I wonder if there is a way to compile a cmake ExternalProject like it was part of the root project (sharing the same compile flags etc) ?
Thanks for any advice

cmake fails to look up external library(boost) directory

I am trying to use cmake to build a simple parser project. I used boost::program_options in my code, but it seems cmake does not look up the boost lib directory. Get confused and frustrated..
My CMakeLists.txt is
# basic info
CMAKE_MINIMUM_REQUIRED(VERSION 3.1.0)
PROJECT(parser CXX)
SET(CMAKE_CXX_STANDARD 14)
# Boost
FIND_PACKAGE(Boost 1.61.0 REQUIRED PATHS /path/to/boost NO_DEFAULT_PATH)
MESSAGE(STATUS "Boost version: ${Boost_VERSION}" )
MESSAGE(STATUS "Boost include dirs: ${Boost_INCLUDE_DIRS}" )
MESSAGE(STATUS "Boost library dirs: ${Boost_LIBRARY_DIRS}" )
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
LINK_DIRECTORIES(${Boost_LIBRARY_DIRS})
# main
FILE(GLOB main_SRC *.cpp)
ADD_EXECUTABLE(main ${main_SRC})
TARGET_LINK_LIBRARIES(main boost_program_options)
I use a modified BoostConfig.cmake (which points to my own Boost library)
The result for running cd build; cmake .. is
-- Boost version: 1.61.0
-- Boost include dirs: /path/to/boost/include
-- Boost library dirs: /path/to/boost/lib
-- Configuring done
-- Generating done
-- Build files have been written to: /path/to/build
Thus I believe cmake has found the my Boost library. But then if I run make I will end up with a bunch of errors like
undefined reference to `boost::program_options ... `
If I run make VERBOSE=1 I will see
/path/to/g++ -rdynamic CMakeFiles/main.dir/main.cpp.o -o main -lboost_program_options
the command does not have -L or -Wl,rpath for ${Boost_LIBRARY_DIRS}. If I add the flag manually then I can compile the project successfully.
I also tried linking to static lib by TARGET_LINK_LIBRARIES(main ${Boost_LIBRARY_DIR}/libboost_program_options.a) instead of LINK_DIRECTORIES(), but the same error was thrown.
Not sure what makes things wrong..
Thanks in advance
What happens with the standard method using the imported targets? That is,
# Use and set variable/-Dflag/environment for custom Boost location
set(BOOST_ROOT /path/to/boost)
find_package(Boost 1.61.0 REQUIRED
COMPONENTS program_options)
add_executable(main ...)
target_link_libraries(main Boost::program_options)

CMake user built libraries; cannot specify link libraries for target

I'm building a project in Cpp that will communicate with my Java apps via rabbitmq and post updates to twitter. I'm using a few libraries from github
rabbitmq-c
Rabbit installed to /usr/local/lib64
jansson - json library
I installed this a while back for another project, went to /usr/local/lib
twitcurl - C lib for Twitter API
Got installed to /usr/local/lib
If it matters, I'm using CLion as my IDE, which displays jansson and rabbit under auto-complete when defining includes - so that's picking the libs off my system somehow
e.g.
#include <jansson.h>
#include <amqp.h>
I link them using the target_link_libraries(name libs...) and I see output saying
build$ cmake ..
CMake Error at CMakeLists.txt:30 (target_link_libraries):
Cannot specify link libraries for target "twitcurl" which is not built by
this project.
I set LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/usr/local/lib:/usr/local/lib64
I try to set the CMAKE_LIBRARY_PATH to include usr/local/lib and lib64 but doesn't seem to have any effect. Here's my CMakeLists.txt file
#
# This is a CMake makefile. You can find the cmake utility and
# information about it at http://www.cmake.org
#
cmake_minimum_required(VERSION 2.6)
set(PROJECT_NAME twitterUpdater)
set(SOURCE_FILES main.cpp)
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
set(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} "/usr/local/lib"
"/usr/local/lib64")
project(${PROJECT_NAME})
find_package(X11 REQUIRED)
find_package(OpenCV REQUIRED)
IF (X11_FOUND)
INCLUDE_DIRECTORIES(${X11_INCLUDE_DIR})
LINK_LIBRARIES(${X11_LIBRARIES})
ENDIF ( X11_FOUND )
IF (OpenCV_FOUND)
include_directories(${OpenCV_INCLUDE_DIRS})
link_libraries(${OpenCV_LIBS})
ENDIF(OpenCV_FOUND)
add_executable(${PROJECT_NAME} ${SOURCE_FILES})
target_link_libraries(${project_name} twitcurl jansson rabbitmq)
What's confusing me is another project I have uses jansson by simply adding it here TARGET_LINK_LIBRARIES(${project_name} dlib jansson)
What did I miss?? Thanks
CMake variables are case sensitive, thus the variable ${project_name} results in an empty string. Use ${PROJECT_NAME} instead, i.e.:
target_link_libraries(${PROJECT_NAME} twitcurl jansson rabbitmq)
Running CMake with the flag --warn-uninitialized helps you detect mistakes like this.