python - 从 python 调用 C++ 函数并获取返回值

标签 python c++

我正在尝试从 python 脚本调用 C++ 函数。这是我的示例 C++ 和 Python 代码。

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())

我使用以下命令从我的 C++ 程序编译并生成了一个共享库:

g++ -fPIC strfunc.cpp -shared -o strfunc.so

当我尝试执行 strfunc.py 时,出现以下错误:

$ ./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

请帮助我知道如何解决这个问题。同样的事情适用于 int 函数。

最佳答案

如果您在 so 文件上使用 readelf -Ws,它将为您提供 so 库中的项目:

FUNC GLOBAL DEFAULT 12 _Z9getStringB5cxx11v

您会看到您的函数实际上就在那里,它只是有一个错位的名称。 因此,在库上调用 ctype 的正确名称是 _Z9getStringB5cxx11v()。

然而,它仍然有一些问题。 将您的方法标记为 extern 以让编译器知道它具有外部链接:

extern string getString()

或者,如果您想将它用作 getString(),您可以将其标记为 extern "C",这将禁用 C++ mangler

extern "C" string getString()

但无论哪种情况,我认为您都会发现自己有一些内存问题。我认为正确的方法是将 c 样式指针返回到字符数组并自行进行内存管理,这样的事情应该可行:

strfunc.cpp:

#include <iostream>
#include <string>

using namespace std;

char hostname[] = "test.stack.com";

extern "C" char * getString()
{

        return hostname;

}

strfunc.py:

#!/usr/bin/env python
from ctypes import *

test=cdll.LoadLibrary("./strfunc.so")
test.getString.restype=c_char_p
print(test.getString())

如果是字符串,我认为你需要弄清楚如何正确管理内存和返回类型,让 python 知道你实际上正在传递字符串。它可能是可行的,但不像上面那么容易。

关于python - 从 python 调用 C++ 函数并获取返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62871904/

相关文章:

python - 无法切换到框架

python - 非英文文本python不可读

c++ - 如何获取自上次用户登录 Windows 以来的时间?

c++ - VS2013 调试器无法再解析包含源代码路径的系统环境变量

c++ - 类内的静态 constexpr 初始化链

python - 在 Pandas 中使用正则表达式提取特定单词

python - 如何在 2 列上加入 pandas 数据框?

python - 在 python 中,为什么要从内置模块导入 'object'?

c++ - Boost Asio 回调不会被调用

c++ - 寻找对我的线程安全、无锁队列实现的批评