Linux下配置C++操作json檔案
阿新 • • 發佈:2020-12-17
nlohmann-json
是一個用C++操作json檔案的庫,功能非常強大。本文記錄下如何在linux下用vcpkg
安裝nlohmann-json
並用CLion呼叫。
- 安裝vcpkg
- 克隆vcpkg庫
$ git clone https://github.com/microsoft/vcpkg
- 執行bootstrap-vcpkg.sh檔案
$ ./vcpkg/bootstrap-vcpkg.sh
若在執行過程中下載cmake
太慢,可去kitware自行下載對應的版本並放到vcpkg/downloads/
資料夾下,然後重新執行bootstrap-vcpkg.sh
。如在我的系統下,執行bootstrap-vcpkg.sh
cmake
的安裝包,安裝包名稱為cmake-3.18.4-Linux-x86_64.tar.gz.part
。
- 克隆vcpkg庫
- 安裝
nlohmann-json
$ ./vcpkg/vcpkg install nlohmann-json
如果出現下載錯誤(對應的github issue),可自行下載nlohmann-json
的安裝包,並解壓到[vcpkg root]/downloads
裡([vcpkg root]
根據實際情況修改),然後重新執行上述命令,如果安裝成功會出現:
Total elapsed time: 4.86 s The package nlohmann-json:x64-linux provides CMake targets: find_package(nlohmann_json CONFIG REQUIRED) target_link_libraries(main PRIVATE nlohmann_json nlohmann_json::nlohmann_json)
- 配置CLion
依次點選File->Setting->Build, Execution, Deployment->Cmake
,在CMake options中新增:
-DCMAKE_TOOLCHAIN_FILE=[vcpkg root]/scripts/buildsystems/vcpkg.cmake
([vcpkg root]
根據實際情況修改)
最後在CMakeLists.txt
中新增以下兩行:
find_package(nlohmann_json CONFIG REQUIRED) target_link_libraries(main PRIVATE nlohmann_json nlohmann_json::nlohmann_json)
其中,main
和PRIVATE
根據實際情況修改。