Create a shared library for dlib

2019-04-01 04:05发布

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?

2条回答
干净又极端
2楼-- · 2019-04-01 04:22

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)
...
查看更多
The star\"
3楼-- · 2019-04-01 04:26

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.

查看更多
登录 后发表回答