---
name: "bson_include_and_link"
section: "3"
platform: "linux"
package: "libbson-doc"
version: "1.23.1-1+deb12u3"
release: "Debian 12 (Bookworm)"
platforms: ["linux"]
url: "https://new.carta.tech/bson_include_and_link"
---

# bson\_include\_and\_link(3)

Using libbson In Your C Program

## NAME

bson\_include\_and\_link - Using libbson In Your C Program

## INCLUDE BSON.H

All libbson's functions and types are available in one header file. Simply 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;
}
```

## CMAKE

The libbson installation includes a *CMake config-file package*, so you can use 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)
```

You can also use libbson as a static library instead: 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)
```

## PKG-CONFIG

If you're not using CMake, use *pkg-config* on the command line to set header and library paths:

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

Or to statically link to libbson:

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

## AUTHOR

MongoDB, Inc

## COPYRIGHT

2017-present, MongoDB, Inc
