Last active
August 29, 2015 14:11
-
-
Save ChunMinChang/81ef84a674318564257e to your computer and use it in GitHub Desktop.
language binding
This file contains hidden or 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 "foo.h" | |
void | |
Foo::hello(){ | |
std::cout << "Hello world" << std::endl; | |
} | |
int | |
Foo::add(int a, int b){ | |
return a + b; | |
} | |
int | |
Foo::sub(int a, int b){ | |
return a - b; | |
} | |
int | |
Foo::mul(int a, int b){ | |
return a * b; | |
} | |
float | |
Foo::div(int a, int b){ | |
return (b)? (float)a/b : 0; | |
} |
This file contains hidden or 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 <iostream> | |
class Foo{ | |
public: | |
void hello(); | |
int add(int a, int b); | |
int sub(int a, int b); | |
int mul(int a, int b); | |
float div(int a, int b); | |
}; | |
extern "C" { | |
Foo* Foo_new(){ return new Foo(); } | |
void Foo_hello(Foo* foo){ foo->hello(); } | |
int Foo_add(Foo* foo, int a, int b){ return foo->add(a, b); } | |
int Foo_sub(Foo* foo, int a, int b){ return foo->sub(a, b); } | |
int Foo_mul(Foo* foo, int a, int b){ return foo->mul(a, b); } | |
float Foo_div(Foo* foo, int a, int b){ return foo->div(a,b); } | |
} |
This file contains hidden or 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
from ctypes import cdll, c_void_p, c_float, c_int | |
lib = cdll.LoadLibrary('./libfoo.so') | |
# You need to specify the parameter and return types needed | |
# by the functions in your library that you intend to call. | |
# Arguments types) | |
# cdll.LoadLibrary('YOUR_PATH').FUNC_NAME.argtypes = [arg_type1, arg_type2, ...] | |
# Retuen type) | |
# cdll.LoadLibrary('YOUR_PATH').FUNC_NAME.restype = [return_type] | |
# List of type : https://docs.python.org/2/library/ctypes.html#fundamental-data-types | |
class Foo(object): | |
def __init__(self): | |
# Set the arguments types and return type | |
# new) | |
lib.Foo_new.restypes = c_void_p | |
# add) | |
lib.Foo_add.argtypes = [c_void_p, c_int, c_int] | |
lib.Foo_add.restype = c_int | |
# sub) | |
lib.Foo_add.argtypes = [c_void_p, c_int, c_int] | |
lib.Foo_sub.restype = c_int | |
# mul) | |
lib.Foo_add.argtypes = [c_void_p, c_int, c_int] | |
lib.Foo_mul.restype = c_int | |
# div) | |
lib.Foo_add.argtypes = [c_void_p, c_int, c_int] | |
lib.Foo_div.restype = c_float | |
# Create a new object of Foo class | |
self.obj = lib.Foo_new() | |
def printOp(self, a, b, opStr): | |
print str(a) + ' ' + opStr + ' ' + str(b) + ' =', | |
def hello(self): | |
lib.Foo_hello(self.obj) | |
def add(self, a, b): | |
self.printOp(a, b, '+') | |
return lib.Foo_add(self.obj, a , b) | |
def sub(self, a, b): | |
self.printOp(a, b, '-') | |
return lib.Foo_sub(self.obj, a , b) | |
def mul(self, a, b): | |
self.printOp(a, b, '*') | |
return lib.Foo_mul(self.obj, a , b) | |
def div(self, a, b): | |
self.printOp(a, b, '/') | |
return lib.Foo_div(self.obj, a , b) | |
f = Foo() | |
f.hello() | |
print f.add(1,2) | |
print f.sub(3,4) | |
print f.mul(5,6) | |
print f.div(7,8) |
This file contains hidden or 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
# Compiler to use | |
CC = g++ | |
# Source file name | |
SRC = foo.cpp | |
# Object file name -> foo.o | |
OBJ = $(SRC:.cpp=.o) | |
# Shared library name -> libfoo.so | |
SHRLIB = $(addprefix lib, $(OBJ:.o=.so)) | |
# Make Commands | |
all: $(OBJ) $(SHRLIB) | |
$(OBJ): | |
$(CC) -c -fPIC $(SRC) -o $(OBJ) | |
$(SHRLIB): | |
$(CC) -shared -Wl,-soname,$(SHRLIB) -o $(SHRLIB) $(OBJ) | |
clean: | |
rm foo.o libfoo.so |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment