python extension module / external linkage

John Hunter jdhunter at nitace.bsd.uchicago.edu
Mon Aug 27 09:37:13 EDT 2001


I am building a python extension module with boost::python and am
getting an error 'ImportError: ./Trade.so: undefined symbol: mtInit'
when I try to import the module in python.  mtInit is a C function
declared in a header with 'extern "C"' and defined in an object
library that I link against when I build the python extension module.
None of my python wrapper classes use 'mtInit' directly, but one of
the classes that I am wrapping does call it in its constructor.

When I build the shared library Trade.so to import into python, I
compile it with 

g++ -shared $(LIBPATHS) $(OBJS) -o Trade.so -lboost_python -lA -lB

libA.a and libB.a are both object libraries.  mtInit is defined in
libA.a.  A class defined in libB.a uses mtInit in the constructor, as
I mentioned above.

I think I am having some kind of problem with external linkage, but I
don't know how to go about solving it.  

In the .cpp file that I use to build libB.a I have tried 

  extern "C" int mtInit(void);
  extern int mtInit(void);    

and both without the 'void' and it always compiles fine but gives me
the same runtime error in python.  

If I build a static executable (not a python extension) and link
against libA.a and libB.a, I have no problems.  I am new to the game of
dynamically loaded shared object libraries.  Any suggestions will be
welcome.

Thanks,
John Hunter

gcc-2.95.2





More information about the Python-list mailing list