(转)WIN10+dlib-19.15+VS2015环境搭建

https://blog.csdn.net/qq_15715657/article/details/81302283

1.下载VS2015

2.下载CMake 3.12.0

3.下载Dlib:

Dlib官网地址:http://www.dlib.net/ml.html 

官网下载最新dlib文件

解压放在F盘,并新建一个文件build

用cmake—gui搞了一个下午,一直编译不过,最后改成官网推荐的命令行编译了。

右键shirt打开cmd,操作:

  1. cd build

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

巴拉巴拉之后,再输入

cmake --build . --config Release

Release X64位比较快,Debug比较慢,在VS2015配置编译也只是Release X64下

巴拉巴拉之后,这时候在F:\dlib-19.15\build\dlib\Release下有

dlib_lib.lib是我新建备份的

注:选择SSE4或者AVX是官网说在release下,可以加快编译速度,如:http://dlib.net/faq.html#Whyisdlibslow

VS2015新建一个空的win32,C++项目

打开view>>other windows>>项目管理器

编译源码时,需要添加一个源文件source.cpp,使用添加现有项的方式。 
目录是F:/dlib-19.15/dlib/all/source.cpp 

全部改为自己的目录路径既可以

再添加main文件


  
  
  1. // The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
  2. /*
  3. This is an example illustrating the use of the gui api from the dlib C++ Library.
  4. This is a pretty simple example. It makes a window with a user
  5. defined widget (a draggable colored box) and a button. You can drag the
  6. box around or click the button which increments a counter.
  7. */
  8. #include "stdafx.h"
  9. #include <dlib/gui_widgets.h>
  10. #include <sstream>
  11. #include <string>
  12. using namespace std;
  13. using namespace dlib;
  14. // ----------------------------------------------------------------------------
  15. class color_box : public draggable
  16. {
  17. /*
  18. Here I am defining a custom drawable widget that is a colored box that
  19. you can drag around on the screen. draggable is a special kind of drawable
  20. object that, as the name implies, is draggable by the user via the mouse.
  21. To make my color_box draggable all I need to do is inherit from draggable.
  22. */
  23. unsigned char red, green, blue;
  24. public:
  25. color_box(
  26. drawable_window& w,
  27. rectangle area,
  28. unsigned char red_,
  29. unsigned char green_,
  30. unsigned char blue_
  31. ) :
  32. draggable(w),
  33. red(red_),
  34. green(green_),
  35. blue(blue_)
  36. {
  37. rect = area;
  38. set_draggable_area(rectangle( 10, 10, 400, 400));
  39. // Whenever you make your own drawable widget (or inherit from any drawable widget
  40. // or interface such as draggable) you have to remember to call this function to
  41. // enable the events. The idea here is that you can perform whatever setup you
  42. // need to do to get your object into a valid state without needing to worry about
  43. // event handlers triggering before you are ready.
  44. enable_events();
  45. }
  46. ~color_box(
  47. )
  48. {
  49. // Disable all further events for this drawable object. We have to do this
  50. // because we don't want any events (like draw()) coming to this object while or
  51. // after it has been destructed.
  52. disable_events();
  53. // Tell the parent window to redraw its area that previously contained this
  54. // drawable object.
  55. parent.invalidate_rectangle(rect);
  56. }
  57. private:
  58. void draw(
  59. const canvas& c
  60. ) const
  61. {
  62. // The canvas is an object that represents a part of the parent window
  63. // that needs to be redrawn.
  64. // The first thing I usually do is check if the draw call is for part
  65. // of the window that overlaps with my widget. We don't have to do this
  66. // but it is usually good to do as a speed hack. Also, the reason
  67. // I don't have it set to only give you draw calls when it does indeed
  68. // overlap is because you might want to do some drawing outside of your
  69. // widget's rectangle. But usually you don't want to do that :)
  70. rectangle area = c.intersect(rect);
  71. if (area.is_empty() == true)
  72. return;
  73. // This simple widget is just going to draw a box on the screen.
  74. fill_rect(c, rect, rgb_pixel(red, green, blue));
  75. }
  76. };
  77. // ----------------------------------------------------------------------------
  78. class win : public drawable_window
  79. {
  80. /*
  81. Here I am going to define our window. In general, you can define as
  82. many window types as you like and make as many instances of them as you want.
  83. In this example I am only making one though.
  84. */
  85. public:
  86. win(
  87. ) : // All widgets take their parent window as an argument to their constructor.
  88. c(* this),
  89. b(* this),
  90. cb(* this, rectangle( 100, 100, 200, 200), 0, 0, 255), // the color_box will be blue and 101 pixels wide and tall
  91. mbar(* this)
  92. {
  93. // tell our button to put itself at the position (10,60).
  94. b.set_pos( 10, 60);
  95. b.set_name( "button");
  96. // let's put the label 5 pixels below the button
  97. c.set_pos(b.left(), b.bottom() + 5);
  98. // set which function should get called when the button gets clicked. In this case we want
  99. // the on_button_clicked member to be called on *this.
  100. b.set_click_handler(* this, &win::on_button_clicked);
  101. // Alternatively, if you have a compiler which supports the lambda functions from the
  102. // new C++ standard then you can use a lambda function instead of telling the click
  103. // handler to call one of the member functions. So for example, you could do this
  104. // instead (uncomment the code if you have C++0x support):
  105. /*
  106. b.set_click_handler([&](){
  107. ++counter;
  108. ostringstream sout;
  109. sout << "Counter: " << counter;
  110. c.set_text(sout.str());
  111. });
  112. */
  113. // In general, all the functions which register events can take either member
  114. // functions or lambda functions.
  115. // Let's also make a simple menu bar.
  116. // First we say how many menus we want in our menu bar. In this example we only want 1.
  117. mbar.set_number_of_menus( 1);
  118. // Now we set the name of our menu. The 'M' means that the M in Menu will be underlined
  119. // and the user will be able to select it by hitting alt+M
  120. mbar.set_menu_name( 0, "Menu", 'M');
  121. // Now we add some items to the menu. Note that items in a menu are listed in the
  122. // order in which they were added.
  123. // First let's make a menu item that does the same thing as our button does when it is clicked.
  124. // Again, the 'C' means the C in Click is underlined in the menu.
  125. mbar.menu( 0).add_menu_item(menu_item_text( "Click Button!", * this, &win::on_button_clicked, 'C'));
  126. // let's add a separator (i.e. a horizontal separating line) to the menu
  127. mbar.menu( 0).add_menu_item(menu_item_separator());
  128. // Now let's make a menu item that calls show_about when the user selects it.
  129. mbar.menu( 0).add_menu_item(menu_item_text( "About", * this, &win::show_about, 'A'));
  130. // set the size of this window
  131. set_size( 430, 380);
  132. counter = 0;
  133. set_title( "dlib gui example");
  134. show();
  135. }
  136. ~win(
  137. )
  138. {
  139. // You should always call close_window() in the destructor of window
  140. // objects to ensure that no events will be sent to this window while
  141. // it is being destructed.
  142. close_window();
  143. }
  144. private:
  145. void on_button_clicked(
  146. )
  147. {
  148. // when someone clicks our button it will increment the counter and
  149. // display it in our label c.
  150. ++counter;
  151. ostringstream sout;
  152. sout << "counter: " << counter;
  153. c.set_text(sout.str());
  154. }
  155. void show_about(
  156. )
  157. {
  158. message_box( "About", "This is a dlib gui example program");
  159. }
  160. unsigned long counter;
  161. label c;
  162. button b;
  163. color_box cb;
  164. menu_bar mbar;
  165. };
  166. // ----------------------------------------------------------------------------
  167. int main()
  168. {
  169. // create our window
  170. win my_window;
  171. // wait until the user closes this window before we let the program
  172. // terminate.
  173. my_window.wait_until_closed();
  174. return 0;
  175. }
  176. // ----------------------------------------------------------------------------
  177. // Normally, if you built this application on MS Windows in Visual Studio you
  178. // would see a black console window pop up when you ran it. The following
  179. // #pragma directives tell Visual Studio to not include a console window along
  180. // with your application. However, if you prefer to have the console pop up as
  181. // well then simply remove these #pragma statements.
  182. #ifdef _MSC_VER
  183. # pragma comment( linker, "/entry:mainCRTStartup" )
  184. # pragma comment( linker, "/SUBSYSTEM:WINDOWS" )
  185. #endif
  186. // ----------------------------------------------------------------------------

点击编译即可

按照上面就不会出现

USER_ERROR__inconsistent_build_configuration__see_dlib_faq_2和

USER_ERROR__inconsistent_build_configuration__see_dlib_faq_1

的问题,但是

(1)如果遇到png.h文件找不到,确定附加库目录已经包含dlib,使用"dlib/external/libpng/png.h"去代替<png.h>即可。 
(2)最好使用Release版本去编译使用,速度快。 
(3)如在Debug模式下OBJ字节数组太大,则在属性 -> C/C++ -> 命令行 加入/bigobj即可。 
(4)若读取jpeg图片错误或找不到jpeglib.h,在第五步中,加入DLIB_JPEG_STATIC即可。

(5)如果是使用VS2015,主要要更新到update 3,这才支持C++11的

猜你喜欢

转载自blog.csdn.net/zyb418/article/details/88085536