1

ここの手順を使用して MongoDB C ドライバーをインストールしました (「リリース tarball からのビルド」セクションの下: http://api.mongodb.com/c/current/installing.html#installing-unix、および次を取得していますMongoDB のサンプル コードをコンパイルしようとしたときのエラー:

nicholas@nicholas-CQ5715F:~$ gcc -o connect connect.c $(pkg-config --cflags --libs libmongoc-1.3.5) パッケージ libmongoc-1.3.5 が pkg-config 検索パスに見つかりませんでした。おそらく、`libmongoc-1.3.5.pc' を含むディレクトリを PKG_CONFIG_PATH 環境変数に追加する必要があります。 No package 'libmongoc-1.3.5' found connect.c:1:18: fatal error: bson.h: No such file またはディレクトリ #include ^ コンパイルが終了しました。

コードは次のとおりです。

#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;
}
4

1 に答える 1