最近需要使用dlib库,因此在自己的电脑上安装了该库,本文主要记录编译安装的过程。首先给出我的软件环境:
dlib-19.19
├─build_mingw64
└─sources
├─dlib
├─docs
├─examples
├─python_examples
└─tools
......
官网提供了在c++程序中使用dlib时如何编译的说明:Using dlib from C++。这里因为要修改一些配置,所以我使用CMake的GUI生成编译dlib的Makefile。具体如下:
工具链:
所在目录:
选择完毕后点击finish
,之后会有一个配置过程,这时CMake会检查dlib依赖的一些库是否存在,如果不存在,那么相关配置就会被设置为OFF
。比如,我电脑上的显卡不支持CUDA,因此配置时就会输出如下信息:
Could NOT find CUDA (missing: CUDA_TOOLKIT_ROOT_DIR CUDA_NVCC_EXECUTABLE CUDA_INCLUDE_DIRS CUDA_CUDART_LIBRARY) (Required is at least version "7.5")
相应的配置项被设置为OFF:
在最终生成Makefile之前,可以根据自己的需要修改配置项,比如我使能了以下两个配置项:
使能断言,有助于调试:
使能AVX指令支持,有助于提升程序速度:
修改配置项后,需要再次点击Configure,之后各个配置项变成白色。
这一步很简单,点击Generate即可。
有了Makefile就可以进行编译了,可以进入到build_mingw64
目录,使用命令行进行编译:
D:\Libraries\dlib-19.19\build_mingw64>mingw32-make -j 4
执行如下命令:
D:\Libraries\dlib-19.19\build_mingw64>mingw32-make install
之后,安装目录下就会出现lib
文件夹,存有编译出来的库文件;include
文件夹,存有头文件。
值得一提的是,如果编译动态库,还需要将库文件所在目录添加到系统环境变量(PATH),这样在链接或者程序加载运行时才不会出现找不到动态库的情况。当然,把动态库直接拷贝到项目里也是一种解决办法。
在CLion中创建一个项目,然后将dlib自带的例子中的3d_point_cloud_ex.cpp
源码拷贝到刚刚建立的工程的main.c
中,内容如下:
#include
#include
#include
#include
using namespace dlib;
using namespace std;
// ----------------------------------------------------------------------------------------
int main()
{
// Let's make a point cloud that looks like a 3D spiral.
std::vector<perspective_window::overlay_dot> points;
dlib::rand rnd;
for (double i = 0; i < 20; i+=0.001)
{
// Get a point on a spiral
dlib::vector<double> val(sin(i),cos(i),i/4);
// Now add some random noise to it
dlib::vector<double> temp(rnd.get_random_gaussian(),
rnd.get_random_gaussian(),
rnd.get_random_gaussian());
val += temp/20;
// Pick a color based on how far we are along the spiral
rgb_pixel color = colormap_jet(i,0,20);
// And add the point to the list of points we will display
points.push_back(perspective_window::overlay_dot(val, color));
}
// Now finally display the point cloud.
perspective_window win;
win.set_title("perspective_window 3D point cloud");
win.add_overlay(points);
win.wait_until_closed();
}
这时候直接编译肯定是无法通过的,我们还需要指出头文件和dlib库文件的位置,即在CLion项目的CMakeLists.txt
中添加如下内容:
include_directories(D:/Libraries/dlib-19.19/build_mingw64/include)
target_link_libraries(ProjectName D:/Libraries/dlib-19.19/build_mingw64/lib/libdlib.a)
可见,dlib被编译为了静态库,这也带来了一些问题。在添加完上述语句之后,仍然会出现链接错误:
D:/Libraries/dlib-19.19/build_mingw64/lib/libdlib.a(gui_core_kernel_1.cpp.obj):gui_core_kernel_1.cpp:(.text+0xb9b): undefined reference to `ImmGetContext'
D:/Libraries/dlib-19.19/build_mingw64/lib/libdlib.a(gui_core_kernel_1.cpp.obj):gui_core_kernel_1.cpp:(.text+0xbbb): undefined reference to `ImmSetCompositionWindow'
D:/Libraries/dlib-19.19/build_mingw64/lib/libdlib.a(gui_core_kernel_1.cpp.obj):gui_core_kernel_1.cpp:(.text+0xbc7): undefined reference to `ImmReleaseContext'
D:/Libraries/dlib-19.19/build_mingw64/lib/libdlib.a(gui_core_kernel_1.cpp.obj):gui_core_kernel_1.cpp:(.text+0x3b1a): undefined reference to `__imp__TrackMouseEvent'
D:/Libraries/dlib-19.19/build_mingw64/lib/libdlib.a(gui_core_kernel_1.cpp.obj):gui_core_kernel_1.cpp:(.text+0x3bc5): undefined reference to `ImmGetContext'
D:/Libraries/dlib-19.19/build_mingw64/lib/libdlib.a(gui_core_kernel_1.cpp.obj):gui_core_kernel_1.cpp:(.text+0x3bed): undefined reference to `ImmGetCompositionStringW'
D:/Libraries/dlib-19.19/build_mingw64/lib/libdlib.a(gui_core_kernel_1.cpp.obj):gui_core_kernel_1.cpp:(.text+0x3c35): undefined reference to `ImmGetCompositionStringW'
D:/Libraries/dlib-19.19/build_mingw64/lib/libdlib.a(gui_core_kernel_1.cpp.obj):gui_core_kernel_1.cpp:(.text+0x3ced): undefined reference to `ImmReleaseContext'
D:/Libraries/dlib-19.19/build_mingw64/lib/libdlib.a(misc_api_kernel_1.cpp.obj):misc_api_kernel_1.cpp:(.text+0x68a): undefined reference to `__imp_timeGetTime'
这是因为libdlib.a
依赖了一些其他的库,从错误信息来看这是一些windows的库。当libdlib.a作为静态库被链接时,我们要自行根据其依赖关系显式指出那些被libdlib.a依赖的库(除非那些库已经出现在编译器的默认链接选项里)。简单点说,自己写Makefile的话,要添加-l选项
;用CMake则是添加如下语句:
target_link_libraries(ProjectName imm32 comctl32 winmm)
最后,我们在编译dlib的时候选择的是Release版(运行速度较Debug更快,参考:Why is dlib slow?),因此CLion工程也要相应的设置为Release。
[1] dlib官方文档
[2] CMake官方文档