C/C++执行Python代码

Python脚本:

import sys


def start():
    print('Hello World')

C/C++示例:

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

using namespace std;

int main()
{
	Py_Initialize();
	if (!Py_IsInitialized())
	{
		cout << "Py_IsInitialized() fail" << endl;
		return 0;
	}
	
	PyRun_SimpleString("import hello");
	PyRun_SimpleString("hello.start()");

	Py_Finalize();

	cout << "exit" << endl;
	getchar();


	return 0;
}

猜你喜欢

转载自my.oschina.net/u/3489228/blog/1811038