I am using ctypes to call a C code:
mysum.cpp
//global variables and required functions are defined
int mysum(int ECG_sample)
{
int HR;
ecg_wave_sample = ECG_sample;
Filter_CurrentECG_sample(&ecg_wave_sample, &ecg_filterout);
Calculate_HeartRate(ecg_filterout,&global_HeartRate); // calculate HeartRate
HR = global_HeartRate;
return HR;
}
g++ -shared -o mysum.so -fPIC mysum.cpp
used to create the .so
file
Python wrapper:
libname = '/home/yasaswini/hp2-notebooks/notebooks/Algorithm_testing_on_database/mysum_example /mysum.so'
libdir = './'
lib=ctl.load_library(libname, libdir)
py_add_one = lib.mysum
py_add_one.restype = ctypes.c_int
py_add_one.argtypes = [ctypes.c_int]
sample = -145
results = py_add_one(sample)
I am getting this error
AttributeError Traceback (most recent call last) <ipython-input-75-858227ec99e5> in <module>
6 # 1. open the shared library
7 #mylib = ctypes.CDLL(libfile)
----> 8 py_add_one = lib.mysum
9
10 # 2. tell Python the argument and result types of function mysum
~/anaconda3/lib/python3.7/ctypes/__init__.py in __getattr__(self, name)
375 if name.startswith('__') and name.endswith('__'):
376 raise AttributeError(name)
--> 377 func = self.__getitem__(name)
378 setattr(self, name, func)
379 return func
~/anaconda3/lib/python3.7/ctypes/__init__.py in __getitem__(self, name_or_ordinal)
380
381 def __getitem__(self, name_or_ordinal):
--> 382 func = self._FuncPtr((name_or_ordinal, self))
383 if not isinstance(name_or_ordinal, int):
384 func.__name__ = name_or_ordinal
AttributeError: /home/yasaswini/hp2-notebooks/notebooks/Algorithm_testing_on_database /mysum_example/mysum.so: undefined symbol: mysum
Can someone point out what exactly I am doing wrong here?