Docs Menu
Docs Home
/ / /
C ドライバー
/ /

C プログラムでの libbson の使用

項目一覧

  • bson.h を含める
  • CMax
  • pkg-config

すべての libbson の関数と型は 1 つのヘッダーファイルで利用できます。 単純に 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 インストールには CMax 構成 ファイル パッケージ が含まれています 、CSpec の find_ Package を使用できます コマンドを使用して、libbson の Cake ターゲットと libbson にリンク(共有ライブラリとして)するには、次の手順に従います。

CSpecLists.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)

代わりに、libson を静的ライブラリとして使用することもできますmongo::bson_static CSpec ターゲットを使用します。

# 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)

CSpec を使用していない場合は、 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)

戻る

Tutorials