3

I can't find mongoc.h after installing Mongodb and C driver on Ubuntu 16.04.

sudo apt-get install mongodb
sudo apt-get install libmongoc-1.0-0
sudo apt-get install libbson-1.0

This is the error I get:

gcc -o mtest mtest.c -I/usr/local/include/libbson-1.0 -I/usr/local/include/libmongoc-1.0 -lmongoc-1.0 -lbson-1.0
mtest.c:1:20: fatal error: mongoc.h: No such file or directory
compilation terminated.

I checked the disk and can not find the file. Tips appreciated.

Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
RR1
  • 333
  • 1
  • 6
  • 13
  • @Nisse, what alternative is there to install other that apt-get and will this resolve my issue? I just tried to follow http://mongoc.org/libmongoc/current/installing.html – RR1 Jul 27 '18 at 19:04
  • Sorry, I don't know the answer to this. – Nisse Engström Jul 27 '18 at 19:15

2 Answers2

1

If you have installed via apt-get, then the packages almost certainly don't install to /usr/local, but to plain old /usr. What happens if you run

gcc -o mtest mtest.c -I/usr/include/libbson-1.0 -I/usr/include/libmongoc-1.0 -lmongoc-1.0 -lbson-1.0

P.S. The right way to pick up these paths is by using pkg-config instead of hard coding them, please see http://mongoc.org/libmongoc/current/tutorial.html#pkg-config

acm
  • 12,183
  • 5
  • 39
  • 68
  • It starts a whack-a-mole with new header files... gcc -o mtest mtest.c -I/usr/include/libbson-1.0 -I/usr/include/libmongoc-1.0 -lmongoc-1.0 -lbson-1.0 In file included from mtest.c:1:0: /usr/include/libmongoc-1.0/mongoc.h:22:18: fatal error: bson.h: No such file or directory compilation terminated. – RR1 Jul 27 '18 at 00:53
1

I faced the same problem, then instead installing the packages for the libraries I followed the steps below (for Ubuntu), according with this link for libmongoc :

1 - Check if you have cmake installed, if not: $ sudo apt-get install cmake

2 - to download, build sources and install libraries:

$ wget https://github.com/mongodb/mongo-c-driver/releases/download/1.14.0/mongo-c-driver-1.14.0.tar.gz
$ tar xzf mongo-c-driver-1.14.0.tar.gz
$ cd mongo-c-driver-1.14.0
$ mkdir cmake-build
$ cd cmake-build
$ cmake -DENABLE_AUTOMATIC_INIT_AND_CLEANUP=OFF ..

3 - to Test, edit a file and save as hello_mongo.c:

#include <mongoc/mongoc.h>

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

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

  /*
   * Optionally get MongoDB URI from command line
   */
  if (argc > 1) {
     uri_string = argv[1];
  }

  /*
   * Safely create a MongoDB URI object from the given string
   */
  uri = mongoc_uri_new_with_error (uri_string, &error);
  if (!uri) {
     fprintf (stderr,
            "failed to parse URI: %s\n"
            "error message:       %s\n",
            uri_string,
            error.message);
     return EXIT_FAILURE;
  }

  /*
   * Create a new client instance
   */
  client = mongoc_client_new_from_uri (uri);
  if (!client) {
     return EXIT_FAILURE;
  }

  /*
   * Register the application name so we can track it in the profile logs
   * on the server. This can also be done from the URI (see other examples).
   */
  mongoc_client_set_appname (client, "connect-example");

  /*
   * Get a handle on the database "db_name" and collection "coll_name"
   */
  database = mongoc_client_get_database (client, "db_name");
  collection = mongoc_client_get_collection (client, "db_name", "coll_name");

  /*
   * Do work. This example pings the database, prints the result as JSON and
   * performs an insert
   */
  command = BCON_NEW ("ping", BCON_INT32 (1));

  retval = mongoc_client_command_simple (
     client, "admin", command, NULL, &reply, &error);

  if (!retval) {
     fprintf (stderr, "%s\n", error.message);
     return EXIT_FAILURE;
  }

  str = bson_as_json (&reply, NULL);
  printf ("%s\n", str);

  insert = BCON_NEW ("hello", BCON_UTF8 ("world"));

  if (!mongoc_collection_insert_one (collection, insert, NULL, NULL, &error)) {
     fprintf (stderr, "%s\n", error.message);
  }

  bson_destroy (insert);
  bson_destroy (&reply);
  bson_destroy (command);
  bson_free (str);

  /*
   * Release our handles and clean up libmongoc
   */
  mongoc_collection_destroy (collection);
  mongoc_database_destroy (database);
  mongoc_uri_destroy (uri);
  mongoc_client_destroy (client);
  mongoc_cleanup ();

  return EXIT_SUCCESS;
}

4 - to compile and run the C program:

$ gcc -o hello_mongoc hello_mongoc.c -I/usr/local/include/libbson-1.0 
-/usr/local/include/libmongoc-1.0 -lmongoc-1.0 -lbson-1.0
$ ./hello_mongoc

If you're working with the raspberrypi 3, you're problaby using mongodb 2.4.14 and then libraries libbson and libmongoc must be version 1.7.0.. look these: libmongoc and libbson.

Tavares
  • 353
  • 2
  • 8
  • Your answer is missing the actual installation part of libmongoc ("executing the build" on http://mongoc.org/libmongoc/current/installing.html) – joyfantastic Feb 04 '22 at 15:01