9

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?

ManiAm
  • 1,759
  • 5
  • 24
  • 43

2 Answers2

24

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.

Davis King
  • 4,731
  • 1
  • 25
  • 26
  • Thanks Davis! The last line does not work for me. It seems that there is no rule to make target install. I am running the `make install` in the build folder. – ManiAm Nov 30 '15 at 12:03
  • This doesn't create any .so files anywhere on Linux (Ubuntu 16.04) for me. Just puts a .a file in /usr/local/lib/. `locate libdlib` just returns: `/usr/local/lib/libdlib.a` – ndtreviv Sep 18 '18 at 11:45
  • 1
    Yeah. These instructions are dated. I just fixed them. – Davis King Sep 18 '18 at 11:53
  • @DavisKing How to link an example file say face_recognition_ex.cpp? I created the -o file using `g++ -c -I../ -std=c++11 dnn_face_recognition_ex.cpp` Then to link, I tried `g++ dnn_face_recognition_ex.o -o dnn_face_recognition_ex -ldlib` but get this error `ld: library not found for -ldlib` – Sleeba Paul Nov 09 '18 at 07:24
0

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)
...
Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
  • This isn't how you compile a dlib shared library. – Davis King Nov 30 '15 at 11:43
  • Except missed `make install`(just forgot about it, thinking it goes without saying), what is wrong with my answer? – Tsyvarev Nov 30 '15 at 11:50
  • The add_library statement is not going to work in a sensible way. – Davis King Nov 30 '15 at 11:52
  • `The add_library statement is not going to work in a sensible way` - Why? `add_library(... IMPORTED)` is one of the common ways for use 3d-party libraries. – Tsyvarev Nov 30 '15 at 11:55
  • If you want to use cmake to link with an installed library use cmake's find_package command. Or at the very least don't include dlib/cmake. That's for static builds. – Davis King Nov 30 '15 at 12:10
  • `That's for static builds.` - Why `dlib/cmake` is for static build only? I haven't find any evidence of that by looking into code. The reason of using `add_library(dlib SHARED IMPORTED)` is exactly for being combined with `include(dlib/cmake)`, which does useful things aside from the linking. `find_package()` approach will not work in this case, as it declares `dlib` target as *static*, so example will be again linked statically. – Tsyvarev Nov 30 '15 at 12:17