ncnn官方在github上有编译好的静态库可以直接使用。但如果需要修改ncnn代码的话就只能自己编译了。
源码及静态库下载地址:Releases · Tencent/ncnn
编译过程参考链接:how-to-build#build-for-ios-on-macosx-with-xcode
(新旧版略有不同,有问题可在本文全局搜索错误信息找解决办法)
第一步:下载源码
源码下载地址:Releases · Tencent/ncnn
找到需要的版本下载即可。(我下载的是ncnn-20181228版本)
第二步:下载安装vulkan-sdk
# 下载并解压
$ wget
https://sdk.lunarg.com/sdk/download/1.1.114.0/mac/vulkansdk-macos-1.1.114.0.tar.gz?Human=true -O vulkansdk-macos-1.1.114.0.tar.gz
$ tar -xf vulkansdk-macos-1.1.114.0.tar.gz
# setup env(安装)
$ export VULKAN_SDK=`pwd`/vulkansdk-macos-1.1.114.0/macOS
注1:wget命令如果无法成功执行,可以直接复制链接地址到浏览器自行下载。
https://sdk.lunarg.com/sdk/download/1.1.114.0/mac/vulkansdk-macos-1.1.114.0.tar.gz?Human=true
下载后控制台cd到下载的目录,并执行export命令安装
注2:为避免无法预知的问题,下载的【ncnn源码文件夹】和安装好vulkan后出现的【vulkansdk-macos-1.1.114.0文件夹】最好在同一目录下
第三步:编译真机静态库
$ cd <ncnn-root-dir>
$ mkdir build-ios
$ cd build-ios
$ cmake -DCMAKE_TOOLCHAIN_FILE=../toolchains/ios.toolchain.cmake -DIOS_PLATFORM=OS ..
# vulkan is only available on arm64 devices
$ cmake -DCMAKE_TOOLCHAIN_FILE=../toolchains/ios.toolchain.cmake -DIOS_PLATFORM=OS64 -DVulkan_INCLUDE_DIR=`pwd`/vulkansdk-macos-1.1.114.0/MoltenVK/include -DVulkan_LIBRARY=`pwd`/vulkansdk-macos-1.1.114.0/MoltenVK/iOS/dynamic/libMoltenVK.dylib -DNCNN_VULKAN=ON ..
$ make -j4
$ make install
以上命令执行完且无报错就可以在build-ios文件夹中看到真机静态库(lib文件夹中)和头文件(include文件夹中)了。
如果过程中无报错可以跳过,继续看第四步。
部分不同版本的源码可能会出现错误。如果报错看文末的错误汇总吧🥺。也可以全文搜索错误信息。
第四步:编译模拟器静态库
$ cd <ncnn-root-dir>
$ mkdir build-ios-sim
$ cd build-ios-sim
$ cmake -DCMAKE_TOOLCHAIN_FILE=../toolchains/ios.toolchain.cmake -DIOS_PLATFORM=SIMULATOR ..
$ make -j4
$ make install
(步骤和编译真机库基本相同。就是DIOS_PLATFORM参数要设置为SIMULATOR)
· 以上命令执行完且无报错就可以在build-simu文件夹中看到模拟器静态库(lib文件夹中)和头文件(include文件夹中)了。
第五步:合并真机、模拟器静态库
会合并可以直接略过。不会合并的移步简书:iOS 动态库和静态库(.a和.framework)真机库和模拟器库的合并
官方文档没有合并,是把两个库打包成framework了。贴下命令
$ cd <ncnn-root-dir>
$ mkdir -p ncnn.framework/Versions/A/Headers
$ mkdir -p ncnn.framework/Versions/A/Resources
$ ln -s A ncnn.framework/Versions/Current
$ ln -s Versions/Current/Headers ncnn.framework/Headers
$ ln -s Versions/Current/Resources ncnn.framework/Resources
$ ln -s Versions/Current/ncnn ncnn.framework/ncnn
$ lipo -create \
build-ios/install/lib/libncnn.a \
build-ios-sim/install/lib/libncnn.a \
-o ncnn.framework/Versions/A/ncnn
$ cp -r build-ios/install/include/* ncnn.framework/Versions/A/Headers/
$ cp Info.plist ncnn.framework/Versions/A/Resources/
pick ncnn.framework folder for app development
错误汇总:
1、旧版新版cmake文件位置不同
以下为【ncnn-20181228】版本文件目录以下为【ncnn-20180830】版本文件目录
所以编译命令中cmake路径需要改变
编译真机库命令:
$ cd <ncnn-root-dir>
$ mkdir build-ios
$ cd build-ios
#✌️这里的路径要修改
$ cmake -DCMAKE_TOOLCHAIN_FILE=../ios.toolchain.cmake -DIOS_PLATFORM=iPhoneOS ..
# vulkan is only available on arm64 devices
#✌️这里的路径也要改
$ cmake -DCMAKE_TOOLCHAIN_FILE=../ios.toolchain.cmake -DIOS_PLATFORM=iPhoneOS -DVulkan_INCLUDE_DIR=`pwd`/vulkansdk-macos-1.1.114.0/MoltenVK/include -DVulkan_LIBRARY=`pwd`/vulkansdk-macos-1.1.114.0/MoltenVK/iOS/dynamic/libMoltenVK.dylib -DNCNN_VULKAN=ON ..
$ make -j4
$ make install
编译模拟器库命令$ cd <ncnn-root-dir>
$ mkdir build-ios-sim
$ cd build-simu
#✌️这里的路径要改
$ cmake -DCMAKE_TOOLCHAIN_FILE=../ios.toolchain.cmake -DIOS_PLATFORM=iPhoneSimulator ..
$ make -j4
$ make install
否则会出现报错1:
-- CMAKE_TOOLCHAIN_FILE = /Users/xy/Downloads/ncnn-20180830/toolchains/ios.toolchain.cmake
-- CMAKE_INSTALL_PREFIX = /Users/xy/Downloads/ncnn-20180830/build-ios/install
CMake Error at /usr/local/Cellar/cmake/3.15.2/share/cmake/Modules/CMakeDetermineSystem.cmake:99 (message):
#问题原因在这里✌️
Could not find toolchain file:
/Users/xy/Downloads/ncnn-20180830/toolchains/ios.toolchain.cmake
#问题原因结束
Call Stack (most recent call first):
CMakeLists.txt:21 (project)
CMake Error: CMake was unable to find a build program corresponding to "Unix Makefiles". CMAKE_MAKE_PROGRAM is not set. You probably need to select a different build tool.
CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage
CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage
-- Configuring incomplete, errors occurred!
修改cmake文件的相对路径即可。
2、旧版本ncnn编译真机库和模拟器库不能直接使用OS和SIMULATOR设置参数。
会报如下错误:
CMake Error at ios.toolchain.cmake:118 (message):
#关键原因在这里✌️
Unsupported IOS_PLATFORM value selected. Please choose iPhoneOS or iPhoneSimulator
#关键原因结束
Call Stack (most recent call first):
/usr/local/Cellar/cmake/3.15.2/share/cmake/Modules/CMakeDetermineSystem.cmake:93 (include)
CMakeLists.txt:21 (project)
CMake Error: CMake was unable to find a build program corresponding to "Unix Makefiles". CMAKE_MAKE_PROGRAM is not set. You probably need to select a different build tool.
-- Configuring incomplete, errors occurred!
解决办法:DIOS_PLATFORM设置错误。例如错误命令:
$ cmake -DCMAKE_TOOLCHAIN_FILE=../ios.toolchain.cmake -DIOS_PLATFORM=OS ..
需改修改为
#真机 -DIOS_PLATFORM=iPhoneOS
$ cmake -DCMAKE_TOOLCHAIN_FILE=../ios.toolchain.cmake -DIOS_PLATFORM=iPhoneOS ..
#或 模拟器 -DIOS_PLATFORM=iPhoneSimulator
$ cmake -DCMAKE_TOOLCHAIN_FILE=../ios.toolchain.cmake -DIOS_PLATFORM=iPhoneSimulator ..
3、编译的时候报错"Invalid CMAKE_OSX_SYSROOT: does not exist"
-- CMAKE_TOOLCHAIN_FILE = /Users/xy/Desktop/ncnn-20181228/toolchains/ios.toolchain.cmake
-- CMAKE_INSTALL_PREFIX = /Users/xy/Desktop/ncnn-20181228/build-ios/install
-- Building with Xcode version:
-- Configuring iOS build for platform: OS, architecture(s): armv7;armv7s;arm64
CMake Error at toolchains/ios.toolchain.cmake:211 (message):
#主要原因在这里👆
Please make sure that Xcode is installed and that the toolchainis pointing
to the correct path. Please run:sudo xcode-select -s
/Applications/Xcode.app/Contents/Developerand see if that fixes the problem
for you.
#原因end
Call Stack (most recent call first):
/usr/local/Cellar/cmake/3.15.2/share/cmake/Modules/CMakeDetermineSystem.cmake:93 (include)
CMakeLists.txt:24 (project)
#还有这里✌️
CMake Error at toolchains/ios.toolchain.cmake:215 (message):
Invalid CMAKE_OSX_SYSROOT: does not exist.
#原因end
Call Stack (most recent call first):
/usr/local/Cellar/cmake/3.15.2/share/cmake/Modules/CMakeDetermineSystem.cmake:93 (include)
CMakeLists.txt:24 (project)
CMake Error: CMake was unable to find a build program corresponding to "Unix Makefiles". CMAKE_MAKE_PROGRAM is not set. You probably need to select a different build tool.
CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage
CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage
-- Configuring incomplete, errors occurred!
参考链接How to use Mac
执行如下方法解决了:
sudo xcode-select -s /Applications/Xcode.app/Contents/Developer
4、
CMake Error: The source directory "/Users/**/***路径名" does not appear to contain CMakeLists.txt.
Specify --help for usage, or press the help button on the CMake GUI.
解决办法:
看看当前文件夹位置,CMakeLists.txt是不是没有在当前文件夹的上级文件夹里。要进入build-ios文件夹中再执行命令。
5、一堆代码报错
In file included from /Users/xy/Downloads/ncnn-20180830/src/allocator.cpp:15:
In file included from /Users/xy/Downloads/ncnn-20180830/src/allocator.h:22:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/pthread.h:60:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/time.h:71:
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_size_t.h:31:9: error:
unknown type name '__darwin_size_t'; did you mean '__darwin_ino_t'?
typedef __darwin_size_t size_t;
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types.h:64:26: note:
'__darwin_ino_t' declared here
typedef __darwin_ino64_t __darwin_ino_t; /* [???] Used for inodes */
^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
fatal error: too many errors emitted, stopping now [-ferror-limit=]
fatal error: too many errors emitted, stopping now [-ferror-limit=]
#(一堆代码报错)
问题原因:我以为这条不用执行就跳过了结果报错。执行这行命令再执行make -j4就ok了
6、
CMake Warning:
No source or binary directory provided. Both will be assumed to be the
same as the current working directory, but note that this warning will
become a fatal error in future CMake releases.
CMake Error: The source directory "/Users/xy/Downloads/ncnn-20180830/build-ios" does not appear to contain CMakeLists.txt.
Specify --help for usage, or press the help button on the CMake GUI.
问题原因:cmake命令后面的 .. (两个英文句号)没写。
例如错误命令:
$ cmake -DCMAKE_TOOLCHAIN_FILE=../ios.toolchain.cmake -DIOS_PLATFORM=iPhoneOS
后面需要加..(注意两点前有空格)
$ cmake -DCMAKE_TOOLCHAIN_FILE=../ios.toolchain.cmake -DIOS_PLATFORM=iPhoneOS ..
7、旧版无法直接编译支持真机/模拟器所有架构的库。
来源:build for ios.zh · Tencent/ncnn
且由文档可知,每个架构需要分别编译。重复第三步~第四步把所有架构的静态库分别编译出来,然后用lipo -create -output命令合并成胖子库。
我们在步骤三四中的cmake命令中把它加上就可以了。例如要编译armv7s的架构:
#设置编译的静态库支持的架构为armv7s
$ cmake -DCMAKE_TOOLCHAIN_FILE=../ios.toolchain.cmake -DIOS_PLATFORM=iPhoneOS -DCMAKE_OSX_ARCHITECTURES=armv7s