3

== installed: mongo-c-driver-1.1.0 /usr/src/mongo-c-driver-1.1.0/src/mongoc

issue : header file is in same direcroty still not getting .

=====

mongo c driver install issue : test.c error: mongoc.h: No such file or directory
root@webrtc mongoc]# vim  test.c
[root@webrtc mongoc]# gcc CFLAGS=-std=c99 test.c 192.168.0.181 27017 -o test.o
gcc: CFLAGS=-std=c99: No such file or directory
gcc: 192.168.0.181: No such file or directory
gcc: 27017: No such file or directory
test.c:18:20: error: mongoc.h: No such file or directory
test.c: In function ‘main’:
test.c:26: error: ‘mongoc_database_t’ undeclared (first use in this function)
test.c:26: error: (Each undeclared identifier is reported only once
test.c:26: error: for each function it appears in.)
test.c:26: error: ‘database’ undeclared (first use in this function)
test.c:27: error: ‘mongoc_cursor_t’ undeclared (first use in this function)
test.c:27: error: ‘cursor’ undeclared (first use in this function)

code :

http://code-trick.com/mongodb-c-driver-examples/

https://github.com/mongodb/mongo-c-driver/releases

#include <mongoc.h>
#include <stdio.h>
int
main (int   argc,
      char *argv[])

   port = (argc == 3) ? atoi(argv[2]) : 27017;

   if (strncmp (argv[1], "mongodb://", 10) == 0) {
      host_and_port = bson_strdup (argv [1]);
   } else {
      host_and_port = bson_strdup_printf("mongodb://%s:%hu", argv[1], port);
   }

   client = mongoc_client_new(host_and_port);

   if (!client) {
      fprintf(stderr, "Invalid hostname or port: %s\n", host_and_port);
      return 2;
   }
   bson_init(&ping);
   bson_append_int32(&ping, "ping", 4, 1);
   database = mongoc_client_get_database(client, "test");
   cursor = mongoc_database_command(database, 0, 0, 1, 0, &ping, NULL, NULL);
   if (mongoc_cursor_next(cursor, &reply)) {
      str = bson_as_json(reply, NULL);
      fprintf(stdout, "%s\n", str);
      bson_free(str);
   } else if (mongoc_cursor_error(cursor, &error)) {
      fprintf(stderr, "Ping failure: %s\n", error.message);
      return 3;
   }

   mongoc_cursor_destroy(cursor);
   bson_destroy(&ping);
   mongoc_client_destroy(client);
   bson_free(host_and_port);

   return 0;
}

=====

Steven Carlson
  • 925
  • 1
  • 10
  • 25
  • #include #include int main (int argc, char *argv[]) { mongoc_database_t *database; mongoc_cursor_t *cursor; mongoc_client_t *client; – Hardik K. Chauhan Jul 21 '15 at 06:49
  • int main (int argc, char *argv[]) { mongoc_database_t *database; mongoc_cursor_t *cursor; mongoc_client_t *client; const bson_t *reply; uint16_t port; bson_error_t error; bson_t ping; char *host_and_port; char *str; – Hardik K. Chauhan Jul 21 '15 at 06:50
  • Try changing `#include ` to `#include "mongoc.h"` – Sourav Ghosh Jul 21 '15 at 06:51
  • try but now bson.h is not found. – Hardik K. Chauhan Jul 21 '15 at 11:49
  • which header file contains the prototypes for the bson... functions? Where is the associated library (and its' name) located? – user3629249 Jul 23 '15 at 06:17
  • when using the main() parameters, always start by checking that the 'argc' parameter has the correct number. If not the correct number, then print a 'usage' statement and exit. – user3629249 Jul 23 '15 at 06:20
  • this line: 'gcc CFLAGS=-std=c99 test.c 192.168.0.181 27017 -o test.o' contains several problems: 1) do no use 'CFLAGS=' 2) do use '-Wall -Wextra -pedantic'. To perform a compile step only, the '-c' parameter needs to be included. the '192.168.0.181 27017' has absolutely no meaning to the gcc compiler. You might need those parameters when executing the program (which still needs to be linked after fixing all the problems with the compile step) – user3629249 Jul 23 '15 at 06:26
  • you state that the mongoc.h file is located in the current directory. So it is a user defined header file. So it should be included by '#include "mongoc.h"' so the compiler knows it is a local/user file and not a system file. – user3629249 Jul 23 '15 at 06:28

2 Answers2

2

gcc -o menu menu.c $(pkg-config --cflags --libs libmongoc-1.0) solved

0
the compile step needs a -I. parameter so it will find the header file in the same directory

The link step 'may' need a '-L<pathToLibrary> parameter of non standard library location
the link step needs a '-l<short name of mongoc library' parameter so the library will be included
user3629249
  • 16,402
  • 1
  • 16
  • 17