官方给出的编译方法是用于VS,Qt中编译稍有不同,过程更简单。其他:Qt+ncnn+android配置方法。
1. 把ncnn的CMakeLists.txt文件直接拖进Qt中。
2. 设置要编译哪些东西,我这里不需要用到benchmark,examples,tools和tests,(所以这里我不需要用到protobuf),所以可以在CMakeLists.txt文件中注释掉或者在构建设置中进行设置。如下图所示。
3.创建build文件夹,执行cmake
4. 在构建目录下进行make。执行下面这句:
D:\Qt5\Qt5.9.6\Tools\mingw530_32\bin\mingw32-make.exe
编译成功:
最终在build/src目录下可以找到libncnn.a文件夹。可以把它拷出来,和include文件放到一起,然后在Qt中正常地使用即可。
5. 运行结果如下图所示:
20211221补充:
不知道为啥,cmake的结果突然变成生成ninja文件了,需要在cmake的时候指定一下想要MinGW的结果:
cmake -G "MinGW Makefiles" -DCMAKE_SH="CMAKE_SH-NOTFOUND"
20220213补充:
如果编译遇到int32什么的问题,在cmakelist.txt文件中添加一行:
add_compile_options(-std=c++11)
带vulkan编译:
1.下载protobuf
2.下载安装Vulkan SDK
3.编译protobuf(好像不需要protobuf,ncnn编译设置中protobuf相应去掉)
cmake -G"MinGW Makefiles" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=%cd%/install -Dprotobuf_BUILD_TESTS=OFF -Dprotobuf_MSVC_STATIC_RUNTIME=OFF ../cmake
<Qt_install_dir>\Tools\mingw730_64\bin\mingw32-make.exe -j4
<Qt_install_dir>\Tools\mingw730_64\bin\mingw32-make.exe install
4.编译ncnn带vulkan:
如果出现问题Error: invalid register for .seh_savexmm:
在cmakelist.txt文件中添加:
add_compile_options(-fno-asynchronous-unwind-tables)
5.cmake & make & make install
cmake -G"MinGW Makefiles" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=%cd%/install -DProtobuf_INCLUDE_DIR=D:/zzxs/codes/protobuf-3.4.0/protobuf-3.4.0/mybuild/%cd%/install/include -DProtobuf_LIBRARIES=D:/zzxs/codes/protobuf-3.4.0/protobuf-3.4.0/mybuild/%cd%/install/lib/libprotobuf.a -DProtobuf_PROTOC_EXECUTABLE=D:/zzxs/codes/protobuf-3.4.0/protobuf-3.4.0/mybuild/%cd%/install/bin/protoc.exe -DNCNN_VULKAN=ON ..
20220510补充:
编译动态库添加设置:
-DNCNN_SHARED_LIB=True