我试图从我的python代码中调用以下C ++方法:
TESS_API TessResultRenderer* TESS_CALL TessTextRendererCreate(const char* outputbase)
{
return new TessTextRenderer(outputbase);
}
我在如何将指针传递给方法方面遇到了困难:
遵循正确的方法吗?
textRenderer = self.tesseract.TessTextRendererCreate(ctypes.c_char)
或者我应该这样做:
outputbase = ctypes.c_char * 512
textRenderer = self.tesseract.TessTextRendererCreate(ctypes.pointer(outputbase))
上面的操作给出了错误:
TypeError: _type_ must have storage info
答案 0 :(得分:3)
你应该传递一个字符串。
例如:
self.tesseract.TessTextRendererCreate('/path/to/output/file/without/extension')
这是一个带有模拟API的通用示例。在lib.cc
:
#include <iostream>
extern "C" {
const char * foo (const char * input) {
std::cout <<
"The function 'foo' was called with the following "
"input argument: '" << input << "'" << std::endl;
return input;
}
}
使用以下命令编译共享库:
clang++ -fPIC -shared lib.cc -o lib.so
然后,在Python中:
>>> from ctypes import cdll, c_char_p
>>> lib = cdll.LoadLibrary('./lib.so')
>>> lib.foo.restype = c_char_p
>>> result = lib.foo('Hello world!')
The function 'foo' was called with the following input argument: 'Hello world!'
>>> result
'Hello world!'