Can't compile Hello, world! program using installed C driver

The version of the driver you are trying to build (branch or tag).
~ Apparently, successfully built. Version 1.22.0

Host OS, version, and architecture.
~ macOS Monterey 12.4, Apple M1

C Compiler and version.
~ Apple clang version 13.1.6 (clang-1316.0.21.2.5)
~ InstalledDir: /Library/Developer/CommandLineTools/usr/bin

  • The output of any cmake , make , or other commands executed during the build.

Compiling gives …

193 warnings generated.
ld: library not found for -l/opt/homebrew/Cellar/mongo-c-driver/1.22.0/lib/cmake/mongoc-1.0

hello.c …

#include <mongoc/mongoc.h>

int main() {
   const char *uri_string = "mongodb://localhost:27017";
   mongoc_uri_t *uri;
   mongoc_client_t *client;
   mongoc_database_t *database;
   mongoc_collection_t *collection;
   bson_t *command, reply, *insert;
   bson_error_t error;
   char *str;
   bool retval;

   /*
    * Required to initialize libmongoc's internals
    */
   mongoc_init ();


  printf( "Hello, world!\n" );

}

Content of library directory …

ls -lh /opt/homebrew/Cellar/mongo-c-driver/1.22.0/lib/cmake/mongoc-1.0
total 40
-r–r–r-- 1 james admin 1.9K 29 Jun 17:41 mongoc-1.0-config-version.cmake
-r–r–r-- 1 james admin 125B 29 Jun 17:41 mongoc-1.0-config.cmake
-r–r–r-- 1 james admin 1.4K 29 Jun 17:41 mongoc-targets-release.cmake
-r–r–r-- 1 james admin 4.8K 29 Jun 17:41 mongoc-targets.cmake

I was expecting some files with today’s date within the directory.

Any ideas? I will be happy to supply further info on request. Thanks in advance for your time.

it is either you haven’t fully built the driver or you are using the wrong library folder.
have you followed the installation steps and checked the tutorial on libmongoc — libmongoc 1.22.0 ?

1 Like

Thank you I was not aware of the document. I will study it and see if it will lead to a solution to my problem.

Thanks for you help. Problem was that I was specifying the header and include paths manually. As such the library was not being found. Using CMake worked, but produced extra files and directories. Initially, pkg-config compiled but gave hundreds of Warnings from /usr/local/include/stdlib.h

I removed Python, Xcode and Command Line Tools. Downloaded the DMG for Command Line Tools and installed. The directory /usr/local is now empty and my theory is that is where the problem arose.

I don’t know how to clean up a Mac, but try reinstalling C development environment. it seems scrambled a lot.

“CMake” is not actual builder, but a maker for files of other build systems such as “make”. You can still invoke a build command with it, but you need to pay attention to its flags and parameters.

then take a close look where it puts those builds. you will need in your mongodb compilation path.

I don’t own a Mac that I can help only so far.

You have been very helpful already !! Thank you so much :yum:

The above helped the solution to my problem

1 Like