• C++ call python with cmake


    cmake python3

    vcpkg install python3
    

    cmake file

    cmake_minimum_required(VERSION 3.0.0)
    project(c++_python VERSION 0.1.0 LANGUAGES C CXX)
    
    # python
    find_package(Python3 COMPONENTS Development REQUIRED)
    
    add_executable(c++_python main.cpp)
    
    # libs
    target_link_libraries(${PROJECT_NAME} PRIVATE Python3::Python)
    

    1. python as string

    #include 
    int main(int, char**){
        Py_Initialize();
        PyRun_SimpleString("print('hello')");
        Py_Finalize();
        return 0;
    }
    

    2. Call python from File

    #include
    #include
    #include
    #include 
    int main(int, char**){
        std::string filename="../test.py";
        std::wstring wfilename=std::wstring(filename.begin(),filename.end());
        std::wstring mode=L"r";
        FILE* file;
        Py_Initialize();
        file=_Py_wfopen(wfilename.c_str(),mode.c_str());
        // PyRun_SimpleString("print('hello')");
        PyRun_SimpleFile(file,filename.c_str());
        Py_Finalize();
        return 0;
    }
    

    3. Call python function

    pyhelper.hpp

    #ifndef PYHELPER_HPP
    #define PYHELPER_HPP
    #pragma once
    
    #include 
    
    class CPyInstance
    {
    public:
    	CPyInstance()
    	{
    		Py_Initialize();
    	}
    
    	~CPyInstance()
    	{
    		Py_Finalize();
    	}
    };
    
    class CPyObject
    {
    private:
    	PyObject *p;
    public:
    	CPyObject() : p(NULL)
    	{}
    
    	CPyObject(PyObject* _p) : p(_p)
    	{}
    
    	
    	~CPyObject()
    	{
    		Release();
    	}
    
    	PyObject* getObject()
    	{
    		return p;
    	}
    
    	PyObject* setObject(PyObject* _p)
    	{
    		return (p=_p);
    	}
    
    	PyObject* AddRef()
    	{
    		if(p)
    		{
    			Py_INCREF(p);
    		}
    		return p;
    	}
    
    	void Release()
    	{
    		if(p)
    		{
    			Py_DECREF(p);
    		}
    
    		p= NULL;
    	}
    
    	PyObject* operator ->()
    	{
    		return p;
    	}
    
    	bool is()
    	{
    		return p ? true : false;
    	}
    
    	operator PyObject*()
    	{
    		return p;
    	}
    
    	PyObject* operator = (PyObject* pp)
    	{
    		p = pp;
    		return p;
    	}
    
    	operator bool()
    	{
    		return p ? true : false;
    	}
    };
    
    #endif
    

    then call function:

    	CPyInstance hInstance;
    
    	//import module
    	CPyObject pName = PyUnicode_FromString("pyemb3");
    	CPyObject pModule = PyImport_Import(pName);
    
    	if(pModule)
    	{
    		CPyObject pFunc = PyObject_GetAttrString(pModule, "getInteger");
    		if(pFunc && PyCallable_Check(pFunc))
    		{
    			//call func
    			CPyObject pValue = PyObject_CallObject(pFunc, NULL);
    			printf_s("C: getInteger() = %ld\n", PyLong_AsLong(pValue));
    		}
    		else
    		{
    			printf("ERROR: function getInteger()\n");
    		}
    	}
    	else
    	{
    		printf_s("ERROR: Module not imported\n");
    	}
    
  • 相关阅读:
    java api System类
    Map,Set和哈希表的使用
    吃透BGP,永远绕不开这些基础概述,看完再也不怕BGP了!
    2022已经过去一半了,你不会还不知道这个做短视频必备工具吧
    ubuntu22.04编译PBRT-v4
    在KubeSphere启动服务网格Istio并解决解决ContainerCreating问题
    数字调制与星座图
    软件测试八款优秀的API安全测试工具,会用三款工作效率能提升50%
    Linux服务器上测试TCP/UDP端口的连通性
    P1160 队列安排题解【STL双向链表】
  • 原文地址:https://blog.csdn.net/qq_44092699/article/details/138452956