[Pythonmac-SIG] Using SWIG, C++, and OS X - and Boost.Python

Siddartha Krishnan sidd.krish at gmail.com
Fri Jul 7 17:16:49 CEST 2006


Hi,

Thanks for your help. I can now build the module and import and use  
it in Python (using SWIG)!

Regarding Boost.Python, I am unable to run bjam successfully.
I get the following error:

user409:~/boosttest siddarthakrishnan$ ls
Jamfile         Jamrules        hello.cpp
user409:~/boosttest siddarthakrishnan$ bjam -sTOOLS=darwin
Failed to find the project root for directory '.'.
Did not find a project-root.jam file there or in any of its parent  
directories.
Please consult the documentation at 'http://www.boost.org'.

I have changed the project root path in Jamrules to the Boost directory.
project-root.jam is in the directory.
Does anyone know how to solve this error.

Thanks,

Siddartha

On 6-Jul-06, at 2:37 PM, Kent Quirk wrote:

> Siddartha Krishnan wrote:
>> I'm new to using SWIG for python. I am having several problems   
>> creating a C++ extension module for python. I am using OS X 10.4  
>> with  python 2.4 and SWIG 1.3.29 (using darwinports).
>>
>> I can use SWIG and distutils in order to make a C extension  
>> module  for python, however, I cannot do so for a C++ extension  
>> module - I  get an import error:
>>
>> mie-15-203:~/swig/class_test siddarthakrishnan$ python mymod.py
>> Traceback (most recent call last):
>>    File "mymod.py", line 5, in ?
>>      import _mymod
>> ImportError: Failure linking new module: /Users/siddarthakrishnan/  
>> SWIG/class_test/_mymod.so: Symbol not found: __ZN9SomeClassC1Eii
>>    Referenced from: /Users/siddarthakrishnan/SWIG/class_test/ 
>> _mymod.so
>>    Expected in: dynamic lookup
>>
>> The setup.py file is as follows:
>>
>> # setup.py
>> from distutils.core import setup, Extension
>> setup (name = "_mymod",
>> 	version = "1.0",
>> 	maintainer = "Your Name",
>> 	maintainer_email = "your.name at domain.org",
>> 	description = "Sample Python C++ Extension",
>> 	ext_modules = [Extension('_mymod',
>> 				   sources=['mymod_wrap.cxx'])])
>>
>> Does anyone know how to create a setup.py file that works with c+ 
>> +  extension modules?
>>
>>
> I've never used SWIG, but I think your problem is related to name  
> mangling.
>
> Because C++ is designed to work with primitive linker technology  
> (which uses names and only names for symbol resolution), it can't  
> rely on the linker to be able to put together overloaded functions  
> properly. If you have functions called abs(float) and abs(int), if  
> the only name exported was abs, there'd be a conflict. So C++  
> "mangles" the names to include type information.
>
> You can tell C++ to leave the names alone (giving them C linkage  
> semantics) by using extern "C" like so:
>
> extern "C" void myfunc(int x)
> {
> }
>
> on the routines that you want to make available to Python.  
> Understand that they'll have to be pure external functions, not  
> members of classes.
>
> You should probably read and understand this:
>
> http://llama.med.harvard.edu/~fgibbons/doc/SWIG1.3.10/Manual/SWIG.html
>
> If you're into C++ and want to use the power of C++ in conjunction  
> with Python, consider using boost::python instead of SWIG.
>
>    Kent
>

Are you linking with all libraries that are needed for the extension?  
Your module seems to be missing 'SomeClass::SomeClass(int, int)'.

If you do the link step that setup.py generates manually, but add '- 
framework Python' and remove '-undefined surpress' you should get a  
list of all symbols that aren't present in the extension. However,  
don't build the final version of the extension like that.

Ronald




More information about the Pythonmac-SIG mailing list