最近的工作中用到了dlib这个库,该库是一个机器学习的开源库,使用起来很方便,直接包含头文件即可,并且不依赖于其他库(自带图像编解码库源码)。不过由于是开源的,所以bug多少有一些,我在example编译和使用时就遇到了一些问题,总结如下:

1.example编译

按照官网的指导,下载最新的18.18版本dlib,然后解压缩到某个路径。dlib库不用单独编译,只需要编译example,即可看到效果。根据README.txt文件中的步骤,在exmaple文件夹下,执行下面命令即可,

mkdir build

cd build

cmake .. 

cmake –build .

但是一般在最后一步会遇到一些问题,导致编译失败,解决方法如下:

1.在dlib/CMakeLists.txt中 project(dlib) 的下一行添加set(Boost_USE_STATIC_LIBS ON)

2.把 if (DLIB_PNG_SUPPORT) (227行左右)这一行以下的这些代码进行注释,其余不变

         # try to find libpng 
         #find_package(PNG QUIET)
         # Make sure there isn't something wrong with the version of LIBPNG
         # installed on this system.  
         #if (PNG_FOUND)
            #set(CMAKE_REQUIRED_LIBRARIES ${PNG_LIBRARY})
            #CHECK_FUNCTION_EXISTS(png_create_read_struct LIBPNG_IS_GOOD)
         #endif()
         #if (PNG_FOUND AND LIBPNG_IS_GOOD)
            #include_directories(${PNG_INCLUDE_DIR})
            #set (dlib_needed_libraries ${dlib_needed_libraries} ${PNG_LIBRARY})
         #else()
            # If we can't find libpng then statically compile it in.

3.把 if (DLIB_JPEG_SUPPORT) (280行左右)这一行以下的这些代码进行注释,其余不变

         # try to find libjpeg 
         #find_package(JPEG QUIET)
         # Make sure there isn't something wrong with the version of libjpeg 
         # installed on this system.  Also don't use the installed libjpeg
         # if this is an APPLE system because apparently it's broken (as of 2015/01/01).
         #if (JPEG_FOUND)
            #set(CMAKE_REQUIRED_LIBRARIES ${JPEG_LIBRARY})
            #CHECK_FUNCTION_EXISTS(jpeg_read_header LIBJPEG_IS_GOOD)
         #endif()
         #if (JPEG_FOUND AND LIBJPEG_IS_GOOD AND NOT APPLE)
            #include_directories(${JPEG_INCLUDE_DIR})
            #set (dlib_needed_libraries ${dlib_needed_libraries} ${JPEG_LIBRARY})
         #else()
            # If we can't find libjpeg then statically compile it in.

这样应该就可以编译通过了,编译出的二进制文件应该也可以执行,如果执行时依然报错找不到jpeg或png库,再确认一下CMakeLists文件是否按照上述修改。