문서 메뉴
문서 홈
/ / /
C 드라이버
/ /

C 프로그램에서 libbson 사용

이 페이지의 내용

  • bson.h 포함
  • C 메이크
  • pkg-config

하나의 헤더 파일에서 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;
}

libbson 설치에는 CMake 구성 파일 패키지가 포함되어 있습니다. CMake의 find_package 를 사용할 수 있습니다. 명령을 사용하여 libbson의 CMake 대상을 가져오고 libbson에 연결(공유 라이브러리로):

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)

CMake를 사용하지 않는 경우 pkg-config 를 사용하세요. 명령줄에서 헤더 및 라이브러리 경로를 설정합니다.

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)

돌아가기

자습서

다음

BSON 문서 만들기

이 페이지의 내용