在Windows上编译dlib

时间:2016-07-01 03:42:49

标签: c++ visual-c++ mingw gnu dlib

我尝试在Windows上的Qt项目中使用dlib。下载后我立即开始尝试构建我的Qt应用程序,而不构建示例。我还从GnuWin下载了静态库libpng和libjpeg。

.pro文件:

SOURCES += main.cpp\
        mainwindow.cpp \
    C:/Users/user/Downloads/dlib-19.0/dlib-19.0/dlib/all/source.cpp

HEADERS  += mainwindow.h

INCLUDEPATH += "C:\Users\user\Downloads\dlib-19.0\dlib-19.0"
LIBS += -L"C:\Users\user\Downloads\dlib-19.0\dlib-19.0"

INCLUDEPATH += "C:\Users\user\Downloads\dlib-19.0\dlib-19.0\dlib\external\libpng"
INCLUDEPATH += "C:\Users\user\Downloads\dlib-19.0\dlib-19.0\dlib\external\libjpeg"

LIBS += -L"C:\Users\user\Downloads\jpeg-6b-4-lib\lib" -ljpeg

LIBS += -L"C:\Users\user\Downloads\libpng-1.2.37-lib\lib" -lpng

LIBS += -lwinmm -lws2_32 -luser32 -lgdi32 -lcomctl32 -limm32

QMAKE_CXXFLAGS += -std=c++11 -DDLIB_PNG_SUPPORT -DDLIB_JPEG_SUPPORT

代码:

try
{
     array2d<rgb_pixel> img;
     load_image(img, "C:\\Users\\user\\Downloads\\test.jpg");
}
catch (exception& e)
{
    cout << "\nexception thrown!" << endl;
    cout << e.what() << endl;
}

MinGW输出

  

png_loader.cpp:103:对'png_set_longjmp_fn'的未定义引用

     

png_loader.cpp:153:对'png_set_longjmp_fn'的未定义引用

     

save_png.cpp:20:对'png_set_longjmp_fn'的未定义引用

     

save_png.cpp:72:对'png_set_longjmp_fn'的未定义引用

我知道我需要使用libpng 1.4,但我在GnuWin中找不到这个版本,所以我使用了最相关的1.2。

MSVC运行时异常:

  

异常抛出!

     

无法在文件C:\ Users \ user \ Downloads \ test.jpg。

中加载图像      

您必须#define DLIB_JPEG_SUPPORT并链接到libjpeg以读取JPEG   文件。

     

按照http://dlib.net/compile.html上的说明执行此操作。

     

请注意,您必须为您的身份定义DLIB_JPEG_SUPPORT   整个项目。

     

所以不要在一个文件中定义#define。相反,将它添加到   C / C ++ - &gt;预处理器 - &gt;预处理器定义

     Visual Studio的“属性页”窗口中的

字段,以使其生效   你的整个申请。

但是我在.pro文件中设置-DDLIB_JPEG_SUPPORT,与jpeg和png链接,甚至下载到工作文件夹共享库jpeg.dll中。 如何解决这个问题或如何在Windows上的Qt中使用dlib?

UPD: E:\CMake\bin\cmake.exe --build . --config Release之后的错误:

"C:\Users\user\Downloads\dlib-19.0\dlib-19.0\examples\build\ALL_BUILD.vcxproj" (default target) (1) ->
"C:\Users\user\Downloads\dlib-19.0\dlib-19.0\examples\build\3d_point_cloud_ex.vcxproj" (default target)
(3) ->
"C:\Users\user\Downloads\dlib-19.0\dlib-19.0\examples\build\dlib_build\dlib.vcxproj" (default target) (4
) ->
(target ClCompile) ->
  f:\gstreamer\1.0\x86\include\jmorecfg.h(211): error C2371: 'boolean': redefinition; different basic types [C:\Users\user
  \Downloads\dlib-19.0\dlib-19.0\examples\build\dlib_build\dlib.vcxproj]
  C:\Program Files (x86)\Windows Kits\8.1\Include\shared\basetsd.h(72): error C2371: 'INT32': redefinition; different b
asic types [C:\Users\user\Downloads\dlib-19.0\dlib-19.0\examples\build\dlib_build\dlib.vcxproj]
  c:\program files (x86)\windows kits\8.1\include\shared\rpcndr.h(178): error C2371: 'boolean': redefinition; different
 basic types [C:\Users\user\Downloads\dlib-19.0\dlib-19.0\examples\build\dlib_build\dlib.vcxproj]
  c:\program files (x86)\windows kits\8.1\include\shared\wtypesbase.h(493): error C2371: 'BOOLEAN': redefinition; diffe
rent basic types [C:\Users\user\Downloads\dlib-19.0\dlib-19.0\examples\build\dlib_build\dlib.vcxproj]

UPD2:

"C:\Users\user\Downloads\dlib-19.0\dlib-19.0\examples\build\ALL_BUILD.vcxproj" (default target) (1) ->
"C:\Users\user\Downloads\dlib-19.0\dlib-19.0\examples\build\3d_point_cloud_ex.vcxproj" (default target) (3) ->
"C:\Users\user\Downloads\dlib-19.0\dlib-19.0\examples\build\dlib_build\dlib.vcxproj" (default target) (4) ->
(target ClCompile) ->
  C:\Users\user\Downloads\dlib-19.0\dlib-19.0\dlib\image_loader\jpeg_loader.cpp(17): fatal error C1083: Cannot open include file: 'jpeglib.h': No such file or directory [C:\U
sers\user\Downloads\dlib-19.0\dlib-19.0\examples\build\dlib_build\dlib.vcxproj]
  C:\Users\user\Downloads\dlib-19.0\dlib-19.0\dlib\image_saver\save_jpeg.cpp(16): fatal error C1083: Cannot open include file: 'jpeglib.h': No such file or directory [C:\User
s\user\Downloads\dlib-19.0\dlib-19.0\examples\build\dlib_build\dlib.vcxproj]

UPD3:

.pro文件:

INCLUDEPATH += "E:\dlib_build\include"
LIBS += -L"E:\dlib_build"
LIBS += -ldlib
QMAKE_CXXFLAGS_RELEASE += /arch:AVX
QMAKE_CXXFLAGS += -std=c++11 -DDLIB_PNG_SUPPORT -DDLIB_JPEG_SUPPORT

MSVC 2015 64位版本输出:

