Search code examples
linkerlddynamic-linkingcc

linking a Python module: difference between linking with `ld` and with `cc`


This works:

cc leveldb_ext.cc leveldb_object.cc -o leveldb.so -I /usr/include/python2.7 -lpython2.7 -lleveldb -lsnappy -shared -lc

This does not work:

cc -I /usr/include/python2.7 -g -c leveldb_ext.cc leveldb_object.cc
ld -shared -o leveldb.so -lpython2.7 -lleveldb -lsnappy leveldb_ext.o leveldb_object.o -lc

In both cases, I don't get any compiler/linking errors. However, when trying to import it, I get this error:

$ python -c "import leveldb"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: ./leveldb.so: undefined symbol: _ZNK7leveldb6Status8ToStringEv

Why? Is there any difference between the two methods? What is the difference?


Solution

  • The order of the object files and the libraries is not the same between the two cases. The order is significant.