简体   繁体   中英

Linking up C++ and Python

I am trying to set up a program in Visual Studio where I link up a C++ file and a Python file. The printing statement from the Python statement still outputs and I am able to change it. However, whenever I run the program my console says:

Start 1

2

00000000

File "C:\Users\marce\source\repos\PythonCPPSample\Release\setup.py", line 4 print("Hello everyone. My name is Marcel.")

IndentationError: expected an indented block after function definition on line 3

3

Is this really a syntax problem? What can I do to fix this? Here is my code:

source.cpp

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

using namespace std;

void main()
{
cout << "Start 1 \n";
Py_Initialize();
cout << "2\n";
PyObject* my_module = PyImport_ImportModule("setup");
cerr << my_module << "\n";
PyErr_Print();
cout << "3\n";
PyObject* my_function = PyObject_GetAttrString(my_module,
    "printsomething");
cout << "4\n";
PyObject* my_result = PyObject_CallObject(my_function, NULL);
Py_Finalize();
}

setup.py

import re
import string
def printsomething():
print("Hello everyone! My name is Marcel.")

This error message is appearing in the console application, but even if the printing statement were to be indented underneath the function in the setup.py it would not fix the problem. This is because the python file that it's linking to had not been pulled up from the correct release folder.

So, I just had to look at the path to the.py file that was shown on the console. Right click on the Source file in Solution Explorer and add existing item. Go to the file referred to by the path, and then click on it. On that file, you can fix the indentation problem and the problem as a whole.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM