I have made a shared libray in C++ which has multiple code files and want to use the functions in those files in Python. How can I use ctypes to invoke these functions?
I have two code files (with one header file for each of them) which implement simple functions.
foo.cpp foo.h
palindrome.cpp palindrome.h
palindrome.h :
extern int isPalindrome(int A);
palindrome.cpp :
int isPalindrome(int A) {
if (A < 0)
return 0;
int rev = 0;
int num = A;
while (A != 0){
rev = rev * 10 + (A % 10);
A = A / 10;
}
if (rev == num)
return 1;
return 0;
}
I created the executables using :
g++ -c -Wall -Werror -fpic foo.cpp
g++ -c -Wall -Werror -fpic palindrome.cpp
and used these executables to create a shared libray.
g++ -shared -fpic -o libcalc.so foo.o palindrome.o
Now I want to use the libcalc.so to use the functions defined in different cpp files to create a wrapper module in python. On trying:
import ctypes
_sum = ctypes.CDLL('./libcalc.so')
_sum.isPalindrome.argtypes = (ctypes.c_int)
it gives
AttributeError: ./libcalc.so: undefined symbol: isPalindrome
What is the cause of this error and how can I solve it? Is there a way other than ctypes for this purpose?