Create a shared library for dlib - cmake

Following the instructions to compile dlib using cmake (here) generates a static dlib library:
cd examples
mkdir build
cd build
cmake ..
cmake --build . --config Release
How can I instruct cmake to generate a shared (.so) library instead?

If you want to make a .so file then do this:
cd dclib/dlib
mkdir build
cd build
cmake -DBUILD_SHARED_LIBS=1 ..
make
sudo make install
On a unix system, that will install dlib system wide. This means installing the .so file and also the header files so you can compile programs with a command like g++ main.cpp -ldlib. Finally, on linux systems you will also need to run sudo ldconfig after installing any new shared libraries.
However, for most users, I would recommend using CMake as shown in the examples. That way would allow you to enable or disable debugging modes whenever you want and also makes distributing the project easier, both in source form and compiled form. For example, if you wanted to compile on windows then shared libraries are definitely not the way to go. Moreover, using CMake as shown in the examples will always work in a straightforward manner without any setup.

According to dlib/CMakeLists.txt, standalone (not from examples) building of dlib also creates shared library named dlib-shared:
mkdir shared_build # Build directory can be any
cd shared_build
cmake ..
cmake --build . --config Release
make install # Install library for make it acessible for others
For use this library in examples, you need to add definition of dlib library into your examples/CMakeLists.txt before include(../dlib/cmake).
examples/CMakeLists.txt:
...
PROJECT(examples)
add_library(dlib SHARED IMPORTED) # Imported(!) dlib target
set_target_properties(dlib PROPERTIES IMPORTED_LOCATION "<full path to the installed dlib-shared library file>")
# Now it is safe to include other dlib infrustucture - it won't build dlib again.
include(../dlib/cmake)
...

Related

Use CMake binares to build cpp projects without installing CMake

I want to use specific version of CMake 3.19.0 for Ubuntu 14.04 (32-bits) without installing CMake (use only binaries).
I tried to build CMake 3.19.0 on my test machine. It builded and installed successfully. In install_manifest.txt I see lot of files that were installed on my test system.
So, I tried to copy only installed binaries from /usr/local/bin/ (this is default path where CMake binaries were installed) and paste it to another machine that doesn't know about CMake. I paste 3 binaries: cmake, ctest, cpack to /usr/local/bin/.
If I run which cmake it shows path:
/usr/local/bin/cmake
If I run cmake --verison it shows:
CMake Error: Could not find CMAKE_ROOT !!!
CMake has most likely not been installed correctly.
Modules directory not found in
/usr/local/share/cmake-3.19
cmake version 3.19.0
CMake suite maintained and supported by Kitware (kitware.com/cmake).
It looks like CMake needs some modules that I haven't copied yet. I tried to build my cpp project and it shows me:
CMake Error: Could not find CMAKE_ROOT !!!
CMake has most likely not been installed correctly.
Modules directory not found in
/usr/local/share/cmake-3.19
CMake Error: Error executing cmake::LoadCache(). Aborting.
What are the minimum required modules needed for stable building? And where I should copy it?
Just copied builded Modules and Templates directories from cmake-3.19.0 build directory to /usr/local/share/cmake-3.19

How to build TensorFlow Lite C API static library?

I'd like to build the TensorFlow Lite as C API static library (Linux Debian x64). The instructions state the following CMake workflow:
// get the sources
git clone https://github.com/tensorflow/tensorflow.git tensorflow_src
// create a build directory
mkdir tflite_build
cd tflite_build
// build the lib using CMake
cmake ../tensorflow_src/tensorflow/lite/c
cmake --build . -j
However, this builds the shared library libtensorflowlite_c.so.
What would be the recommended way to build static version of the C API lib? Does modifying the CMake config files require expert CMake knowledge or it could be achieved rather easy?
According to the TensorFlow Lite CMakeLists.txt file this should be rather easy. Add -DTFLITE_C_BUILD_SHARED_LIBS:BOOL=OFF to your CMake configuration step. That means, clear your build directory before doing any changes and rerun the CMake commands
cmake -S ../tensorflow_src/tensorflow/lite/c -DTFLITE_C_BUILD_SHARED_LIBS:BOOL=OFF
cmake --build . -j
Be sure to also add to your project a dependency to TF-lite and its dependencies (as mentioned here in its CMakeLists.txt) if they are not header only, as TF-lite does not provide a CMake config package (AFAIK) that otherwise would include these transitive dependencies.

