4

cmake引用jansson库

 8 months ago
source link: http://yongqing.is-programmer.com/posts/216768.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

cmake引用jansson库

妙音 posted @ 2024年1月09日 14:50 in cxx with tags json jansson c , 18 阅读

描述

在ubuntun系统上使用clion开发c程序, cmake如何引入jansson库.

下载jansson

wget http://digip.org/jansson/releases/jansson-2.13.tar.gz
tar -xvf

编译jansson

cd jansson-2.13
mkidr build
cd build
cmake ../
make

查看编译结果

├── include
│      ├── jansson_config.h
│      └── jansson.h
├── lib
│     └── libjansson.a

clion创建C程序

File->New->Project->选中"C Executable"

CMakeLists.txt引用jansson

cmake_minimum_required(VERSION 3.22)
project(demo_task C)
set(CMAKE_C_STANDARD 11)
#自定义库, jansson放在了source目录下
set(LIBHOME "/home/xxx/workspace/source/")
# jansson
include_directories(${LIBHOME}/jansson-2.13/build/include)
link_directories(${LIBHOME}/jansson-2.13/build/lib)
link_libraries(jansson)
add_executable(demo_task main.c)

main.c使用jansson

创建demo.json并放入任意json数据, jansson读取demo.json,并打印结果
#include <stdio.h>
#include <jansson.h>
int main() {
json_t *json;
json_error_t error;
json = json_load_file("../demo.json", 0, &error);
if(!json) {
/* the error variable contains error information */
}
char *result = json_dumps(json, 0);
printf("%s\n", result);
free(result);
return 0;
}
clion运行并查看结果
评论 (0)

[取消回复评论]

昵称 登录 E-mail: *
Web:
Twitter:
当有新评论通过 E-mail 通知我
loading captcha image...
(输入验证码)

or Ctrl+Enter


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK