C++, Python and Swig

Started by
8 comments, last by stodge 20 years, 9 months ago
I built a simple class:

class Test
{
	public:
		Test() {};
		virtual ~Test() {};

		int Get() { return 4; };
};
and created a binding header file for Swig:

// pair.i - SWIG interface

%module pair
%{
	#include "pair.h"
%}

// Parse the original header file

%include "pair.h"
I built the shared library and ran python:

Python 2.2.2 (#2, Feb  5 2003, 10:40:08)
[GCC 3.2.1 (Mandrake Linux 9.1 3.2.1-5mdk)] on linux-i386
Type "help", "copyright", "credits" or "license" for more information.
>>> import pair
>>> f = pair.new_Test()
>>> print pair.Test_Get(f)
4
So this was successful, but the Python script seems incredibly ugly and messy. Is this the cleanest solution for something like this with C++, Swig and Python or is there a better alternative? Thanks
---------------------http://www.stodge.net
Advertisement
What I mean is, is there no way to do something like this in Python with Swig (made up syntax here):

f = pair.Test.new()print f.Get()
---------------------http://www.stodge.net
Try Boost''s python library.

pair.cpp
#include <boost/python.hpp>using namespace boost::python;class Test{  public:     Test() {};     virtual ~Test() {};     int Get() { return 4; };};BOOST_PYTHON_MODULE(pair){   class_<Test>("Test")        .def("Get", &Test::Get)        ;}


Build the DLL, linking with the boost python library, and then

>>> import pair>>> p = pair.Test()>>> p.Get()4


Note: The library itself is a bit annoying to build, but if you follow the instructions it should be alright.

[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Yes, all you have to do is change your import statement slightly:



from pair import *
f = new_Test()
f.Test_Get()


Interim
That looks a lot cleaner; I was actually going to try Boost next but you beat me to it.

I presume it's safe to mix using Swig and Boost, as Swig seems to be a nice solution for exporting things like define and constants.

Thanks

Edit: Oh yeah, now I remember. The Boost tutorials are built using jam, which I don't want to touch. Still it does look interesting (reading through the docs).

[edited by - stodge on July 8, 2003 11:09:11 AM]
---------------------http://www.stodge.net
quote:Original post by stodge
Edit: Oh yeah, now I remember. The Boost tutorials are built using jam, which I don''t want to touch. Still it does look interesting (reading through the docs).


You need to use bjam to compile the library itself, but once it is done, you don''t have to use it ever again. I certainly do not.

[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Since you apparently have built the library using bjam, how are you supposed to set the ''environment variables''? I''m using WinXP, so I tried going into my Control Panel, and Adding the variables to my "User Variables" list. This however, doesn''t seem to work so well. Can y''all help me? I''m just trying to build the Boost.Python library.

Chris Pergrossi
< ctoan >
My Realm
Chris PergrossiMy Realm | "Good Morning, Dave"
quote:Original post by c t o a n
This however, doesn''t seem to work so well.

"Doesn''t work" is hardly an error description. What happens? What doesn''t happen? How do you know it doesn''t happen?


AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
Ok, I''ve gotten bjam to compile the library (using a hack, but who cares? it worked) But now I''m trying to actually USE the library, and I''m running into problems. I''m trying to compile the examples provided with the library using MSVC++ 6.0. I''ve decided to link to the library at compile time, and then should I compile the examples as DLL''s? I''m trying to build a simple app that embeds Python, but extends it with some functions, classes, etc. Can anyone explain this to me? Until then, I''ll just keep trying to simply extend Python (I''ve got the embedding part down). Thanks

Chris Pergrossi
< ctoan >
My Realm
Chris PergrossiMy Realm | "Good Morning, Dave"
Yes, the extension modules need to be compiled as DLLs.
One way to do it is to simply use Python's distutils module.

The script can be as simple as :

# setup.pyfrom distutils.core import setup, Extensionmodule1 = Extension( "modulename",                     [ "foo.cpp", "bar.cpp" ],                     libraries = [ "libboost_python" ] )setup ( name = "myext",        description = "An extension module that does cool stuff",        fullname = "My Extension That Does Stuff",        ext_modules = [ module1 ] )  


And then run the script with python setup build (I think, it's 'build', I can't quite remember at the moment)

[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

[edited by - Fruny on July 13, 2003 4:11:21 AM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement