1.下載源碼protobuf-3.10.0
2.解壓源碼到工作目錄
3.找到解壓目錄下的cmake/README.md
4.依次按照其步驟編譯
README.md步驟
1.打開Native Tools Command Prompt(VS2013是VS2013 x64 本機工具命令提示,VS2017是VS 2017的 x64_x86 交叉工具命令提示符),VS2013和VS2017該工具的路徑也不一樣,注意區分
2.在Cmd命令下進入解壓目錄同級目錄,比如第一步解壓在C:\Path\to目錄下,執行C:\Path\to>mkdir install,創建install目錄,在編譯過程中,這個目錄並沒什麼用,暫時創建出來就可
3.假如命1中的工具不在環境變量,將其加入環境變量。配置git,因爲後面如果沒有提前下載源碼的zip包,是在cmd下使用git下載
C:\Path\to> git clone -b [release_tag] https://github.com/protocolbuffers/protobuf.git
4.進入解壓目錄, C:\Path\to>cd protobuf
5.進入cmake目錄,C:\Path\to\protobuf>cd cmake。C:\Path\to\protobuf\cmake>
6.創建build目錄,並進入build目錄。 C:\Path\to\protobuf\cmake>mkdir build & cd build
C:\Path\to\protobuf\cmake\build>
7.創建release目錄。
C:\Path\to\protobuf\cmake\build>mkdir release & cd release
C:\Path\to\protobuf\cmake\build\release>cmake -G "NMake Makefiles" ^
-DCMAKE_BUILD_TYPE=Release ^
-DCMAKE_INSTALL_PREFIX=../../../../install ^
../..
8.創建debug目錄
C:\Path\to\protobuf\cmake\build>mkdir debug & cd debug
C:\Path\to\protobuf\cmake\build\debug>cmake -G "NMake Makefiles" ^
-DCMAKE_BUILD_TYPE=Debug ^
-DCMAKE_INSTALL_PREFIX=../../../../install ^
../..
9.創建解決方案目錄
C:\Path\to\protobuf\cmake\build>mkdir solution & cd solution
C:\Path\to\protobuf\cmake\build\solution>cmake -G "Visual Studio 14 2015 Win64" ^
-DCMAKE_INSTALL_PREFIX=../../../../install ^
../..
10.編譯。編譯的話分兩種,一種是通過nmake編譯,一種是通過VS的sln編譯,我們選擇第9步生成的。
11.打開sln,protobuf.sln,如果已經下載了protoc.exe,在只需要編譯libprotobuf工程,否則還得編譯libprotoc工程。
12.遇到v140工具的問題,打開VS的安裝器(VS installer,VS菜單欄->幫助->檢查更新->打開Visual Studio安裝程序,選在安裝器界面的,更多->更改->單個組件,搜索框搜索v140,安裝適用於桌面的VC++2015.3 v14.00(v140)工具集),繼續生成libprotobuf,release。
13.生成incluede目錄,執行extract_includes.bat
14.如果需要Zlib,則進行Zlib庫安裝,目前我僅進行到13,沒有安裝ZLib
15.將生成的生成的include和lib拷貝到UE4中,添加頭文件包含目錄和庫包含目錄,以及庫的引用。
16.修改.Build.cs,添加lib的路徑
public class ExcelPBC : ModuleRules
{
private string ModulePath
{
get
{
return ModuleDirectory;
}
}
private string ThirdPartyPath
{
get
{
return Path.GetFullPath(Path.Combine(ModulePath, "../ExcelPBC/ThirdParty/ProtoBuffer"));
}
}
public ExcelPBC(ReadOnlyTargetRules Target) : base(Target)
{
PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "HeadMountedDisplay" });
PublicIncludePaths.Add(Path.Combine(ThirdPartyPath, "include"));
PublicAdditionalLibraries.Add(Path.Combine(ThirdPartyPath,"lib", "libprotobuf.lib"));
}
}
17.添加由protoc生成的.proto對應的.cc和.h文件,編譯UE4,會報錯,見注意事項3。
注意事項:
1.v140的安裝
2.屬性配置修改:如果UE使用protocbuff,則需要修改當前項目屬性->配置屬性->C/C++->代碼生成,選擇運行庫,設置爲多線程DLL(/MD),不然UE4會找不到lib
3.修改.cc,消除UE4產生的警告和報錯
參考鏈接:http://www.v5xy.com/?p=925