2

I'm using Python 3.8.5 (64-bit) and Dev-C++ (Code generation ISO C++11, Includes Python's include folder and Libraries also has Python's Lib folder).

I designed a very simple and barebones ttk GUI in Python and all I need is for my .cpp to call the GUI and send and receive data from it.

So far, I've noticed I need #include <Python.h> and to use Py_Initialize(); and Py_Finalize(); for something. I'm aware that I can call the .cpp from the Python GUI's source file, but my teacher wants it the other way around.

Is there any way to accomplish this?

Edit 1: For example, if I run the following code:

#include <iostream>
#include <Python.h>

using namespace std;

int main()
{
    Py_Initialize();
    cout << Py_GetVersion() << endl;
    PyRun_SimpleString("print('C++ back-end & Python front-end')");
    Py_Finalize();
    return 1;
}

Then I get the error: Python-C++.cpp:(.text+0x10): undefined reference to `__imp_Py_Initialize', GetVersion, SimpleStringFlags and for Finalize too.

Can I do it in Visual Studio? I'm tearing my hair out here.

Edit 2: I got Embedded Python to work in Visual Studio. Now, how do I import my Interface.Py with my .cpp and how do I use it as a GUI?

  • Just “to call the GUI and send and receive data from it” might be a tall order, and could have a fair number of meanings. More details on the goal, please? – Davis Herring Aug 09 '20 at 02:25
  • @DavisHerring My homework is to simulate a university enrollment program. Our professor told us to create a GUI in Python and then have C++ (we can use DevC++ or Visual Studio) call the GUI since the GUI has entry boxes and buttons that indicate whether the user is a student or administrator, wants to enroll or do other stuff, etc. I only need to be able to call the Interface.py from the .cpp and somehow link them. – Daniel Pantigoso Aug 09 '20 at 03:56
  • https://stackoverflow.com/questions/37238645/how-to-open-external-programs-in-python Perhaps this would help you... –  Aug 09 '20 at 07:10
  • @HokoLomal That was what I found, but that is not what our professor wants. She wants to just compile and run the .cpp/exe, and that handles everything. It opens the tkinter GUI (Interface.py), and the only thing the GUI does is receive input/entries and sends it to the .cpp. The .cpp handles all the back-end calculations. – Daniel Pantigoso Aug 09 '20 at 07:17

1 Answers1

0
//Daniel Pantigoso e Isaac Brenes
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <stdio.h>
#include <conio.h>
#include <errno.h>
#include <Python.h>

using namespace std;

int main()
{
    FILE* scr;
    Py_Initialize();

    scr = fopen("Interface.py", "r");
    PyRun_SimpleFile(scr, "r");

    Py_Finalize();
    return 1;
}

You have to do a couple of tinkering with Visual Studio's options, but it's doable. Visual studio has to match your Python installation (64-bit Python - x64 VS, 32-bit Python - x86). Project properties has to have:

  • C/C++
  • Linker

In C/C++ General: Additional Include Directories: click the button on the right -> <Edit...> -> folder button -> ... button -> navigate to where your Python is installed -> Python -> Python(your version) -> include -> Select Folder -> Add -> Ok Preprocessor: Preprocessor Definitions: click the button on the right -> <Edit...> -> Copy paste this: _CRT_SECURE_NO_WARNINGS -> Ok

In Linker General: Additional Library Directories (you will have to do this twice because you need to add two folders): click the button on the right -> <Edit...> -> folder button -> ... button -> navigate to where your Python is installed -> Python -> Python(your version) -> Lib & libs (you have to add these two, this is why you have to do it twice) -> Select Folder -> Add -> Ok

You're done.

  • This is the trivial part; how are you going to use the C++ program other than as a replacement for `python foo.py` as a command? – Davis Herring Aug 09 '20 at 15:39
  • @DavisHerring I have no idea and my partner for the homework and I scrapped the idea because it was draining too much time and there weren't enough resources available. Even though we were able to embed Python in C++ and call a Tkinter window, passing data from the Tkinter entry fields to the C++ back-end was just impossible. No reference wherever we looked. – Daniel Pantigoso Aug 09 '20 at 15:57
  • It’s not fundamentally hard to provide a module that the embedded Python script can import and that provides callback functions that the script can register with Tkinter. But with no information at all about how the C++ code works, it’s hard to be sure that this is what you want or to provide a meaningful example. Absent that, this comment and this answer don’t answer the question. – Davis Herring Aug 09 '20 at 16:22
  • I understand it's not fundamentally *hard*, but it's sort of running around in the dark when I don't know what I'm doing when embedding and how exactly I'm doing things wrong :/ If I didn't have to turn this in in an hour and a half, I'd provide a more meaningful example with the C++ code. I'll update it after I turn it in and get some sleep, since I went right through the night while working on this. – Daniel Pantigoso Aug 09 '20 at 16:29