0

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.

SColvin
  • 11,584
  • 6
  • 57
  • 71
  • In the gist, `json_tokener_parse` is only called and never defined. Obviously it cannot work. Maybe the function that contains it is not needed for `mong_test`? – Armin Rigo Nov 15 '13 at 19:16
  • `json_tokener_parse` is defined, it's defined [in json-c](https://github.com/json-c/json-c/blob/master/json_tokener.h) which is included in `json2bson.c` via `json.h`. I'm also sure it's called in the test function. – SColvin Nov 15 '13 at 19:20

1 Answers1

0

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.

SColvin
  • 11,584
  • 6
  • 57
  • 71