C/C++:conan包管理工具的使用
官网 https://conan.io/
中文 https://conan.org.cn/
安装conan(以macos为例)
brew install conanconan --version
Conan version 2.7.1
项目结构
.
├── CMakeLists.txt
├── conanfile.txt
└── src└── main.c
conanfile.txt
可以参考:https://conan.io/center/recipes/json-c
[requires]
json-c/0.14
[generators]
CMakeDeps
CMakeToolchain
CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
project(compressor C)find_package(json-c REQUIRED)add_executable(${PROJECT_NAME} src/main.c)
target_link_libraries(${PROJECT_NAME} json-c::json-c)
main.c
#include <stdio.h>
#include "json.h"int main(int argc, char const *argv[])
{// Create a new JSON objectjson_object *jobj = json_object_new_object();// Add some data to the JSON objectjson_object_object_add(jobj, "name", json_object_new_string("John Doe"));json_object_object_add(jobj, "age", json_object_new_int(30));// Convert JSON object to stringconst char *jstr = json_object_to_json_string(jobj);printf("JSON string: %s\n", jstr);// Free the JSON objectjson_object_put(jobj);return 0;
}
$ conan install . --output-folder=build --build=missing
$ cd build
$ cmake .. -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake -DCMAKE_BUILD_TYPE=Release
$ cmake --build .
$ ./compressor
输出结果
JSON string: { "name": "John Doe", "age": 30 }
参考文章
https://github.com/json-c/json-c
https://docs.conan.io/2/tutorial/consuming_packages/build_simple_cmake_project.html