Skip to content

Instantly share code, notes, and snippets.

@kmwenja
Last active November 25, 2016 04:04
Show Gist options
  • Save kmwenja/8f6602b2552460892a029d6a342f2db4 to your computer and use it in GitHub Desktop.
Save kmwenja/8f6602b2552460892a029d6a342f2db4 to your computer and use it in GitHub Desktop.
Use linux dynamic libraries in python. First run `make` then `python main.py`
Using linux dlls in python.
1. `make`
2. `python main.py`
int add(int a, int b){
return a + b;
}
import os
from ctypes import cdll
THIS_DIR = os.path.join(
os.path.dirname(os.path.abspath(__file__)))
libadd = cdll.LoadLibrary(os.path.join(THIS_DIR, "libadd.so"))
print(libadd.add(1, 3))
all: libadd.so
libadd.so: add.o
# make shared dll
gcc -shared -o libadd.so add.o
add.o:
# ensure you compile with position independent code (PIC)
gcc -c -fPIC -o add.o add.c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment