How to use a shared library created in cython?

affonso elias ferreira junior affonso_elias at hotmail.com
Sun Apr 11 20:08:15 UTC 2021


Hi everyone, I'm trying to use a shared library created in 
Cython. example.

*********file run_cython.pyx******************************

cpdef int test(int x):
     cdef int y = 1
     cdef int i
     for i in range(1, x+1):
         y *= i
     return y
***********************************************************

************file setup.py*********************************

from distutils.core import setup
from Cython.Build import cythonize

setup(ext_modules = cythonize('run_cython.pyx'))

*********************************************************

****file Gethello.d**************************************
import core.stdc.stdio;
import core.stdc.stdlib;
import core.sys.posix.dlfcn;

extern (C) int test();

int main()
{
     int x;
     x = 2;
     printf("+main()\n");
     void* lh = dlopen("run_cython.so", RTLD_LAZY);
     if (!lh)
     {
         fprintf(stderr, "dlopen error: %s\n", dlerror());
         exit(1);
     }
     printf("libsubs.so is loaded\n");
     int function() fn = cast(int function())dlsym(lh, "test");
     char* error = dlerror();
     if (error)
     {
         fprintf(stderr, "dlsym error: %s\n", error);
         exit(1);
     }
     printf("dll() function is found\n");
     fn();
     printf("unloading run_cython.so\n");
     dlclose(lh);
     printf("-main()\n");
     return 0;
}

************************************************************************

I am using the following commands to compile

python setup.py build_ext --inplace
dmd Gethello.d
dmd Gethello.o  -defaultlib=libphobos2.so 
-L-rpath=/home/affonso/Desktop/lab7

The program compiles but the error message appears when I run it. 
dlsym error:

how can I use the shared library generated by cython in dlang? or 
how do I fix this error?

Thank you very much in advance.







More information about the Digitalmars-d-learn mailing list