【问题标题】:connect.c:1:18: fatal error: bson.h: No such file or directoryconnect.c:1:18:致命错误:bson.h:没有这样的文件或目录
【发布时间】:2016-07-24 13:48:30
【问题描述】:

我使用此处的说明安装了 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) 在 pkg-config 搜索路径中找不到软件包 libmongoc-1.3.5。 也许你应该添加包含 `libmongoc-1.3.5.pc' 的目录 到 PKG_CONFIG_PATH 环境变量 找不到包'libmongoc-1.3.5' connect.c:1:18:致命错误:bson.h:没有这样的文件或目录 #包括 ^ 编译终止。

代码如下:

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

【问题讨论】:

  • 来自 git 还是发布 tar.gz?我几天前在debian中从发布的tar.gz安装了libbson和libmongoc,没有任何问题
  • @YOU:我是从发行版 tarball 构建的。抱歉,我会更新帖子。
  • 可以设置 C_INCLUDE_PATH 环境变量以包含那些头文件
  • 你的问题解决了吗?我和你有同样的问题。我在我的项目中复制了下面的 libbson-1.0 和 libmongoc-1.0,但是这个问题总是在这里吗?你能给我一些建议吗?谢谢!
  • 我看到有人这样做: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,但是如何在 Eclipse CDT 中进行配置?在哪里添加包含文件?

标签: c mongodb mongodb-c


【解决方案1】:

你必须先加载 pkgconfig 模块

[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

【讨论】:

  • 我尝试了你的代码,但发现错误:未定义引用'bson_destory',未定义引用'mongoc_cursor_destory'......我尝试了很多方法来解决这个问题,但失败了,我是libmongoc的新手,能否给点建议,谢谢!
猜你喜欢
  • 2018-06-15
  • 1970-01-01
  • 1970-01-01
  • 2017-05-09
  • 2021-05-15
  • 2014-10-31
  • 2022-01-11
  • 2021-09-19
相关资源
最近更新 更多