Dlib C++ Library - How To Compile
Dlib C++ Library - How To Compile
Custom Search
The Library
Algorithms
How to compile
API Wrappers
Bayesian Nets
Compression Compiling C++ Example Programs on Any Operating System Using CMake
Containers
Graph Tools The best way to compile a program that uses dlib is to use CMake. For example, the following commands will compile the
Image Processing example programs on any operating system:
Linear Algebra
Machine Learning
Metaprogramming cd examples
Miscellaneous mkdir build
Networking cd build
Optimization
cmake ..
Parsing
cmake --build . --config Release
Help/Info
Dlib Blog Note that you also need to have a C++11 compiler installed on your system. There are free C++11 compilers for most operating
Examples: C++ systems. For example, Visual Studio is free on Windows and GCC is free and works well on Mac OS X and Linux systems. If
Examples: Python you have multiple compilers/IDEs installed then you can tell CMake which one you want it to use via the -G option.
FAQ
Home The examples/CMakeLists.txt file tells CMake how to build the examples. You can create your own projects by starting with
How to compile
How to contribute this file and editing it however you like. You can also perform additional configuration of a cmake project using the cmake-gui or
Index ccmake tool. For example, if you are using dlib's face detector then you should turn on either SSE4 or AVX instructions since
Introduction this makes it run much faster (also see this FAQ).
License
Python API Finally, note that when using Visual Studio, CMake will by default generate a 32bit executable. This means the programs you
Suggested Books compile will only be able to use 2GB of RAM. To avoid this, you need to tell CMake to generate a 64bit executable. You do this
Who uses dlib? by using a command like
Current Release
Change Log cmake -G "Visual Studio 14 2015 Win64" ..
Release Notes
http://dlib.net/compile.html 1/5
24/08/2017 dlib C++ Library - How to compile
instead of
Download dlib
ver.19.4 cmake ..
You can see the list of valid arguments to -G by running cmake with no options.
Alternatively, if you want to add more python bindings to dlib's python interface then you probably want to avoid the setup.py
file and work directly using CMake. In particular, dlib's python API is built by the CMake project in the tools/python folder. You
build this project using the usual CMake commands and when compiled it outputs the dlib shared library that defines the python
API for dlib.
An example makefile that uses this library can be found here: dlib/test/makefile. It is the makefile used to build the regression
test suite for this library.
Again, note that you should not add the dlib folder itself to your compiler's include path. Doing so will cause the build to fail
because of name collisions (e.g. dlib/string.h with string.h from the standard library). Instead you should add the folder that
http://dlib.net/compile.html 2/5
24/08/2017 dlib C++ Library - How to compile
contains the dlib folder to your include search path and then use include statements of the form #include <dlib/queue.h> . This
will ensure that everything builds correctly.
Note also that if you want to work with jpeg/png/gif files using dlib then you will need to link your program with libjpeg, libpng,
and/or libgif. You also need to tell dlib about this by defining the DLIB_JPEG_SUPPORT, DLIB_PNG_SUPPORT, and
DLIB_GIF_SUPPORT preprocessor directives. How you "link to libjpeg/libpng/libgif" varies from platform to platform. On
UNIX machines you usually just add a -ljpeg, -lpng, or -lgif switch to your compiler (after installing the libraries). On windows
it's less well defined. So dlib comes with a copy of libjpeg and libpng in the dlib/external folder so you can statically compile
them into your application if no system wide version is available on your machine. If all this talk about linking is confusing to
you then just use CMake. It will set this all up for you.
Dlib is also capable of using any optimized BLAS or LAPACK libraries that are installed on your system. Linking to these
libraries will make many things run faster. To do this you define the DLIB_USE_BLAS and/or DLIB_USE_LAPACK
preprocessor directives and then link your program with whatever BLAS or LAPACK libraries you have. If you use CMake it
will set this up automatically.
From within the examples folder, you can compile nearly all of the examples with a single command like so:
Note that not all examples require this much work. For example, the svm_ex.cpp example can be compiled with just:
On non-Linux systems like Solaris, you might have to link to other libraries. For example, I have seen systems where it was also
necessary to supply -lnsl or -lsocket options to g++. Additionally, the X11 development library isn't installed on Ubuntu by
default. So if you require it and are using Ubuntu you can install it by typing:
http://dlib.net/compile.html 3/5
24/08/2017 dlib C++ Library - How to compile
The commands for gcc on windows are the same as above but you may also have to link (via the -l option) to the following
libraries: gdi32, comctl32, user32, winmm, ws2_32, or imm32.
All you need to do is create an empty console project. Then add dlib/all/source.cpp to it and add the folder containing the dlib
folder to the #include search path. Then you can compile any example program by adding it to your project.
Again, note that dlib will only be able to work with jpeg and png files if you link in libjpeg and libpng. In Visual Studio, the
easiest way to do this is to add all the libjpeg, libpng, and zlib source files in the dlib/external folder into your project and also
define the DLIB_PNG_SUPPORT and DLIB_JPEG_SUPPORT preprocessor directives. If you don't know how to configure
Visual Studio then you should use CMake as shown above since it will take care of everything automatically.
#define ENABLE_ASSERTS
Defining this directive causes all the DLIB_ASSERT macros to be active. If you are using Visual Studio or CMake then
ENABLE_ASSERTS will be automatically enabled for you when you compile in debug mode. However, if you are using a
different build system then you might have to manually enable it if you want to turn the asserts on.
#define DLIB_ISO_CPP_ONLY
This is a #define directive that you can set to cause the library to exclude all non ISO C++ code (The things in the API wrappers
section and any objects that depend on those wrappers). This is useful if you are trying to build on a system that isn't fully
http://dlib.net/compile.html 4/5
24/08/2017 dlib C++ Library - How to compile
supported by the library or if you just decide you don't want any of that stuff compiled into your program for your own reasons.
#define DLIB_NO_GUI_SUPPORT
This is just like the DLIB_ISO_CPP_ONLY option except that it excludes only the GUI part of the library. An example of when
you might want to use this would be if you don't need GUI support and you are building on a UNIX platform that doesn't have
the X11 headers installed.
#define NO_MAKEFILE
This preprocessor directive causes the dlib headers to pull in all the code that would normally be built in dlib/all/source.cpp. Thus
if you #define NO_MAKEFILE you won't have to add dlib/all/source.cpp to your project. The only time this is useful is when
your project consists of a single translation unit (i.e. a single cpp file). In this instance NO_MAKEFILE allows you to easily
build your project on the command line by saying something like g++ -DNO_MAKEFILE project.cpp . But again, this is only for
single cpp file projects. If you use NO_MAKEFILE with projects that contain more than one cpp file you will get linker errors
about multiply defined symbols.
Also note that if you use this macro then the stack trace functionality in the library will be disabled.
If you use dlib to create your threads then you receive the benefit of the dlib dynamic thread pool (Note that the
dlib::thread_pool object is something else unrelated to this so don't confuse the two). This pool enables dlib to spawn new
threads very rapidly since it draws threads back out of its thread pool when the pool isn't empty.
Thus, when a thread that was created by dlib ends it actually goes back into the dlib thread pool and waits
DLIB_THREAD_POOL_TIMEOUT milliseconds before totally terminating and releasing its resources back to the operating
Last Modified: system. The default timeout used by this library is 30,000 milliseconds (30 seconds). You may however change this to whatever
Oct 09, 2016
you like by defining DLIB_THREAD_POOL_TIMEOUT to some new value.
http://dlib.net/compile.html 5/5