[SciPy-user] C++ extensions

David Grant david at dwavesys.com
Mon Nov 8 20:29:48 EST 2004


David Grant wrote:

> Prabhu,
>
> I tried doing what you describe.  I basically am doing the following 
> as a test to see if I can get this to work.  I have the following test 
> python script:
>
> import weave
> inc_dirs=['/home/david/working_dir/python/qcd', '/usr/include']
> lib_dirs=['/home/david/working_dir/python/qcd', '/usr/lib']
> libs=['dcomplex']
> code = """
> #include "DComplex.h"
> DComplex z(2,3);
> """
> weave.inline(code, include_dirs=inc_dirs, library_dirs=lib_dirs, 
> libraries=libs, verbose=2)
>
> DComplex.cpp and DComplex.h define a class for complex numbers.  I 
> build DComplex.cpp separately using a makefile. This does:
>
> gcc -c DComplex.cpp
> ar rc libdcomplex.a DComplex.o
> ranlib libdcomplex.a
>
> This builds a static library libdcomplex.a  Then, as shown in my test 
> python script above, I use libs=['dcomplex'] and that will include 
> libdcomplex.a in the current directory.  The linking actually seems to 
> work ok!  The prooblem seems to be when python imports the file:
>
> running build_ext
> customize UnixCCompiler
> customize UnixCCompiler using build_ext
> building 'sc_89dfe56fc9f6dd6ee19b60174c27c7511' extension
> compiling C++ sources
> g++ options: '-fno-strict-aliasing -DNDEBUG -fPIC'
> compile options: '-I/home/david/working_dir/python/qcd -I/usr/include 
> -I/usr/lib/python2.3/site-packages/weave 
> -I/usr/lib/python2.3/site-packages/weave/scxx -I/usr/include/python2.3 
> -c'
> g++: 
> /home/david/.python23_compiled/sc_89dfe56fc9f6dd6ee19b60174c27c7511.cpp
> g++ -pthread -shared 
> /tmp/david/python23_intermediate/compiler_d039d257d176c3731283eef034e62e43/home/david/.python23_compiled/sc_89dfe56fc9f6dd6ee19b60174c27c7511.o 
> /tmp/david/python23_intermediate/compiler_d039d257d176c3731283eef034e62e43/usr/lib/python2.3/site-packages/weave/scxx/weave_imp.o 
> -L/home/david/working_dir/python/qcd -L/usr/lib -ldcomplex -o 
> /home/david/.python23_compiled/sc_89dfe56fc9f6dd6ee19b60174c27c7511.so
>
> Traceback (most recent call last):
>  File "test_weave.py", line 20, in ?
>    weave.inline(code,['arr','_Narr'], include_dirs=inc_dirs, 
> library_dirs=lib_dirs, libraries=libs, verbose=2)
>  File "/usr/lib/python2.3/site-packages/weave/inline_tools.py", line 
> 335, in inline
>    auto_downcast = auto_downcast,
>  File "/usr/lib/python2.3/site-packages/weave/inline_tools.py", line 
> 445, in compile_function
>    exec 'import ' + module_name
>  File "<string>", line 1, in ?
> ImportError: 
> /home/david/.python23_compiled/sc_89dfe56fc9f6dd6ee19b60174c27c7511.so: 
> undefined symbol: _ZZ13compiled_funcP7_objectS0_EN8DComplexC1Edd
>
> As you can see above there is an undefined symbol in the python .so 
> extension, which ends in ...DComplex...
>
> I've been stuck on this for an hour and I have no clue what is wrong.  
> If you or anything else can help me, it would be appreciated.

Grr....I figured it out.  I need to use the headers=[] arg.  It isn't 
documented on scipy.org.  Grrr....  I'll email Eric Jones and politely 
inform him. :-)

Dave




More information about the SciPy-User mailing list