mainwindow.obj : error LNK2019: unresolved external symbol "public: __cdecl dlib::entropy_decoder_kernel_2::entropy_decoder_kernel_2(void)" (??0entrop...
mainwindow.obj : error LNK2019: unresolved external symbol "public: virtual __cdecl dlib::entropy_decoder_kernel_2::~entropy_decoder_kernel_2(void)" (...
mainwindow.obj : error LNK2019: unresolved external symbol "public: void __cdecl dlib::entropy_decoder_kernel_2::set_stream(class std::basic_istream<c...
mainwindow.obj : error LNK2019: unresolved external symbol "public: void __cdecl dlib::entropy_decoder_kernel_2::decode(unsigned int,unsigned int)" (?...
mainwindow.obj : error LNK2019: unresolved external symbol "public: unsigned int __cdecl dlib::entropy_decoder_kernel_2::get_target(unsigned int)" (?g...
mainwindow.obj : error LNK2019: unresolved external symbol "public: __cdecl dlib::png_loader::png_loader(class std::basic_string<char,struct std::char...
mainwindow.obj : error LNK2019: unresolved external symbol "public: __cdecl dlib::png_loader::~png_loader(void)" (??1png_loader@dlib@@QEAA@XZ) referen...
mainwindow.obj : error LNK2019: unresolved external symbol "public: bool __cdecl dlib::png_loader::is_gray(void)const " (?is_gray@png_loader@dlib@@QEB...
mainwindow.obj : error LNK2019: unresolved external symbol "public: bool __cdecl dlib::png_loader::is_graya(void)const " (?is_graya@png_loader@dlib@@Q...
mainwindow.obj : error LNK2019: unresolved external symbol "public: bool __cdecl dlib::png_loader::is_rgb(void)const " (?is_rgb@png_loader@dlib@@QEBA_...
mainwindow.obj : error LNK2019: unresolved external symbol "public: bool __cdecl dlib::png_loader::is_rgba(void)const " (?is_rgba@png_loader@dlib@@QEB...
mainwindow.obj : error LNK2019: unresolved external symbol "private: unsigned char const * __cdecl dlib::png_loader::get_row(unsigned int)const " (?ge...
mainwindow.obj : error LNK2019: unresolved external symbol "public: __cdecl dlib::jpeg_loader::jpeg_loader(class std::basic_string<char,struct std::ch...
mainwindow.obj : error LNK2019: unresolved external symbol "public: bool __cdecl dlib::jpeg_loader::is_gray(void)const " (?is_gray@jpeg_loader@dlib@@Q...
mainwindow.obj : error LNK2019: unresolved external symbol USER_ERROR__missing_dlib_all_source_cpp_file__OR__inconsistent_use_of_DEBUG_or_ENABLE_ASSER...
E:\dlib_build\lib\dlib.lib : warning LNK4272: library machine type 'X86' conflicts with target machine type 'x64'
release\dlibWindows.exe : fatal error LNK1120: 15 unresolved externals
jom: F:\Projects\QtProjects\build-dlibWindows-Desktop_Qt_5_6_0_MSVC2015_64bit-Release\Makefile.Release [release\dlibWindows.exe] Error 1120

UPD4:

.pro文件:

INCLUDEPATH += "E:\dlib_build\include"
LIBS += -L"E:\dlib_build"
LIBS += -ldlib
QMAKE_CXXFLAGS_RELEASE += /arch:AVX

代码:

try
{
    //or even just one line "frontal_face_detector detector = get_frontal_face_detector();"
    array2d<rgb_pixel> img;
    load_image(img, "C:/Users/user/Downloads/test.jpg");
}
catch (exception& e)
{
    cout << "\nexception thrown!" << endl;
    cout << e.what() << endl;
}

发布输出:

  

退出代码255

1 个答案:

答案 0 :(得分:1)

您已通过将 all / source.cpp 包含在项目中来选择编译方式。这是可能的,但我认为不是如何使用dlib的最佳方法。 因此,如果您想在项目中使用 all / source.cpp ,请按照此处的说明进行操作:http://dlib.net/compile.html

  

使用Visual Studio在Windows上进行编译

     

您需要做的就是创建一个空的控制台项目。然后将dlib / all / source.cpp添加到它并将包含dlib文件夹的文件夹添加到#include搜索路径。然后,您可以通过将任何示例程序添加到项目中来编译它。

     

再次注意,如果你在libjpeg和libpng中链接,dlib将只能使用jpeg和png文件。 在Visual Studio中,最简单的方法是将dlib / external文件夹中的所有源文件添加到项目中,并定义DLIB_PNG_SUPPORT和DLIB_JPEG_SUPPORT预处理程序指令。如果您不知道如何配置Visual Studio,那么您应该使用如上所示的CMake,因为它将自动处理所有内容。

添加dlib / external文件夹 - 您可以从项目中删除libjpeg和libpng。

但正如我所说 - 这不是编译dlib的最佳方法。 Dlib旨在与CMAKE一起使用,最好的方法是使用CMAKE构建它。 为什么这种方式更好:

  1. 每次项目更改时都不需要重建dlib
  2. 可能使用CUDA代码(现在不是 - 但稍后将发布CUDA 8和VS update 4)
  3. 这里的第一步(安装CMAKE之后)应检查Dlib是否有效。根据Dlib的说明,最好的方法是构建示例并运行它们(来自dlib的文件夹):

    cd examples
    mkdir build
    cd build
    cmake ..
    cmake --build . --config Release
    

    运行 cmake .. 时,您可以指定编译器版本,例如:

    cmake .. -G"Visual Studio 14 2015 Win64" 
    

    使用Visual Studio 2015 x64构建示例

    在测试示例之后,它是构建Dlib并开始在实际项目中使用它的好时机。是的,再次构建是由CMAKE(来自dlib文件夹)完成的:

    mkdir build
    cd build
    cmake .. -G"Visual Studio 14 2015 Win64" -DCMAKE_INSTALL_PREFIX=C:\Users\user\dlib
    cmake --build . --config Release --target install
    

    构建过程与构建示例略有不同:

    1. CMAKE_INSTALL_PREFIX = C:\ Users \ user \ dlib - 已编译的dlib将安装到C:\ Users \ user \ dlib
    2. - 目标安装表示我们正在安装dlib
    3. 构建过程完成后 - 您可以从C:\ Users \ user \ dlib \ lib中自由使用dlib.lib(Dlib的静态版本)。如果您将CMAKE用于您的项目 - 您将需要添加-DCMAKE_PREFIX_PATH = c:\ Users \ user \ dlib。但是现在你正在尝试使用Qt Creator(QMake)进行编译。这是你的.pro文件看起来像(最小可能的例子):

      SOURCES += face_detection_ex.cpp
      INCLUDEPATH += "C:\Users\user\dlib\include"
      LIBS += -L"C:\Users\user\dlib"
      LIBS += -ldlib
      QMAKE_CXXFLAGS_RELEASE += /arch:AVX
      

      此项目不会在调试模式构建中编译和运行,因为我们在构建过程中使用了 - config Release 。但发布模式运行速度非常快

      请注意

      1. Visual Studio需要为Release和Debug版本安装单独的dlib。而Dlib的Debug构建将非常缓慢
      2. 添加/ arch:AVX到编译器标志(MSVC)将使Dlib运行得更快
      3. 在Cuda 8和MSVC 2015更新4发布之前,DNN模块将无法在MSVC下运行
相关问题