Menu Docs
Página inicial do Docs
/ / /
Driver C
/ /

Usando o libbson em seu programa em C

Nesta página

  • Incluir bson.h
  • CCriar
  • pacote-config

Todas as funções e tipos do libbson estão disponíveis em um arquivo de cabeçalho. Basta incluir 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;
}

A instalação do libbson inclui um pacote de arquivo de configuração CMake, para que você possa usar o find_package do CMake comando para importar o destino CMake do libbson e vincular ao libbson (como uma biblioteca compartilhada):

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)

Você também pode usar a libbson como uma biblioteca estática: use o destino CMake mongo::bson_static :

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

Se você não estiver usando o CMake, use pkg-config na linha de comando para definir os caminhos do cabeçalho e da biblioteca:

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

Ou para vincular estaticamente à libbson:

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

Voltar

Tutoriais

Próximo

Criar um documento BSON