Visual Studio上编译ncnn

prerequisite

是为了在PC上熟悉ncnn的基本代码,所以用Visual Studio来配置的。

期间用过VS2013(update5)/VS2015/VS2017,反正都是基于CMake生成一个.sln再开VS编译,都能编译通过。

个别细节在博客里可能有写错、写漏,如果你觉得要吐槽欢迎在评论区贴出。

先装了这些:

  • vs2015,注意勾选win10 SDK
  • cmake
  • git
  • opencv, 官网下载的3.x版本的是vs2015预编译好的,或自己编译

下载https://github.com/google/protobuf/archive/v3.4.0.zip并解压

后续步骤都使用nmake来搞。注意几点:

  • 依赖库和主项目,要使用同样的build type(Debug/Release)。这里都通过cmake的-DCMAKE_BUILD_TYPE=Release指定

    • vs2015: call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" amd64

    • vs2017: call "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars64.bat" amd64

  • 调用不同visual studio版本的nmake前,先调用vcvarsall.bat来临时(只在当前cmd中)设定相关的环境变量。

  • 调用vcvarsall.bat最好是指定目标cpu架构,这里使用amd64

更多nmake参数内容见nmake官方文档

下面以vs2015为例进行配置,vs2017简要替换即可

compile protobuf

cd D:\lib\protobuf-3.4.0\build-vs2015

call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" amd64

cmake -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=%cd%/install ^
-Dprotobuf_BUILD_TESTS=OFF ^
-Dprotobuf_MSVC_STATIC_RUNTIME=OFF ../cmake nmake
nmake install

compile ncnn

cd D:\work\ncnn\build-vs2015

call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" amd64

cmake -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=%cd%/install ^
-DProtobuf_INCLUDE_DIR=D:/lib/protobuf-3.4.0/build-vs2015/install/include ^
-DProtobuf_LIBRARIES=D:/lib/protobuf-3.4.0/build-vs2015/install/lib/libprotobuf.lib ^
-DProtobuf_PROTOC_EXECUTABLE=D:/lib/protobuf-3.4.0/build-vs2015/install/bin/protoc.exe .. nmake
nmake install

compile ncnn examples

把<ncnn_root>/examples整个目录拷贝到一个新的地方(比如d:/work/ncnn_examples),并修改新的CMakeLists.txt内容为:

cmake_minimum_required(VERSION 2.8.10)
project(ncnn-examples) #set (CMAKE_CXX_STANDARD 11) # 用于找到opencv编译好的各种lib和头文件
if (CMAKE_SYSTEM_NAME MATCHES "Windows")
list(APPEND CMAKE_PREFIX_PATH "D:/work/opencv320/build")
elseif (CMAKE_SYSTEM_NAME MATCHES "Linux")
list(APPEND CMAKE_PREFIX_PATH "/home/chris/work/gitdown/opencv-2.4.13/build")
endif (CMAKE_SYSTEM_NAME MATCHES "Windows") find_package(OpenCV QUIET COMPONENTS core highgui imgproc imgcodecs)
if(NOT OpenCV_FOUND)
find_package(OpenCV REQUIRED COMPONENTS core highgui imgproc)
endif() find_package(OpenMP)
if(OpenMP_CXX_FOUND OR OPENMP_FOUND)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
endif() # 用于找到ncnn编译好的各种东西
include_directories(D:/work/ncnn/src)
include_directories(D:/work/ncnn/build-vs2015/src)
link_directories(D:/work/ncnn/build-vs2015/install/lib) # for win32
add_definitions(-D_SCL_SECURE_NO_WARNINGS -D_CRT_SECURE_NO_DEPRECATE) add_executable(squeezenet squeezenet.cpp)
target_link_libraries(squeezenet ncnn ${OpenCV_LIBS}) add_executable(fasterrcnn fasterrcnn.cpp)
target_link_libraries(fasterrcnn ncnn ${OpenCV_LIBS}) add_executable(yolov2 yolov2.cpp)
target_link_libraries(yolov2 ncnn ${OpenCV_LIBS}) add_subdirectory(ssd)

需要把squeezenet.cpp中添加头文件#include <functional>

然后开始编译:

cd D:\work\ncnn_examples\build-vs2015

cmake  ..  -G  "NMake Makefiles"  -DCMAKE_BUILD_TYPE=Release
上一篇:在Eclipse中配置Tomcat 创建和运行Servlet/JSP


下一篇:session的创建和销毁时间