Cannot compile mongo-c-driver example

I am trying to write a simple mongo c client. Source file (ac):

#include <stdio.h> #define MONGO_HAVE_STDINT #include <mongo.h> void mongo_init_c(mongo *con) { mongo_init(con); } int main() { return 0; } 

And I'm trying to compile it with:

 gcc -I/usr/local/include -L/usr/local/lib -lmongoc ac 

But get the error:

 ac:(.text+0xd): undefined reference to `mongo_init' 

The files / usr / local / include / mongo.h and /usr/local/lib/libmongoc.so exist

How to compile ac?

ps mongo-2.0.4, gcc-4.6, mongo-c-driver - pulled from github

Update

$ nm / usr / local / lib / libmongoc.so | grep init

 000034e0 T _init 0000dd10 T bson_init 0000c740 T bson_init_data 0000c7b0 T bson_init_finished_data 0000dc10 T bson_init_size 0000d060 T bson_iterator_init 0000a5e0 T gridfile_init 00009af0 T gridfile_writer_init 000095e0 T gridfs_init 00010a18 R initialBufferSize 00005f40 T mongo_cursor_init 00008da0 T mongo_env_sock_init 00005d90 T mongo_init 000057b0 T mongo_init_sockets 00004800 T mongo_md5_init 00005e40 T mongo_replica_set_init 00005f00 T mongo_replset_init 00005b80 T mongo_write_concern_init 

$ gcc -I / usr / local / include -L / usr / local / lib -Wall -Werror -lmongoc ac

 /tmp/cccuNEp1.o: In function `mongo_init_c': ac:(.text+0xd): undefined reference to `mongo_init' 
+6
source share
1 answer

Try linking the library after the source file, for example gcc ac -lmongoc . This is because you are using a traditional single-pass linker, which expects dependencies to be satisfied with subsequent ones, and not with the previous objects specified on the command line.

+6
source

All Articles