Created
September 4, 2011 19:44
-
-
Save mattbierbaum/1193397 to your computer and use it in GitHub Desktop.
SWIG with C++ class example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include "kitty.h" | |
kitty::kitty(){ | |
printf("Constructor\n"); | |
variable = 100; | |
} | |
kitty::~kitty(){ | |
printf("Destructor\n"); | |
variable = 0; | |
} | |
void kitty::speak(){ | |
printf("I'm a cat.\n"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
class kitty { | |
public: | |
kitty(); | |
~kitty(); | |
void speak(); | |
void speak2(){ printf("totes works\n"); } | |
private: | |
int variable; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%module kitty | |
%{ | |
#include "kitty.h" | |
%} | |
%include "kitty.h" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# standard compile options for the c++ executable | |
FLAGS = -fPIC | |
# the python interface through swig | |
PYTHONI = -I/usr/include/python2.6/ | |
PYTHONL = -Xlinker -export-dynamic | |
# default super-target | |
all: | |
g++ -fPIC -c kitty.cc -o kitty.o | |
swig -c++ -python -o kitty_wrap.cxx kitty.i | |
g++ $(FLAGS) $(PYTHONI) -c kitty_wrap.cxx -o kitty_wrap.o | |
g++ $(PYTHONL) $(LIBFLAGS) -shared kitty.o kitty_wrap.o -o _kitty.so |
i love you
i love you
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I love you.
One more remark:
(pay attention to the position of $(LIBFLAGS)!)
A response to partyman66: your understanding is correct. E.g.
LIBFLAGS = -L/usr/local/lib -lgmpxx -lgmp
at the beginning