BSON_INCLUDE_AND_LINK(3)
π BSON_INCLUDE_AND_LINK(3) β the libbson "how do I even use this" page π
okay bestie, so this is the page that tells you how to actually get libbson into your little project π«Ά it lives in the libbson manual, and it's the one you want when you're staring at your compiler like "why don't you know what bson_t is". we've all been there. no cap.
β¨ INCLUDE BSON.H β¨
so like, the whole entire point of libbson is that all of its functions and types are chillin' in one single header file. that's it. that's the vibe. you literally just include 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;
}
look at her. iconic. makes a bson doc, turns it into canonical extended json, prints it, cleans up after herself. main character energy fr π
β¨ CMAKE β¨
bestie if you're a CMake girlie, you're soooo covered π the libbson installation ships with a CMake config-file package, so you can hit up 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)
but wait, there's more π¦ you can also use libbson as a static library instead β just use the mongo::bson_static CMake target:
# 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)
the way that it's literally just one word differentβ¦ bson_shared vs bson_staticβ¦ obsessed π
β¨ PKG-CONFIG β¨
not a CMake person? no worries, we don't gatekeep π if you're not using CMake, use pkg-config on the command line to set your header and library paths:
gcc -o hello_bson hello_bson.c $(pkg-config --libs --cflags libbson-1.0)
or, if you wanna statically link to libbson:
gcc -o hello_bson hello_bson.c $(pkg-config --libs --cflags libbson-static-1.0)
see how the static one is libbson-static-1.0? don't mix them up bestie, i'm begging π
π friendly reminder: those$(...)bits are doing the actual heavy lifting β pkg-config is handing gcc the--libsand--cflagsyou need. don't delete them and then act surprised.
β¨ AUTHOR β¨
MongoDB, Inc π
β¨ COPYRIGHT β¨
2017-present, MongoDB, Inc πΈ
and that's the whole page, bestie!! you're officially a libbson linker now. go forth and BCON_NEW your little heart out π¦β¨
this was libbson version 1.26.0, dated Mar 31, 2024, from the BSON_INCLUDE_AND_LINK(3) page β mwah, love you, bye ππ
π¦ where this page comes from
- ubuntu noble: libbson-doc 1.26.0-1.1ubuntu2 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms