在VS2019中使用CMake 3.18.2編譯安裝yaml-cpp庫
一、應用背景
因爲採用C++編寫控制檯程序的過程當中使用yaml文件比較多,目前使用比較的多的就是yaml-cpp這個C++ yaml解析庫了。以前寫過兩篇關於在CentOS7下編譯yaml-cpp庫和Windows10下使用VS2017編譯和使用yaml-cpp庫的文章。
最近實際項目中換成了最新版的Visual Studio 2019,我使用的Windows系統是Win7和Windows10,因此須要從新編譯基於VS2019下的yaml-cpp的Debug和Release版本的Win32的靜態lib庫,大致步驟和Windows10下使用VS2017編譯和使用yaml-cpp庫這篇博客描述的一致。php
二、下載yaml-cpp源代碼
首先從https://github.com/jbeder/yaml-cpp上下載源代碼html
git clone https://github.com/jbeder/yaml-cpp.git
使用git
拉取代碼
或者直接在https://github.com/jbeder/yaml-cpp上下載zip文件,而後解壓縮到指定的文件目錄下:
ios
三、使用VS2019和CMake3.18.3編譯yaml-cpp
首先確保已經在本身的系統下安裝了VS2019和CMake
個人系統是Windows七、VS201九、CMake 3.18.3,能夠根據須要選擇Windows十、CMake 3.19.2也行
解壓後的yaml-cpp源碼包目錄結構以下:
Github倉庫上對於在Windows10下使用yaml-cpp的CMake編譯介紹到比較簡單,具體以下圖所示:
下載好yaml-cpp源代碼並解壓到F:\rate\iot_sdk\third_part
目錄下,而後再該目錄下建立一個build
目錄,用於存放編譯過程當中的中間文件,這樣作主要是爲了避免影響源代碼,弄錯了能夠刪除重來。
我在Windows7系統下使用VS2019和CMake 3.18.3編譯yaml-cpp的命令爲:git
cmake .. -G "Visual Studio 16 2019" -A Win32 -DCMAKE_INSTALL_PREFIX=F:\rate\iot_sdk\third_part\yaml-cpp\install -DYAML_BUILD_SHARED_LIBS=OFF ..
或者github
cmake .. -G "Visual Studio 16 2019 Win32 " -DCMAKE_INSTALL_PREFIX=F:\rate\iot_sdk\third_part\yaml-cpp\install -DYAML_BUILD_SHARED_LIBS=OFF ..
上面的-G命令指定了VS2019做爲工具,-DCMAKE_INSTALL_PREFIX指定安裝目錄,
-DYAML_BUILD_SHARED_LIBS指定是否編譯動態庫的開關,ON表示編譯動態庫,OFF表示不編譯動態庫只編譯靜態庫。
而後使用VS2019打開YAML_CPP.sln工程文件,以下圖所示:
web
3.一、使用VS2019編譯Win32 Debug模式的yaml-cppd.lib
庫
選擇須要編譯的平臺和模式,如Win32/Win64以及Debug/Release
上面選擇了Debug以及Win32,而後右鍵點擊【解決方案】-》【生成解決方案】
而後會生成一些基於Win32平臺的Debug版本的靜態庫
這個yaml-cppd.lib
就是咱們使用VS2019項目編譯出來的基於Win32平臺的Debug靜態庫,在使用VS2019開發yaml-cpp的項目中須要引入這個lib庫windows
3.二、使用VS2019編譯Win32 Release模式的yaml-cpp.lib
庫
選擇Release、Win32,右鍵點擊【解決方案】-》【生成解決方案】,以下圖所示:
關於在Windows7下使用VS2019編譯Win64位的Debug庫和Release庫的方法也是相似的,對應的CMake編譯命令爲:svg
cmake .. -G "Visual Studio 16 2019 Win64 " -DCMAKE_INSTALL_PREFIX=F:\rate\iot_sdk\third_part\yaml-cpp\install -DYAML_BUILD_SHARED_LIBS=OFF ..
生成了YAML-CPP.sln項目工程後,使用VS2019打開,分別選擇Win64位的Debug或Win64的Release模式,而後右鍵點擊【解決方案】-》【生成解決方案】,會分別在相應目錄下生成yaml-cppd.lib和yaml-cpp.lib文件工具
四、在VS2019中使用yaml-cpp庫讀寫yaml文件
這個在VS2019中引入yaml-cpp庫的方式和VS2017中同樣,具體能夠參考我以前的博客Windows10下使用VS2017編譯和使用yaml-cpp庫
測試的config.yaml文件以下:測試
lastLogin: 2021-01-20 08:26:10 username: root password: 123
C++測試源代碼以下:
#include <iostream> #include <fstream> #include "yaml-cpp/yaml.h" int main() { YAML::Emitter out; out << "Hello, World!"; std::cout << "Here's the output YAML:\n" << out.c_str(); YAML::Node config = YAML::LoadFile("config.yaml"); if (config["lastLogin"]) { std::cout << "Last logged in: " << config["lastLogin"].as<std::string>() << std::endl; } const std::string username = config["username"].as<std::string>(); const std::string password = config["password"].as<std::string>(); //login(username, password); //config["lastLogin"] = getCurrentDateTime(); config["lastLogin"] = "2021-01-21 10:26:10"; std::cout << "username: " << username << ", password: " << password << std::endl; std::ofstream fout("config.yaml"); fout << config; return 0; }
五、參考資料
- CentOS7下編譯yaml-cpp庫
- Windows10下使用VS2017編譯和使用yaml-cpp庫
- windows下yaml-cpp從配置環境到使用
- jbeder/yaml-cpp
- Tutorial
- How To Emit YAML
- yaml-cpp API文檔
- How to load YAML file via yaml-cpp?
本文同步分享在 博客「雪域迷影」(CSDN)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。