c++ - 在 Windows 上编译 dlib

标签 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: undefined reference to 'png_set_longjmp_fn'

png_loader.cpp:153: undefined reference to 'png_set_longjmp_fn'

save_png.cpp:20: undefined reference to 'png_set_longjmp_fn'

save_png.cpp:72: undefined reference to 'png_set_longjmp_fn'

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

MSVC 运行时异常:

exception thrown!

Unable to load image in file C:\Users\user\Downloads\test.jpg.

You must #define DLIB_JPEG_SUPPORT and link to libjpeg to read JPEG files.

Do this by following the instructions at http://dlib.net/compile.html.

Note that you must cause DLIB_JPEG_SUPPORT to be defined for your entire project.

So don't #define it in one file. Instead, add it to the C/C++->Preprocessor->Preprocessor Definitions

field in Visual Studio's Property Pages window so it takes effect for your entire application.

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

更新: E:\CMake\bin\cmake.exe --build 之后的错误。 --配置发布:

"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;
}

发布输出:

Exited with code 255

最佳答案

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

Compiling on Windows Using Visual Studio

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 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.

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

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

  1. 无需在每次项目更改时重新构建 dlib
  2. 可能使用 CUDA 代码(不是现在 - 但稍后将发布 CUDA 8 和 VS 更新 4)

这里的第一步(安装 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. --target install 表示我们正在安装dlib

构建过程完成后 - 您可以自由使用 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,因此该项目不会在Debug 模式下编译和运行。但是Release模式会跑的很快

请注意

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

关于c++ - 在 Windows 上编译 dlib,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38136699/

相关文章:

c++ - 从数组初始化 QVector

c++ - LPCSTR、LPCTSTR 和 LPTSTR

c++ - 使用宏 _BIND_TO_CURRENT_VCLIBS_VERSION 是否有任何副作用?

c - Borland vs. MingW/GCC 编译速度..

c++ - 如何将 Eigen C++ 3.2.0 在线文档归档到单个 mht 中?

c++ - 令人晕眩的游戏关卡表示(格式)

visual-c++ - 为什么输入字母或数字后C++程序崩溃?

使用 MinGW 编译的 C++ Hello World 程序崩溃并显示 "Illegal Argument"

windows - 如何在 Windows 10 上安装 MinGW 64

c++ - 发现问题: Visual Studio CRT library : FLS_GETVALUE