如何使用pybind11绑定?

2024-05-13 00:34:53 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的程序结构:

enter image description here

我试图用Python将C++中的程序绑定到GUI。我用的是pybind11,我有一条python_绑定.cpp文件的绑定和一些“.h”和“.cpp”与其他目录中的方法。我包括了“.h”文件,但不知怎么的python_绑定.cpp它无法认出它们。你知道吗

文件配置.cpp只有一个void方法“cargar_configuracion()”,在绑定中是这样的:

#include "Ejemplo/config.h"

PYBIND11_MODULE(Example, m) {
m.doc() = "Binding"; // optional module docstring


m.def("cargar_configuracion", &cargar_configuracion);

这会导致以下错误:

undefined reference to `cargar_configuracion()'

我做错什么了?我应该把我的.cpp和.h和绑定.cpp在同一个目录中?你知道吗

提前谢谢!你知道吗


Tags: 文件方法程序目录configincludeguicpp
2条回答

Change your code from:

bViewResult = QtWidgets.QPushButton('View Results', self) 
bViewResult.clicked.connect(self.openCSV)

to:

bViewResult = QtWidgets.QPushButton('View Results', self)
bViewResult.clicked.connect(cargar_configuracion())

你的pybind11看起来不错,这是一个链接器错误。看起来config.cpp在解决方案中的另一个项目中,并且正在一个单独的可执行文件中构建。你有两个选择,任何一个配置.cpp或者将Ejemplo重新配置为静态库,并在python包装器项目的属性中将其指定为依赖项。你知道吗

相关问题 更多 >