Join us Sept 17 at .local NYC! Use code WEB50 to save 50% on tickets. Learn more >
MongoDB Event
Docs Menu
Docs Home
/ / /
C 드라이버
/ /

C 프로그램에서 libbson 사용

하나의 헤더 파일 에서 libbson의 모든 기능과 유형을 사용할 수 있습니다. bson.h 를 포함하기만 하면 됩니다.

hello_bson.c
#include <stdio.h>
#include <bson/bson.h>
int
main (int argc, const char **argv)
{
bson_t *b;
char *j;
b = BCON_NEW ("hello", BCON_UTF8 ("bson!"));
j = bson_as_canonical_extended_json (b, NULL);
printf ("%s\n", j);
bson_free (j);
bson_destroy (b);
return 0;
}

The libbson installation includes a CMake config-file package, so you can use CMake's find_package command to import libbson's CMake target and link to libbson (as a shared library):

CMakeLists.txt
# Specify the minimum version you require.
find_package (bson-1.0 1.7 REQUIRED)
# The "hello_bson.c" sample program is shared among four tests.
add_executable (hello_bson ../../hello_bson.c)
target_link_libraries (hello_bson PRIVATE mongo::bson_shared)

대신 libbson을 정적 라이브러리로 사용할 수도 있습니다: mongo::bson_static CMake 타겟을 사용하세요:

# Specify the minimum version you require.
find_package (bson-1.0 1.7 REQUIRED)
# The "hello_bson.c" sample program is shared among four tests.
add_executable (hello_bson ../../hello_bson.c)
target_link_libraries (hello_bson PRIVATE mongo::bson_static)

If you're not using CMake, use pkg-config on the command line to set header and library paths:

gcc -o hello_bson hello_bson.c $(pkg-config --libs --cflags libbson-1.0)

또는 libbson에 정적으로 링크하려면 다음을 수행합니다.

gcc -o hello_bson hello_bson.c $(pkg-config --libs --cflags libbson-static-1.0)

돌아가기

Tutorials

이 페이지의 내용