c++调用python(简单版)
来源:互联网 发布:陈漫离婚 知否 编辑:程序博客网 时间:2024/04/27 19:46
声明:这种方法在使用系统自带的python时很有效,但是换个环境,就会出现各种问题;具体问题出现的原因我不在这里叙述,如果你尝试了下述方法,不能成功,那就请点击这篇文章 c++调用python(复杂版)!
Step1:Create the Visual Studio Project
Open Visual Studio and create a new C++ “Win32 Console Application” project.
Step2:Change the build mode from “Debug” to “Release”
If you attempt to build a Visual Studio project that calls Python code in Debug mode the build will fail because, in debugging mode, Visual Studio will look for a library file that does not exist. The reason why this file did not exist on my machine is because the Windows Python installer did not install a version of Python compiled in Debug mode. It is possible to get a version of Python built in Debug mode, but you will have to download the Python source code and build it manually. For the purposes of this blog post we will ignore this and just change to Release mode.
Step3:Add the locations of the Python header and library files to the project properties
My Python installation directory was “C:\Python27”
, so I added “C:\Python27\include”
to the list of include directories and “C:\Python27\libs”
to the list of library directories.
Step4:Add the line “#include <Python.h>”
to the C++ source file
Here is an example code:
#include "stdafx.h"#include <iostream>#include <Python.h>int _tmain(int argc, _TCHAR* argv[]){ printf("Calling Python to find the sum of 2 and 2.\n"); // Initialize the Python interpreter. Py_Initialize(); // Create some Python objects that will later be assigned values. PyObject *pName, *pModule, *pDict, *pFunc, *pArgs, *pValue; // Convert the file name to a Python string. pName = PyString_FromString("Sample"); // Import the file as a Python module. pModule = PyImport_Import(pName); // Create a dictionary for the contents of the module. pDict = PyModule_GetDict(pModule); // Get the add method from the dictionary. pFunc = PyDict_GetItemString(pDict,"add"); // Create a Python tuple to hold the arguments to the method. pArgs = PyTuple_New(2); // Convert 2 to a Python integer. pValue = PyInt_FromLong(2); // Set the Python int as the first and second arguments to the method. PyTuple_SetItem(pArgs, 0, pValue); PyTuple_SetItem(pArgs, 1, pValue); // Call the function with the arguments. PyObject* pResult = PyObject_CallObject(pFunc, pArgs); // Print a message if calling the method failed. if (pResult == NULL) printf("Calling the add method failed.\n"); // Convert the result to a long from a Python object. long result = PyInt_AsLong(pResult); // Destroy the Python interpreter. Py_Finalize(); // Print the result. printf("The result is %d.\n", result); std::cin.ignore(); return 0;}
Step5: Create the Python File
Sample.py
def add(a, b): return a+b
Add this file to the Release directory of the Visual Studio solution. In general it should be in the same directory as the executable that is calling the Python code.
Ref
CALLING PYTHON CODE FROM C++
- c++调用python(简单版)
- Python 调用 C 语言的简单流程(一)
- Python 调用 C 语言的简单流程(二)
- python调用c语言函数的一种简单实现方法
- python调用c函数的一个简单实例
- BCB(C++)调用Python
- C中调用PYTHON
- C调用python
- C调用python
- Python 调用 C函数
- python调用c
- Python 调用C
- c中调用python
- Python调用c/c++
- c 调用python
- python 调用 c 函数
- python调用C函数库
- Python调用C
- SEAndroid安全机制框架分析
- Linux 定时关机使用 crontab /bin/sh找不到shutdown之解决
- 【Linux】利用man命令来查看头文件与函数说明
- APP 缓存数据线程安全问题,多个线程同时对同一资源进行读写问题
- Python3.x和Python2.x的区别
- c++调用python(简单版)
- 基于TI-RTOS的CC2650DK开发(9)---任务示例
- 索引选择性 集群因子 高度
- wireshark怎么抓包、wireshark抓包详细图文教程
- 自己看的20161216...
- C# .net IDE Rider入门
- 1 RBAC(基于角色的权限管理)
- ionic 升级到 ionic2报错处理
- 使用Core Data时需要避免的三个常犯错误