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 !