www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to use a shared library created in cython?

reply affonso elias ferreira junior <affonso_elias hotmail.com> writes:
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.
Apr 11 2021
parent Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= <aferust gmail.com> writes:
On Sunday, 11 April 2021 at 20:08:15 UTC, affonso elias ferreira 
junior wrote:
 Hi everyone, I'm trying to use a shared library created in 
 Cython. example.

 [...]
I am on my mobile phone, and cannot reproduce. But i see that this cast(int function()) should be cast(int function(int)). Try correcting function signatures throughout the code. And extern (c) int test() is irrelevant here because you are loading it via a different mechanism.
Apr 12 2021