Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot compile mongo-c-driver example

I try to write simple mongo c client. Source file (a.c):

#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 try to compile it with:

gcc -I/usr/local/include -L/usr/local/lib -lmongoc a.c

But get an error:

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

Files /usr/local/include/mongo.h and /usr/local/lib/libmongoc.so exists

How can I correctly compile a.c?

p.s. 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 a.c

/tmp/cccuNEp1.o: In function `mongo_init_c':
a.c:(.text+0xd): undefined reference to `mongo_init'
like image 649
user1312837 Avatar asked Feb 18 '13 11:02

user1312837


1 Answers

Try linking the library after the source file, like gcc a.c -lmongoc. This is because you're using a traditional single-pass linker, which expects to satisfy dependencies with subsequent, not previous, objects specified on the command line.

like image 177
John Zwinck Avatar answered Oct 02 '22 00:10

John Zwinck