在Windows上编译dlib
Compiling dlib on Windows
我尝试在Windows上的Qt项目中使用dlib。下载后,我立即开始尝试构建我的Qt应用程序,没有构建示例。我还从GnuWin下载了静态库libpng和libjpeg。
。支持文件:
SOURCES += main.cpp
mainwindow.cpp
C:/Users/user/Downloads/dlib-19.0/dlib-19.0/dlib/all/source.cpp
HEADERS += mainwindow.h
INCLUDEPATH += "C:UsersuserDownloadsdlib-19.0dlib-19.0"
LIBS += -L"C:UsersuserDownloadsdlib-19.0dlib-19.0"
INCLUDEPATH += "C:UsersuserDownloadsdlib-19.0dlib-19.0dlibexternallibpng"
INCLUDEPATH += "C:UsersuserDownloadsdlib-19.0dlib-19.0dlibexternallibjpeg"
LIBS += -L"C:UsersuserDownloadsjpeg-6b-4-liblib" -ljpeg
LIBS += -L"C:UsersuserDownloadslibpng-1.2.37-liblib" -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_pngg .cpp:20:未定义对png_set_longjmp_fn的引用
save_png.cpp:72:未定义对png_set_longjmp_fn的引用
我知道我需要使用libpng 1.4,但我在GnuWin中找不到这个版本,所以我使用了最相关的1.2。
MSVC运行时异常:抛出异常!
无法加载文件C:UsersuserDownloadstest.jpg.
你必须#define DLIB_JPEG_SUPPORT并链接到libjpeg来读取JPEG文件。
按照http://dlib.net/compile.html上的说明操作。
注意,必须使DLIB_JPEG_SUPPORT为您的整个项目。
所以不要在一个文件中定义它。相反,将它添加到C/c++ ->预处理程序->预处理器定义
字段在Visual Studio的属性页窗口中生效您的整个应用程序。
但是我在我的。pro文件中设置了-DDLIB_JPEG_SUPPORT
,链接到jpeg和png,甚至在工作文件夹中下载了共享库jpeg.dll。我如何解决这个问题,或者我如何在Windows上的Qt中使用dlib ?
乌利希期刊指南:E:CMakebincmake.exe --build . --config Release
后的错误:
"C:UsersuserDownloadsdlib-19.0dlib-19.0examplesbuildALL_BUILD.vcxproj" (default target) (1) ->
"C:UsersuserDownloadsdlib-19.0dlib-19.0examplesbuild3d_point_cloud_ex.vcxproj" (default target)
(3) ->
"C:UsersuserDownloadsdlib-19.0dlib-19.0examplesbuilddlib_builddlib.vcxproj" (default target) (4
) ->
(target ClCompile) ->
f:gstreamer1.0x86includejmorecfg.h(211): error C2371: 'boolean': redefinition; different basic types [C:Usersuser
Downloadsdlib-19.0dlib-19.0examplesbuilddlib_builddlib.vcxproj]
C:Program Files (x86)Windows Kits8.1Includesharedbasetsd.h(72): error C2371: 'INT32': redefinition; different b
asic types [C:UsersuserDownloadsdlib-19.0dlib-19.0examplesbuilddlib_builddlib.vcxproj]
c:program files (x86)windows kits8.1includesharedrpcndr.h(178): error C2371: 'boolean': redefinition; different
basic types [C:UsersuserDownloadsdlib-19.0dlib-19.0examplesbuilddlib_builddlib.vcxproj]
c:program files (x86)windows kits8.1includesharedwtypesbase.h(493): error C2371: 'BOOLEAN': redefinition; diffe
rent basic types [C:UsersuserDownloadsdlib-19.0dlib-19.0examplesbuilddlib_builddlib.vcxproj]
UPD2:
"C:UsersuserDownloadsdlib-19.0dlib-19.0examplesbuildALL_BUILD.vcxproj" (default target) (1) ->
"C:UsersuserDownloadsdlib-19.0dlib-19.0examplesbuild3d_point_cloud_ex.vcxproj" (default target) (3) ->
"C:UsersuserDownloadsdlib-19.0dlib-19.0examplesbuilddlib_builddlib.vcxproj" (default target) (4) ->
(target ClCompile) ->
C:UsersuserDownloadsdlib-19.0dlib-19.0dlibimage_loaderjpeg_loader.cpp(17): fatal error C1083: Cannot open include file: 'jpeglib.h': No such file or directory [C:U
sersuserDownloadsdlib-19.0dlib-19.0examplesbuilddlib_builddlib.vcxproj]
C:UsersuserDownloadsdlib-19.0dlib-19.0dlibimage_saversave_jpeg.cpp(16): fatal error C1083: Cannot open include file: 'jpeglib.h': No such file or directory [C:User
suserDownloadsdlib-19.0dlib-19.0examplesbuilddlib_builddlib.vcxproj]
UPD3:
。支持文件:
INCLUDEPATH += "E:dlib_buildinclude"
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 64bit发布输出:
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_buildlibdlib.lib : warning LNK4272: library machine type 'X86' conflicts with target machine type 'x64'
releasedlibWindows.exe : fatal error LNK1120: 15 unresolved externals
jom: F:ProjectsQtProjectsbuild-dlibWindows-Desktop_Qt_5_6_0_MSVC2015_64bit-ReleaseMakefile.Release [releasedlibWindows.exe] Error 1120
UPD4:
。支持文件:
INCLUDEPATH += "E:dlib_buildinclude"
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
您选择的编译方式是将all/source.cpp包含到您的项目中。这是可能的,但我认为不是最好的方式如何使用dlib。所以,如果你想在你的项目中使用all/source.cpp,请遵循这里的说明:http://dlib.net/compile.html
使用Visual Studio在Windows上编译
您所需要做的就是创建一个空的控制台项目。然后在其中添加dlib/all/source.cpp,并将包含dlib文件夹的文件夹添加到#include搜索路径中。然后,您可以通过将其添加到项目中来编译任何示例程序。
再次注意,dlib只能在链接libjpeg和libpng文件时使用jpeg和png文件。在Visual Studio中,最简单的方法是将dlib/external文件夹中的所有源文件添加到项目中,并定义DLIB_PNG_SUPPORT和DLIB_JPEG_SUPPORT预处理器指令。如果你不知道如何配置Visual Studio,那么你应该使用如上所示的CMake,因为它会自动处理一切。
和添加dlib/外部文件夹-你可以从你的项目中删除libjpeg和libpng。
但正如我所说的-这不是编译dlib的最佳方式。Dlib被设计为与CMAKE一起使用,最好的方法是使用CMAKE来构建它。为什么这样更好:
- 不需要在每次项目更改时重新构建dlib
- 可能使用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:Usersuserdlib
cmake --build . --config Release --target install
构建过程与构建示例略有不同:
- CMAKE_INSTALL_PREFIX=C:Usersuserdlib -编译后的dlib将安装到C:Usersuserdlib
- ——target install表示我们正在安装dlib
构建过程完成后-您可以自由使用dlib。C:Usersuserdliblib. lib (Dlib的静态版本)如果您将在项目中使用CMAKE -您需要添加- dcmake_prefix_path =c:Usersuserdlib。但是现在你正在尝试用Qt Creator (QMake)编译。这是你的。pro文件应该是这样的(最小可能的例子):
SOURCES += face_detection_ex.cpp
INCLUDEPATH += "C:Usersuserdlibinclude"
LIBS += -L"C:Usersuserdlib"
LIBS += -ldlib
QMAKE_CXXFLAGS_RELEASE += /arch:AVX
这个项目将不会在调试模式下编译和运行,因为我们在构建过程中使用了——config Release。但是发布模式会运行得非常快
请注意
- Visual Studio将需要在发布和调试版本中单独安装dlib。并且调试构建Dlib将非常慢
- 将/arch:AVX添加到编译器标志(MSVC)将使Dlib运行得更快
- DNN模块将无法在MSVC下工作,直到Cuda 8和MSVC 2015更新4将发布