1

I installed the MongoDB C driver using the instructions here (under the 'Building from a release tarball' section: http://api.mongodb.com/c/current/installing.html#installing-unix, and have been getting the following errors when trying to compile MongoDB's sample code:

nicholas@nicholas-CQ5715F:~$ gcc -o connect connect.c $(pkg-config --cflags --libs libmongoc-1.3.5) Package libmongoc-1.3.5 was not found in the pkg-config search path. Perhaps you should add the directory containing `libmongoc-1.3.5.pc' to the PKG_CONFIG_PATH environment variable No package 'libmongoc-1.3.5' found connect.c:1:18: fatal error: bson.h: No such file or directory #include ^ compilation terminated.

Here is the code:

#include <bson.h>
#include <bcon.h>
#include <mongoc.h>

int
main (int   argc,
      char *argv[])
{
   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 ();

   /*
    * Create a new client instance
    */
       client = mongoc_client_new ("mongodb://localhost:27017");

   /*
    * 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 (collection, MONGOC_INSERT_NONE,   insert, 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_client_destroy (client);
  mongoc_cleanup ();

  return 0;
}
xdg
  • 2,975
  • 19
  • 17
  • from git or releases tar.gz? I installed libbson and libmongoc from released tar.gz in debian few days ago without any problem – YOU Jul 24 '16 at 13:54
  • @YOU: I built it from the release tarball. Sorry, i'll update the post. – Nicholas Brown Jul 24 '16 at 15:00
  • may be set C_INCLUDE_PATH env varible to include that those header files – YOU Jul 25 '16 at 00:07
  • Have you solve you problems? I have the same problems with you. I have copy the libbson-1.0 and libmongoc-1.0 below in my project, but this problems always here? can you give me some advice. Thanks ! – Sucy Jun 29 '17 at 08:51
  • I saw somebody do that: **gcc -o test test.c -I/usr/local/include/libmongoc-1.0 -I/usr/local/include/libbson-1.0/ -lmongoc-1.0 -lbson-1.0**, but how can I configure in eclipse CDT? where to add the include files? – Sucy Jun 29 '17 at 08:58
  • Yeah, I have solve this problem. Move out all mongoc and bson header files out of the libbson-1.0 and libmongoc-1.0 folder, and put them under include folder. That will be ok ! Good luck! – Sucy Jul 03 '17 at 03:12
  • @Sucy Thank you! I'll try this. – Nicholas Brown Aug 16 '17 at 17:55

1 Answers1

0

You have to load the pkgconfig module before

[root@fedora20 microservice]# export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
[root@fedora20 microservice]# pkg-config --modversion libbson-1.0
1.4.2
[root@fedora20 microservice]# pkg-config --modversion libmongoc-1.0
1.4.2
[root@fedora20 microservice]# gcc -o connect connect.c $(pkg-config --cflags --libs libbson-1.0 libmongoc-1.0)
[root@fedora20 microservice]# ls
connect  connect.c
Felipe
  • 7,013
  • 8
  • 44
  • 102
  • I try your code, but find the error that: undefined reference to 'bson_destory', undefined reference to 'mongoc_cursor_destory'...... I try many method to solve this problem, but failed, I am a green hands in libmongoc, can you give some advice, thanks! – Sucy Jun 30 '17 at 08:11