Я новичок в C и хочу использовать библиотеку с именем json-c. Поэтому я последовал за git tutoriel и установил vcpkg. После этого я запускаю команду:
vcpkg install json-c
vcpkg integrate install
Итак, теперь vscode может видеть новую библиотеку, я вижу описания функций и т.д ..
Я создал задачу для компиляции кода:
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "gcc",
"args": [
"-g",
"lib.c",
"-o",
"lib.exe",
"-I",
"c:/Users/antoi/Downloads/vcpkg-master/vcpkg-master/installed/x86-windows/include/"
]
}
]
}
А в каталоге include находятся все заголовочные файлы.
Но когда я запускаю задачу, у меня появляются следующие ошибки:
> Executing task: gcc -g lib.c -o lib.exe -I c:/Users/antoi/Downloads/vcpkg-master/vcpkg-master/installed/x86-windows/include/ <
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\antoi\AppData\Local\Temp\ccVFMijo.o: in function `main':
c:\Users\antoi\Documents\GitHub\Hashcode2019/lib.c:23: undefined reference to `json_tokener_parse'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: c:\Users\antoi\Documents\GitHub\Hashcode2019/lib.c:24: undefined reference to `json_object_to_json_string_ext'
collect2.exe: error: ld returned 1 exit status
The terminal process terminated with exit code: 1
Terminal will be reused by tasks, press any key to close it.
И мой код:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <json-c/json.h>
int main(void)
{
struct json_object *jobj;
char *str = "{ \"msg-type\": [ \"0xdeadbeef\", \"irc log\" ], \
\"msg-from\": { \"class\": \"soldier\", \"name\": \"Wixilav\" }, \
\"msg-to\": { \"class\": \"supreme-commander\", \"name\": \"[Redacted]\" }, \
\"msg-log\": [ \
\"soldier: Boss there is a slight problem with the piece offering to humans\", \
\"supreme-commander: Explain yourself soldier!\", \
\"soldier: Well they don't seem to move anymore...\", \
\"supreme-commander: Oh snap, I came here to see them twerk!\" \
] \
}";
printf("str:\n---\n%s\n---\n\n", str);
jobj = json_tokener_parse(str);
printf("jobj from str:\n---\n%s\n---\n", json_object_to_json_string_ext(jobj, JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY));
}
Спасибо!