[SciPy-user] C++ extensions

David Grant david at dwavesys.com
Mon Nov 8 19:37:18 CST 2004


Robert Kern wrote:

> David Grant wrote:
>
>> 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)
>
>
> I'm pretty sure that you want to put all #include's as support code. 
> Everything in 'code' gets pasted into a function body.
>
> E.g.
>
> support_code = """#include "DComplex.h"
> """
> code = """DComplex z(2,3);
> """
> weave.inline(code, support_code=support_code, ...)

Cool, thanks.  This is similar to headers!  Probably headers just gets 
wrapped into support_code I'm guessing, which might explain why it is 
undocumented?

>
>> 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:
>
>
> When linking shared libraries, the linker will often (always?) not 
> check for missing symbols.

Right.

>
>> 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...
>
>
> The stuff before it, ...compiled_func... points, I believe to the fact 
> that the #include came inside the function body whereas the library 
> needs it outside of the function body.
>
Thanks.


-- 
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/0b80b085/david.vcf


More information about the SciPy-user mailing list