0

This is the structure of my program:

enter image description here

I'm trying to bind my program in C++ with a GUI in python. I'm using pybind11 and I have a python_binding.cpp file for the bind and some ".h" and ".cpp" with the methods in other directories. I include the ".h" files but somehow the python_binding.cpp it's not able to recognize them.

The file config.cpp only has one void method, "cargar_configuracion()" and this is how it looks like in the binding:

#include "Ejemplo/config.h"

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


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

The result of this is the following error:

undefined reference to `cargar_configuracion()'

What am I doing wrong? Should I have my .cpp and .h with the binding.cpp in the same directory?

Thanks in advance!

Yorch
  • 109
  • 1
  • 10
  • Are you trying to bind a certain key to a function? in that case you could use `def callback(event): print('X: %d, Y: %d' %(event.x,event.y) )` and then you need to bind left click to that function by doing this: `bind("", callback) ` – Stanley Aug 27 '18 at 11:33
  • I don't understand what do you ask, I just need to run the method "cargar_configuracion()" in python. – Yorch Aug 27 '18 at 11:40
  • What *key*/button would you want to be pressed for it to run cargar_confiuracion()? – Stanley Aug 27 '18 at 11:42
  • I have one button on my GUI to do that – Yorch Aug 27 '18 at 11:51
  • Then you need to inform us on what module youre using to display your GUI button – Stanley Aug 27 '18 at 11:52
  • I only have one module for pybind, I'm wrapping all of my program in one dll and then I import that dll in python and use it as a library – Yorch Aug 27 '18 at 13:15
  • You specified that you had a GUI button, are you using tkinter to create that button? for instance `button1 = Button(text='do command')` – Stanley Aug 27 '18 at 14:57
  • I'm using pyqt: bViewResult = QtWidgets.QPushButton('View Results', self) bViewResult.clicked.connect(self.openCSV) – Yorch Aug 28 '18 at 07:41

2 Answers2

2

Your pybind11 looks fine, this is a linker error. It looks like config.cpp is in another project within your solution, and is being built within a separate executable. You have two options here, either copy config.cpp into the same directory or reconfigure Ejemplo to be a static library and specify it as a dependency in the properties of the python wrapper project.

ChrisD
  • 927
  • 4
  • 10
0

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())
Stanley
  • 2,434
  • 18
  • 28
  • Sorry but that's not what I'm asking for, maybe I wasn't too clear in my question. I want to know how to make the declaration for a C++ method using Pybind11 because the way I did it makes an error. – Yorch Aug 28 '18 at 08:06