Python imports and uses cdll (with linux.so file)

After one of my last questions about python & C ++ integration, I was told to use DLLs in windows. (Previous question)

This worked fine:

cl /LD A.cpp B.cpp C.pp

on a Windows environment, after setting the enable path for boost, cryptopp sources, and cryptopp libraries.

Now I am trying to do the same on Linux by creating a .so file for import via ctypes in python2.5. I did:

gcc -Wall -Wextra -pedantic A.cpp B.cpp C.cpp /usr/lib/libcryptopp.so -shared -o /test/decoding.so

and therefore the object is created normally. If the remote -shared compilation is ok, but stops because there is no main (obviously;)). Of course libcryptopp.so also exists.

But when I switch to python and import the “so” file, he said that the attribute has no “decrypt”, “encrypt” object, or anything where I am. the use of "dir" over dll objects confirms that they are not there.

external functions are defined in A.cpp as:

 int encrypt (params...) //.. return num; int decrypt (params...) //.. return num; 

also tried using:

 extern "C" encrypt (params...) ..... 

Can someone tell me what I am doing wrong?

Thanks in advance!

rags

+1
source share
2 answers

The C ++ compiler manages function names. To do what you are trying to do, you must have a prototype declaration inside

 extern "C" {...} 

It’s hard to tell from your samples what exactly you have in the source file. As already mentioned, use the nm utility to see which objects are in your shared object.

Do not compile your object without -shared. The Python loading library does not support statically related objects as far as you know.

compile your object using the g ++ compiler, it will reference the standard C ++ library, gcc does not.

+3
source

just to double something since you are using boost.

 #include <string> #include <boost/python.hpp> using namespace std; string hello(string s){ return "Hello World!"; } BOOST_PYTHON_MODULE(pyhello){ using namespace boost::python; def("hello", hello); } 

in python

 >>> import pyhello >>> print pyhello.hello() Hello World! 

just my 2 cents, sorry if this does not help you.

+1
source

Source: https://habr.com/ru/post/1387961/


All Articles