在參考了N多文檔,走了N多彎路後終於看見“MNNConvert.exe”文件了,時間都耽誤在編譯這個轉換工具上了,感謝https://uzshare.com/view/810473這篇文章的作者,但是這篇博客並沒有把編譯轉換工具的流程加進去,所以只好根據官方文檔自力更生。不說廢話了 3, 2, 1 走……
準備工作
-
cmake-3.17.1-win64,解壓後將cmake-3.17.1-win64-x64\bin這個路徑添加到你的環境變量中,如我的:
-
在github上下載protobuf,得到protobuf-master文件夾,在此文件夾下新建一個vsproject文件夾(文件夾名你隨便取,只是因爲此文件夾下已經有了一個build的東東,所以才起的這個名字),然後按照上篇博客裏的描述的那樣使用cmake-gui.exe對protobuf生成vs的工程,然後在vs中對剛纔生成的工程進行編譯,記得選release哦。最後得到如下文件目錄:
直到這一步,基本上不會出現任何問題,一路綠燈 -
將上一步得到的“”“include”和“Release”文件夾路徑寫入你的環境變量,一定要做!!!編譯MNN的轉換工具需要!最後我的環境路徑如下:
-
去MNN官網下載mnn的包吧,解壓後得到MNN-master文件夾
-
默認你已經安裝好了python3和vs2017
開始編譯
- 在MNN-master文件夾下定位到“schema”下,新建一個python文件,文件名:generate.py 內容爲:
#-*-coding:utf-8-*-
#coding by: yuangu([email protected])
import os
import sys
import shutil
import platform
def p():
frozen = "not"
if getattr(sys, 'frozen',False):
frozen = "ever so"
return os.path.dirname(sys.executable)
return os.path.split(os.path.realpath(__file__))[0]
currentWorkPath = p()
os.chdir(currentWorkPath)
if '-lazy' in sys.argv and os.path.isdir("current"):
print("*** done ***")
exit(0)
# check is flatbuffer installed or not
FLATC = '../3rd_party/flatbuffers/tmp/flatc' + ('.exe' if "Windows" == platform.system() else '')
FLATC = os.path.realpath(FLATC)
if not os.path.isfile(FLATC):
print("*** building flatc ***")
tmpDir = os.path.realpath('../3rd_party/flatbuffers/tmp')
if os.path.isdir(tmpDir):
shutil.rmtree(tmpDir)
os.mkdir(tmpDir)
os.chdir(tmpDir)
os.system('cmake -DCMAKE_BUILD_TYPE=Release ..')
if "Windows" == platform.system():
os.system('cmake --build . --target flatc --config Release')
if os.path.isfile( os.path.join(tmpDir, 'Release/flatc.exe') ):
shutil.move(os.path.join(tmpDir, 'Release/flatc.exe'), FLATC)
else:
os.system('cmake --build . --target flatc')
# dir recover
os.chdir(currentWorkPath)
# determine directory to use
DIR='default'
if os.path.isdir('private'):
DIR = 'private'
DIR = os.path.realpath(DIR)
# clean up
print('*** cleaning up ***')
if os.path.isdir('current'):
shutil.rmtree('current')
os.mkdir('current')
# flatc all fbs
os.chdir('current')
listFile = os.listdir(DIR)
for fileName in listFile:
tmpFileName = os.path.join(DIR, fileName)
cmd = "%s -c -b --gen-object-api --reflect-names %s" %(FLATC, tmpFileName)
os.system(cmd)
os.chdir(currentWorkPath)
print( "*** done ***")
保存後,打開cmd,運行 python generate.py,會自動幫你生成後續編譯需要的工程,一般這裏也不會有什麼錯誤。這一步想了解具體過程的請參加:https://uzshare.com/view/810473 我是直接拷貝過來用的
- 進入主題
在命令行下定位到mnn的主目錄
輸入:
mkdir build && cd build
cmake -G "Visual Studio 15 Win64" -DCMAKE_BUILD_TYPE=Release -DMNN_BUILD_CONVERTER=true -DMNN_BUILD_SHARED_LIBS=false ..
解釋:
-G “Visual Studio 15 Win64” 使用64位編譯器
-DCMAKE_BUILD_TYPE=Release 編譯成release版本
-DMNN_BUILD_CONVERTER=true 編譯converter工具
-DMNN_BUILD_SHARED_LIBS 官網上給出的,沒細究
命令最後有兩個點別忘了!!
等待完成。
完成後就輸入:
cmake --build .
這裏官網給出的是使用ninja命令編譯,這個沒用過也沒時間去深究了,就直接cmake吧。
如果一切正常的話在build目錄下會生成一個Release文件夾,裏面就是編譯出來的各種工具了。但是在執行cmake --build的時候很可能會報各種奇怪的錯誤,一般情況下除了那個MNNConvert相關的工程外都能正常編譯,主要是因爲這個轉換的工程依賴於第一步的那個protobuf,列舉幾個經常碰見的問題:
- 找不到頭文件,檢查一下是否把第一步編譯出來的Release和include文件夾路徑寫入了環境路徑並且已經生效
- 報:libprotobuf.lib(any.obj) : error LNK2038: 檢測到“RuntimeLibrary”的不匹配項,仔細看一下錯誤的原因,這裏很可能是使用libprotobuf的debug版本來編譯MNNConvert的Release版本,反之亦然。反正就是版本不對。我就是碰到了這個問題,可是我明明已經指定了DCMAKE_BUILD_TYPE=Release選項,不得已,在vs2017中打開的工程,選擇release編譯通過。
- 報了很多什麼參數不匹配,XX函數未定義這一類的錯誤,建議還是把build文件夾全部刪除,重頭來過吧。
- 最終編譯出的文件列表