BSON_INCLUDE_AND_LINK(3)

πŸ“¦
version 1.26.0 Β· 3 versions
, in ubuntu noble

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 --libs and --cflags you need. don't delete them and then act surprised.

✨ AUTHOR ✨

MongoDB, Inc πŸ‘‘


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