乐趣区

关于windows10:经验分享win10-qmake-构建-ncnn-vs-工程

欢送关注我的公众号 [极智视界],回复 001 获取 Google 编程标准

O_o>_<   o_OO_o~_~o_O

  本教程具体记录了在 win10 环境中应用 qmake 构建 ncnn vs 工程的办法。

1、筹备 opencv

   windows 上想要应用 opencv 很简略,opencv 官网提供了编译好的 windows 版本的库能够间接拿来用。

  传送门:https://opencv.org/releases/

  当然也能够抉择源码编译 opencv,能够参考《【教训分享】windows 源码编译 opencv》。

2、编译 protobuf

   留神要用 vs2017 自带的终端 – 实用于 VS2017 的 X64 本机工具命令提醒。

   下载 protobuf-3.4.0,传送:https://github.com/google/pro…

  编译 protobuf:

cd <protobuf-root-dir>
mkdir build
cd build

call "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Auxiliary\Build\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

  编译完会在 build/install 下生成如下目录构造:

3、编译 ncnn

  clone ncnn 工程:

git clone --recursive https://github.com/Tencent/ncnn.git

  编译 ncnn,其中 <protobuf-root-dir> 为下面下载的 protobuf 源码包门路:

cd <ncnn-root-dir>
mkdir -p build
cd build

call "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Auxiliary\Build\vcvarsall.bat" amd64

cmake -G"NMake Makefiles" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=%cd%/install -DProtobuf_INCLUDE_DIR=<protobuf-root-dir>/build/install/include -DProtobuf_LIBRARIES=<protobuf-root-dir>/build/install/lib/libprotobuf.lib -DProtobuf_PROTOC_EXECUTABLE=<protobuf-root-dir>/build/install/bin/protoc.exe -DNCNN_VULKAN=OFF ..

nmake
nmake install

  编译实现后会在 build/install 下生成如下目录构造:

   且在 build/src 下会生成一些 .h 和 .cpp,会用于前面构建工程。以上就实现了 vs2017 构建 ncnn windows 版本库依赖的编译,上面进入 vs2017 构建。

4、vs2017 构建 ncnn

  我这里应用 qmake 来构建 vs 工程。qmake 的 ncnn.pro 如下,从 pro 里能够看到构建 ncnn 工程时,会将后面的 opencv、protobuf、ncnn 编译进去的文件作为三方库了。

# ncnn.pro
TEMPLATE = app
CONFIG += console debug_and_release warn_on c++11
QT -= gui

win32 {QMAKE_CXXFLAGS += /MP /utf-8}

QMAKE_CXXFLAGS_RELEASE += $$QMAKE_CFLAGS_RELEASE_WITH_DEBUGINFO
QMAKE_LFLAGS_RELEASE   += $$QMAKE_LFLAGS_RELEASE_WITH_DEBUGINFO

Debug:{
    TARGET = ncnn
    DESTDIR  = ./build/debug
    TEMP_DESTDIR = ./build/intermediate/debug/$$TARGET
}

Release:{
    TARGET = ncnn
    DESTDIR  = ./build/release
    TEMP_DESTDIR = ./build/intermediate/release/$$TARGET
}

MOC_DIR         = $$TEMP_DESTDIR/moc
RCC_DIR         = $$TEMP_DESTDIR/rcc
UI_DIR          = $$TEMP_DESTDIR/qui
OBJECTS_DIR     = $$TEMP_DESTDIR/obj

win32 {
    LIBS += -L./3rdparty/protobuf/lib/ libprotobuf.lib libprotobuf-lite.lib libprotoc.lib
    CONFIG(debug,debug|release){
        LIBS += \
            -L./3rdparty/opencv/x64/vc15/lib opencv_imgprocd.lib opencv_imgcodecsd.lib opencv_cored.lib opencv_dnnd.lib
    }
    CONFIG(release,debug|release){
        LIBS += \
            -L./3rdparty/opencv/x64/vc15/lib opencv_imgproc.lib opencv_imgcodecs.lib opencv_core.lib opencv_dnn.lib 
    }
}

win32 {
    INCLUDEPATH  += \
        ./3rdparty/opencv/include \
        ./3rdparty/protobuf/include/google/protobuf \
        ./3rdparty/ncnn/include/ncnn \
        ./3rdparty/ncnn/src \
        ./3rdparty/ncnn/src/layer/x86
}

HEADERS += ./src/xxx.cpp \
    
SOURCES += ./src/xxx.h \
    
#CUDA_SOURCES += \
    
win32 {
    SYSTEM_NAME = x64                 
    SYSTEM_TYPE = 64                  
    CUDA_ARCH = compute_35
    CUDA_CODE = sm_35
    CUDA_INC = $$join(INCLUDEPATH,'"-I"','-I"','"')
    MSVCRT_LINK_FLAG_DEBUG   = "/MDd"
    MSVCRT_LINK_FLAG_RELEASE   = "/MD"
    # Configuration of the Cuda compiler
    CONFIG(debug, debug|release) {
        # Debug mode
        cuda.input = CUDA_SOURCES
        cuda.output = $$OBJECTS_DIR/${QMAKE_FILE_BASE}_cuda.obj
        cuda.commands = $$PWD/./3rdparty/cuda/bin/nvcc.exe -D_DEBUG -Xcompiler $$MSVCRT_LINK_FLAG_DEBUG -c -Xcompiler $$join(QMAKE_CXXFLAGS,",") $$join(INCLUDEPATH,'"-I"','-I"','"') ${QMAKE_FILE_NAME} -o ${QMAKE_FILE_OUT}
        QMAKE_EXTRA_COMPILERS += cuda
    } else {
        # Release mode
        cuda.input = CUDA_SOURCES
        cuda.output = $$OBJECTS_DIR/${QMAKE_FILE_BASE}_cuda.obj
        cuda.commands = $$PWD/./3rdparty/cuda/bin/nvcc.exe -Xcompiler $$MSVCRT_LINK_FLAG_RELEASE -c -Xcompiler $$join(QMAKE_CXXFLAGS,",") $$join(INCLUDEPATH,'"-I"','-I"','"') ${QMAKE_FILE_NAME} -o ${QMAKE_FILE_OUT}
        QMAKE_EXTRA_COMPILERS += cuda
    }
}

   再构建一个 create_vs_pro.bat:

# create_vs_pro.bat
set PRO_FILE=ncnn.pro

set VC_DIR=C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Auxiliary\Build
call "%VC_DIR%\vcvars64.bat"

set QT_5_9_8=C:\Qt\Qt5.9.8\5.9.8\msvc2015_64\bin
set OPENCV_4_3_0=.\3rdparty\opencv\x64\vc15\bin
set CUDA_11_0=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.0\bin

SET "PATH=%OPENCV_4_3_0%;%CUDA_11_0%;%PATH%"

qmake -r -tp vc  %PRO_FILE%

pause

   双击 create_vs_pro.bat 就能生成 ncnn.sln,而后就能够进 vs studio 进行编译调试了。

  须要留神的是,我这里是没有把 examples、tools、tests、benchmark 里大量的实用工具做成独立工程的模式,这些用到的时候再独自加到工程的 source 里就能够了。

  其实 qmake 编译那块还有些细坑没写,推敲一下应该能解决,若有问题欢送分割我,心愿我的分享能对你有一点帮忙。


【公众号传送】
《【教训分享】win10 qmake 构建 ncnn vs 工程》

退出移动版