Skip to content

Instantly share code, notes, and snippets.

@mattbierbaum
Created September 4, 2011 19:44
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save mattbierbaum/1193397 to your computer and use it in GitHub Desktop.
Save mattbierbaum/1193397 to your computer and use it in GitHub Desktop.
SWIG with C++ class example
#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");
}
#include <stdio.h>
class kitty {
public:
kitty();
~kitty();
void speak();
void speak2(){ printf("totes works\n"); }
private:
int variable;
};
%module kitty
%{
#include "kitty.h"
%}
%include "kitty.h"
# 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
@AnnaLynn
Copy link

I love u

@Hermitter
Copy link

I love u

@Bigpig4396
Copy link

I love you

@standbyme
Copy link

I.. I love you

@thkim-cochl
Copy link

I ❤️ you

@Silcet
Copy link

Silcet commented Feb 20, 2020

After banging my head helplessly against the walls of swig and g++, this repository showed me there was an open door I was ignoring. I love you

@rkrishnasanka
Copy link

why is this not there in the documentation? I feel like a couple of examples this would just make life so much easier

@partyman66
Copy link

partyman66 commented Apr 6, 2021

I don't see a definition of LIBFLAGS in the makefile. Could anyone elaborate on what that should have for a value here?

Would that just have something like "-L PathToLibsHere" as a value?

@leiyubiao
Copy link

I Love you.

@OChicken
Copy link

I love you.
One more remark:

all:
	swig -c++ -python demo.i 
	g++ $(FLAGS) $(PYTHONI) -c demo.cpp      -o demo.o
	g++ $(FLAGS) $(PYTHONI) -c demo_wrap.cxx -o demo_wrap.o
	g++ $(PYTHONL) -shared demo.o demo_wrap.o $(LIBFLAGS) -o _demo.so

(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

I don't see a definition of LIBFLAGS in the makefile. Could anyone elaborate on what that should have for a value here?

Would that just have something like "-L PathToLibsHere" as a value?

@ikraduya
Copy link

i love you

@JinyuChata
Copy link

i love you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment