python extension module / external linkage

Ignacio Vazquez-Abrams ignacio at
Mon Aug 27 17:06:04 CEST 2001

On 27 Aug 2001, John Hunter wrote:

> I am building a python extension module with boost::python and am
> getting an error 'ImportError: ./ 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 to import into python, I
> compile it with
> g++ -shared $(LIBPATHS) $(OBJS) -o -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

Try building as well (passing '-shared' during the link phase). There
are actually specific rules that you should follow, but I don't think they're
entirely necessary. The book "Linux Applciation Development" by Michael K.
Johnson and Erik W. Troan (ISBN 0201308215) describes the process. In fact,
it's a book I recommend to anyone doing any kind of development for Linux in
any language.

Ignacio Vazquez-Abrams  <ignacio at>

More information about the Python-list mailing list