ccmake using cmake version 3.10

I am trying to install the new ITK version which needs a cmake version higher than 3.9
I have just download the cmake 3.10 version.
and when I install it there is no ccmake in the bin folder. usually the new ccmake version is here.
when I want to install ccmake using apt it links it to the 3.5 cmake version.
How can I do to have a ccmake version linked to the 3.10 version of cmake ?
Turning my comment into an answer
You can use the following tutorial to build and install the latest CMake version: How do I install the latest version of cmake from the command line?
But - as for the time of your question - it was lacking the hint to install the curses library/headers first (see here,
you don't get ccmake built and installed without it ). So I had the same problem on my Ubuntu and was able to install and rebuild it with the following steps:
# sudo apt-get install libncurses-dev
# cd ~/temp/cmake-3.10.2
# cmake .
...
# make -j8
...
# sudo make install
...
# ccmake
Usage
ccmake <path-to-source>
ccmake <path-to-existing-build>
Specify a source directory to (re-)generate a build system for it in the
current working directory. Specify an existing build directory to
re-generate its build system.
Run 'ccmake --help' for more information.
The binary ccmake is a target of the make file.
To get all, perform:
./configure
make all
sudo make install
To just get ccmake, perform:
make ccmake

CMake v3.4.1 finds CUDA 7.5 instead of CUDA 8.0 on Ubuntu 16.04

I am trying to install the GPU support for XGBoost but I get the following error when I try to build it with CMake:
CMake Error at /usr/local/share/cmake-3.4/Modules/FindPackageHandleStandardArgs.cmake:148 (message):
Could NOT find CUDA: Found unsuitable version "7.5", but required is at
least "8.0" (found /usr)
I only installed CUDA 8.0 and the environmental variables in .bashrc are specified as:
export PATH=/usr/local/cuda-8.0/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-8.0/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
How can I have CMake detect the right CUDA version?
PATH and LD_LIBRARY_PATH have no effect on cmake, so what you have tried will not work.
If you look at the documentation for find_package it is possible to provide a PATHS argument to tell cmake to look in non-standard locations
Unfortunately, if you look at the source for XGBoost's CMakeLists.txt, you can see it calls find_package(CUDA ...), but doesn't allow the user to provide a PATHS option
if(USE_CUDA)
find_package(CUDA 8.0 REQUIRED)
...
endif()
As such you are left with 2 options:
Edit XGBoost's CMakeLists.txt file, and add PATHS /usr/local/cuda-8.0 to the find_package call
Install cuda-8.0 into a standard location (eg: use /usr/local as your PREFIX, not /usr/local/cuda-8.0)
I ran into a similar issue when trying to install the R version with GPU support. The issue was that I was running the commands from their install guide:
git clone --recursive https://github.com/dmlc/xgboost
cd xgboost
git submodule init
git submodule update
cd R-package
R CMD INSTALL .
and then running the commands for GPU support:
mkdir build
cd build
cmake .. -DUSE_CUDA=ON -DR_LIB=ON
I was able to avoid the issue by running:
git clone --recursive https://github.com/dmlc/xgboost
cd xgboost
mkdir build
cd build
cmake .. -DUSE_CUDA=ON -DR_LIB=ON
make install -j

cmake msys2, wrong install path for library

I'm on windows, tried to install EASTL, it got installed in Program Files instead of compiler's path.
Maybe I should change something in CMakeLists?
Library's CMakeLists: https://github.com/electronicarts/EASTL/blob/master/CMakeLists.txt
Commands I used:
cmake -G"MSYS Makefiles"
make
make install
It's a little tricky because MSYS2's CMake is a native Windows program that only understands Windows paths, and MSYS2 has automatic conversions of paths from POSIX-style to Windows-style that gets in the way sometimes.
These commands should work:
MSYS2_ARG_CONV_EXCL=- cmake . -G"MSYS Makefiles" -DCMAKE_INSTALL_PREFIX=$MSYSTEM_PREFIX
make install DESTDIR=/