[SciPy-user] C++ extensions

David Grant david at dwavesys.com
Mon Nov 8 19:04:55 CST 2004


Prabhu Ramachandran wrote:

>>>>>>"DG" == David Grant <david.grant at telus.net> writes:
>>>>>>            
>>>>>>
>
>    DG> I looked at some Weave information, but that didn't help,
>    DG> because I obviously can't run this in-line since it is many
>    DG> files.
>
>That seems like an incorrect assumption.  Weave will let you call any
>function you want from within the inlined code.  From the information
>you have provided, I assume you want to do something like this in
>Python:
>
> a = Numeric.array(...)
> # Setup your matrix the way you want.
>
> call_c_code_that_does_stuff_to_array(a)
>
> # Continue work from Python...
>
>So simply define call_c_code_that_does_stuff_to_array and write some
>simple C++ code that does what you want.  This is definitely doable
>from weave.  The question is what 'output' does your blackbox produce?
>If its a number/array then they are easy to handle since weave will
>let you do that.  When you call inline you can pass headers and link
>to libraries that you have built.  So in theory you can build your
>blackbox C++ code as a library, and call its functions from your
>inlined code.
>  
>
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.

>Of course, you can use SWIG or Boost.Python also.  But weave is still
>an option.
>
>cheers,
>prabhu
>
>_______________________________________________
>SciPy-user mailing list
>SciPy-user at scipy.net
>http://www.scipy.net/mailman/listinfo/scipy-user
>
>  
>


-- 
David J. Grant
Scientific Officer
Intellectual Property
D-Wave Systems Inc.
tel: 604.732.6604
fax: 604.732.6614


**************************
CONFIDENTIAL COMMUNICATION

This electronic transmission, and any documents attached hereto, is
confidential. The information is intended only for use by the recipient
named above. If you have received this electronic message in error,
please notify the sender and delete the electronic message. Any
disclosure, copying, distribution, or use of the contents of
information received in error is strictly prohibited.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: david.vcf
Type: text/x-vcard
Size: 334 bytes
Desc: not available
Url : http://www.scipy.net/pipermail/scipy-user/attachments/20041108/2cdb3704/david.vcf


More information about the SciPy-user mailing list