How do I generate the so library from Python code and call it in C?

Viewed 128

My goal is to generate the so library from Python code and then call it in C.  Here's how I did it

add.py

def add(x, y):
    return x+y

using command: python -m nuitka --module add.py and get add.so

using main.c call add.so:

#include <stdio.h>
#include <dlfcn.h>

int main()
{
    void* handle = dlopen("./add.so", RTLD_LAZY);

    if (!handle) {
            fputs (dlerror(), stderr);
            exit(1);
    }

    int (*add)(int a, int b);

    add = dlsym(handle, "add");
    if(dlerror()) {
      perror("dlsym error");
      abort();
    }
    int sum = add(7, 8);
    printf("7+8 = %d\n", sum);

    dlclose(handle);
    return 0;
}

then compile main.c with: gcc -o main main.c -ldl the result is:

add.so: undefined symbol: PyExc_SystemError

0 Answers
Related