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
根據實際情況修改。