Libbson and MongoDB-C driver compilation

2.1k Views Asked by At

I would like to install the new update of the MongoDB-C driver but it seems that the compilation logically blocks while including the libbson library (#include <bson.h>) previously installed in the directory "/usr/local/".

How to "link" my libraries correctly in order to use both #include <bson.h> and #include <mongoc.h> ?

1

There are 1 best solutions below

2
On

Both libmongoc and libbson are automake-based projects now (./configure, make, make install). They additionally install pkg-config *.pc files that can be used to discover library installation and header paths using the pkg-config program. If you have installed to /usr/local, you might need to set PKG_CONFIG_PATH=/usr/local/lib/pkg-config (or lib64) depending if your system automatically includes that path.

A simple way to build against them is to do:

    gcc $(pkg-config --cflags --libs libmongoc-1.0) myfile.c

If you are in a Makefile, you'll need to shell out first. When using GNU make I typically do:

LIBS := $(shell pkg-config --libs libmongoc-1.0)
CFLAGS := $(shell pkg-config --cflags libmongoc-1.0)
DEBUG := -ggdb
OPTS := -O2
WARNINGS := -Wall -Werror

%.o: %.c %.h
    $(CC) -o $@ -c $(DEBUG) $(WARNINGS) $(OPTS) $(CFLAGS) $*.c

myprog: myprog.o
    $(CC) -o $@ $(DEBUG) $(WARNINGS) $(OPTS) $(LIBS) myprog.o