c++调用python(简单版)

8 篇文章 0 订阅

声明:这种方法在使用系统自带的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++

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值