Linux下配置C++操作json文件
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根据实际情况修改。

浙公网安备 33010602011771号