❮ Ctypes
❯
- ctypes
ctypes - hello
examples/c/hello.c
#include <stdio.h> char * echo(char * what) { return what; } int add_int(int a, int b) { int sum = a+b; return sum; } int add_int(int a, int b) { int sum = a+b; return sum; } int main(void) { printf("hello\n"); printf("%d\n", add_int(2, 3)); printf("%s\n", echo("Foo")); return 0; }
gcc -o hello hello.c gcc -o hello.so -shared -fPIC hello.c
examples/c/hello.py
from ctypes import cdll from ctypes import c_char_p hello_lib = cdll.LoadLibrary("hello.so") print(hello_lib.add_int(4, 5)) # 9 print(hello_lib.echo('Hello World')) # 153977204 hello_lib.echo.restype = c_char_p print(hello_lib.echo('Hello World')) # Hello World