2016-07-24 4 views
1

Я установил драйвер MongoDB C, используя инструкции здесь (в разделе «Строительство из раздела деблокирования»: http://api.mongodb.com/c/current/installing.html#installing-unix и были получать следующие ошибки при попытке компиляции MongoDB пример кода:connect.c: 1: 18: фатальная ошибка: bson.h: нет такого файла или каталога

Николаса @ Николо-CQ5715F: ~ $ НКУ -o подключить connect.c $ (PKG-конфигурации --cflags --libs libmongoc-1.3.5) Пакет libmongoc-1.3.5 не найден в пути поиска pkg-config. Возможно, вы должны добавить каталог, содержащий `libmongoc-1.3.5.pc ' , в переменную окружения PKG_CONFIG_PATH Нет пакета' libmongoc-1.3.5 ' найдено connect.c: 1: 18: фатальная ошибка: bson.h: Нет такого файла или каталога #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; 
} 
+0

от мерзавца или высвобождает tar.gz? Я установил libbson и libmongoc из выпущенного tar.gz в debian несколько дней назад без каких-либо проблем. – YOU

+0

@YOU: Я построил его из деблокирующего tarball. Извините, я обновлю сообщение. –

+0

может быть установлен C_INCLUDE_PATH env varible, чтобы включить эти файлы заголовков – YOU

ответ

0

Вы должны загрузить модуль pkg-config, прежде чем

[[email protected] microservice]# export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig 
[[email protected] microservice]# pkg-config --modversion libbson-1.0 
1.4.2 
[[email protected] microservice]# pkg-config --modversion libmongoc-1.0 
1.4.2 
[[email protected] microservice]# gcc -o connect connect.c $(pkg-config --cflags --libs libbson-1.0 libmongoc-1.0) 
[[email protected] microservice]# ls 
connect connect.c 
+0

Я пробовал свой код, но обнаружил ошибку, которая: неопределенная ссылка на 'bson_destory', неопределенная ссылка на 'mongoc_cursor_destory' ...... Я пробую много способов решить эту проблему, но не удалось, я зеленые руки в libmongoc , можете ли вы дать совет, спасибо! – Sucy