I am trying to call a C++ function from a python script. Here is my sample C++ and Python code.
strfunc.cpp
#include <iostream>
#include <string>
using namespace std;
string getString()
{
string hostname = "test.stack.com";
return hostname;
}
strfunc.py
import ctypes
print(ctypes.CDLL('./strfunc.so').getString())
I compiled and generated a shared library from my C++ program using the following command:
g++ -fPIC strfunc.cpp -shared -o strfunc.so
When I try to execute strfunc.py, it gives the following error:
$ ./strfunc.py
Traceback (most recent call last):
File "./strfunc.py", line 5, in <module>
print(ctypes.CDLL('./strfunc.so').getString())
File "/usr/lib64/python3.7/ctypes/__init__.py", line 372, in __getattr__
func = self.__getitem__(name)
File "/usr/lib64/python3.7/ctypes/__init__.py", line 377, in __getitem__
func = self._FuncPtr((name_or_ordinal, self))
AttributeError: ./strfunc.so: undefined symbol: getString
Please help me know how I can resolve this issue. The same thing works for int function.