Question

I have a python extension written in c which compiles fine however barfs

ImportError: /path/to/cmongo.so: undefined symbol: json_tokener_parse

When I try to import the module (import cmongo) in python.

What makes this strange is the the c compiles AND RUNS fine using gcc directly:

gcc --std=c99 cmongo.h json2bson.c  cmongo.c -lmongoc -ljson -o mong_test
./mong_test
# test main function prints stuff as required.

The extension worked find before adding the json2bson.c file and calls.

Both libraries (mongoc and json) and the extra compiler arg --std=c99 are included in setup.py, so I'm assuming the problem is either the plethora of other arguments python passes to gcc or a need to somehow reference json-c when importing the shared libary. However this is beyond my understanding of such things.

I know external links are frowned up, but to avoid a massive question while still providing a full explanation of the problem I've put all the relevant files in a gist.

Was it helpful?

Solution

Solved it at last!

Turns out I needed :

libraries = ['json-c', 'mongoc'],` 

in setup.py instead of

libraries = ['json', 'mongoc'],

Seems weird to me since gcc worked with -lmongoc -ljson (gcc also works with -lmongoc -ljson-c too as it happens). I guess it's something to do with the way they parse library names - eg. gcc assumed everthing after a - is part of the version number extra so json is considered the same as json-c???

For reference

ldd <library_name>.so

Helped a lot.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top