I am trying to understand how to pass string values between Python3 and cythonized C++ function. However I am unable to build the library with Cython.
In particular, I didn't understand how to declare the string return value and string parameter in the source.pyx. With the int type it works correctly.
The error I get during build with clang is the following:
candidate function not viable: no known conversion from 'PyObject *' (aka '_object *') to 'char *' for 1st argument
My source.pyx is the following:
cdef extern from "source.cpp":
cdef str fun(str param)
def pyfun(mystring):
return fun(mystring)
My source.cpp is:
char * fun(char *string) {
return string;
}
libcpp.stringandstd::string, that I think you are proposing, better than passingbytestochar*and then converting the return value (with typebytes) tostr?Cchar strings, especially since Py3 usesunicode. But what's best may depend on what you are doing with the strings inC++.libcpp.stringas an answer I can accept it.