Can't find the #include <mongoc/mongoc.h> despite installing and building from source

From /etc/release

NAME=“Rocky Linux”
VERSION=“8.7 (Green Obsidian)”
ID_LIKE=“rhel centos fedora”
PRETTY_NAME=“Rocky Linux 8.7 (Green Obsidian)”

Attempt 1:
yum install mongo-c-driver-devel

# CMakeLists.txt
find_package(mongoc-1.0 1.7)
add_executable (hello_mongoc hello_mongoc.c)
target_link_libraries(hello_mongoc PRIVATE mongo::mongoc_shared)

// hello_mongo.c

`cmake -B build -DCMAKE_C_COMPILER=gcc --log-verbose`

It can't find any of the directories

– mongoc-1.0_DIR=mongoc-1.0_DIR-NOTFOUND
– mongoc-1.0_FOUND=0
– mongoc_DIR=mongoc_DIR-NOTFOUND

Attempt 2:
However, it does work if I specify the include directories manually
# CMakeLists.txt

add_executable (hello_mongoc hello_mongoc.c)
target_link_libraries(hello_mongoc PUBLIC mongoc-1.0 bson-1.0)

Any insight?
Also, I cannot write mulitiple ticks and the code-formatting button only generates single ticks for some reason.

Hi @Diego_Hernandez

Welcome to MongoDB community!

The #1 approach with CMake’s find_package should work. You just need to set the CMAKE_PREFIX_PATH variable to point to the directory where the C driver (libmongoc) is installed.

Adding include directories manually is discouraged since the C Driver installation already supplies CMake config-file package.

And here are some more resources apart from official docs to further your learning:
Video Tutorial: Getting Started with C Driver
Written Tutorial: Getting Started with MongoDB and C | MongoDB


I believe the written tutorial and official docs links you sent they all stem from the same github docs I used to create my original post:

include directories manually is discouraged

I apologize as I forgot to mention I receive this error even when using the manually included directories.

help_mongo.c:1:10: fatal error: mongoc/mongoc.h: No such file or directory
 #include <mongoc/mongoc.h>
compilation terminated.

Although it “works” if I use #include <mongoc.h> but then I get a bunch of undefined reference errors. Even if its use is discouraged, we should still get the same intended result as the CMake route. Is it possible I’m using the wrong version? These are the packages included on my yum list installed:

mongo-c-driver.x86_64                 1.3.6-1.el7                                @epel                 
mongo-c-driver-devel.x86_64           1.3.6-1.el7                                @epel                 
mongo-c-driver-libs.x86_64            1.3.6-1.el7                                @epel 

I’ll try your suggestion with CMAKE_PREFIX_PATH and report back.

I still can’t do the triple ticks very easily.

1.3.6 is a quite old version. :slight_smile:
Can you please try with a more recent release (suggest to use the latest, ie. 1.26.1) - Releases · mongodb/mongo-c-driver · GitHub

The version string 1.3.6-1.el7 includes “el7” which means that the package came from the EPEL7 repo. Can you switch to EPEL8 repo? That has more recent 1.24.3 version
You can build from source - that should get you the latest version (1.26.1)

Building from source got me the latest version.

Is this mongo project designed to be built then added as an installed dependency?

@Diego_Hernandez you need to make sure that you remove the old packages which appear in your yum list. Also, can you share the precise sequence of commands you used to install the C driver and the output of those commands?

@Roberto_Sanchez What are you replying to? I’ve got it to build and link properly; I’m now asking how the project was intended to be used when added as a dependency.

@Diego_Hernandez I was replying to you. I misinterpreted your question as an indication that you built from source to get the latest version and still had a problem.

As to your question (now that I understand that it wasn’t an indication of a problem), the project is designed so that you can either depend on the packages provided by the distribution (e.g., EPEL8/9, Debian, Fedora, etc) assuming that they are recent enough for your requirements. You can also incorporate the components into your own project or handle it as an externally installed dependency on the system. It really depends on the requirements of your project.

Great info. Yeah, the problem was ultimately using an older version. I couldn’t get a pre-built installed even after using yum install epel-release , so building from source got me the latest version. It’s probably just a problem with my machine (GCP)

handle it as an externally installed dependency on the system.

I have a few more questions for the both of you regarding this.

  1. What is the default path prefix for the project? i.e., what happens when user omits the --prefix flag?
cd mongo-c-driver/ # latest
cmake -D ENABLE_MONGOC=ON -B build
cmake --build build
  1. Are there any examples of adding the mongo-c-library as an ExternalProject in CMakeLists.txt since there are some rather non-simple build commands?

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.