My CMake setting to create a shared lib in linux is something like
SET (CMAKE_CXX_FLAGS "-fPIC")
SET (LIB_UTILS_SRC
Utils.cpp
)
ADD_LIBRARY (UTILS SHARED
${LIB_UTILS_SRC}
)
Source Utils.cpp
double addTwoNumber(double x, double y)
{
return x + y;
}
When trying to access ‘addTwoNumber’ function using CTypes like
import os
import ctypes as c
libPath = '/home/AP/workspace/LearningCPP/lib/libUTILS.so'
libUTILS = c.cdll.LoadLibrary(libPath)
prototype = c.CFUNCTYPE(
c.c_double,
c.c_double,
c.c_double
)
addTwoNumber = prototype(('addTwoNumber', libUTILS))
res = addTwoNumber(c.c_double(2.3), c.c_double(3.5) )
I am getting some message like.
AttributeError: /home/AP/workspace/LearningCPP/lib/libUTILS.so:
undefined symbol: addTwoNumber
I checked the libUTILS.so using the “nm –demangle libUTILS.so” command and it clearly shows the ‘addTwoNumber’ symbol in it.
Why am I still getting the “undefined symbol” message from python ?
I am guessing there must be some compiler flags to be set so that symbols are mangle properly. Any suggestion would be appreciated !
Interesting, I usually use
numpy.ctypessince I constantly have to deal with large data sets, and never had any issues but I think I know whats going on here, it’s that the names are being mangled by the g++ compiler, I made it work this way:Makefile:
Utils.cpp
test.py
output:
note the
externkeyword this makes sure the compiler doesn’t mangle the name, you have to do some extra stuff when under windows, I did find http://wolfprojects.altervista.org/dllforpyinc.php which was kind of interesting.I hope this helps.
my machine: