Created
July 28, 2024 04:14
-
-
Save hasanisaeed/736a00e30c7bbc73f02c4c5d7fc88d69 to your computer and use it in GitHub Desktop.
My Lib
This file contains 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
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 |
This file contains 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 "my_lib.h" | |
int multiply(Numbers numbers) | |
{ | |
return numbers.a * numbers.b; | |
} |
This file contains 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
#ifndef MYLIB_H | |
#define MYLIB_H | |
typedef struct | |
{ | |
int a; | |
int b; | |
} Numbers; | |
int multiply(Numbers numbers); | |
#endif |
This file contains 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
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