Skip to content

Instantly share code, notes, and snippets.

@hasanisaeed
Created July 28, 2024 04:14
Show Gist options
  • Save hasanisaeed/736a00e30c7bbc73f02c4c5d7fc88d69 to your computer and use it in GitHub Desktop.
Save hasanisaeed/736a00e30c7bbc73f02c4c5d7fc88d69 to your computer and use it in GitHub Desktop.
My Lib
CC = gcc
CFLAGS = -Wall -fPIC
LIBNAME = my_lib
all: $(LIBNAME).so
$(LIBNAME).so: my_lib.o
$(CC) -shared -o $@ $^
my_lib.o: my_lib.c my_lib.h
$(CC) $(CFLAGS) -c my_lib.c
clean:
rm -f *.o *.so
#include "my_lib.h"
int multiply(Numbers numbers)
{
return numbers.a * numbers.b;
}
#ifndef MYLIB_H
#define MYLIB_H
typedef struct
{
int a;
int b;
} Numbers;
int multiply(Numbers numbers);
#endif
import ctypes
lib = ctypes.CDLL('./my_lib.so')
class Numbers(ctypes.Structure):
_fields_ = [("a", ctypes.c_int), ("b", ctypes.c_int)]
numbers = Numbers(3, 4)
result = lib.multiply(numbers)
print(f"{numbers.a} * {numbers.b} = {result}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment