I have a few simple .c programs that look something like this
example.c:
#include <stdlib.h>
#include <stdio.h>
#include <setter.h>
char *storage = NULL;
char *get_storage() {
return storage;
}
void update_storage(char *new) {
set_storage(new);
}
int main(void) {
return 0;
}
setter.h:
void set_storage(char *new_string);
setter.c:
#include <setter.h>
#include <string.h>
#include <stdlib.h>
extern char *storage;
void set_storage(char *new_string) {
if (storage == NULL) {
storage = malloc(sizeof(char) * 13);
}
memcpy(storage, new_string, 13);
}
Which I compile like so:
gcc -shared -o example.so -fPIC example.c setter.c -I.
I then have a simple python script that loads this .so file and tries to execute the update_storage and get_storage functions.
import ctypes
libds = ctypes.CDLL('./libexample.so')
libds.update_storage.argtypes = [ctypes.c_char_p]
libds.get_storage.restype = ctypes.c_char_p
libds.update_storage(ctypes.create_string_buffer("Hello World!".encode()))
res = libds.get_storage()
print(res)
However when I run the Python script I get the following error. Does anyone know why?
AttributeError: ./libexample.so: undefined symbol: update_storage