首页 > 编程语言 >C++调用python(二)

C++调用python(二)

时间:2022-09-29 21:56:06浏览次数:87  
标签:__ 调用 set python nullptr C++ program argv include

目录

正文


一、基本使用方法
二、调用简单语句
三、调用函数
四、调用类
五、调用SSD目标检测算法
六、遇到的错误


回到顶部

三、调用函数

3.1 无参

-CMakeLists.txt

cmake_minimum_required(VERSION 3.9)

project(say_hello)

set(SDK_VERSION 0_0_1)
# >>> build type 
set(CMAKE_BUILD_TYPE "Release")# 指定生成的版本
set(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g2 -ggdb")
set(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")
# <<<


# >>> CXX11 
set(CMAKE_CXX_STANDARD 11)# C++ 11 编译器
SET(CMAKE_CXX_STANDARD_REQUIRED TRUE)
# <<< 


# >>> Python3 
set(PYTHON_ROOT "/home/zjh/anaconda3/envs/learn")
message("python root: " ${PYTHON_ROOT})
include_directories(${PYTHON_ROOT}/include/)
link_directories(${PYTHON_ROOT}/lib/)
# <<<

# --- generate ---
add_executable(say_hello hello.cpp)
target_link_libraries(say_hello -lpython3.6m)
  • hello.py
def say():
    print("hello")


if __name__ == "__main__":
    say()

  • hello.cpp
#include <python3.6m/Python.h>
#include <iostream>


int main(int argc, char *argv[])
{
    wchar_t *program = Py_DecodeLocale(argv[0], nullptr);
    if ( program == nullptr ){
        std::cout << "Fatal Error: cannot decode argv[0]!" << std::endl;
        return -1;
    }
    Py_SetProgramName(program);
    Py_SetPythonHome((wchar_t*)L"/home/zjh/anaconda3/envs/learn");
    Py_Initialize();
    if ( !Py_IsInitialized() ){
        std::cout << "Python init failed!" << std::endl;
return 0;
    }
    
    PyRun_SimpleString("import sys");
    PyRun_SimpleString("sys.path.append('/media/zjh/code/C++_call_python/test/test_hello')");
    
    PyObject* pModule = PyImport_ImportModule("hello");
    if ( pModule == nullptr ){
        std::cout << "module not found!" << std::endl;
        return 1;
    }
    
    PyObject* pFunc = PyObject_GetAttrString(pModule, "say");
    if ( !pFunc || !PyCallable_Check(pFunc) ){
        std::cout << "not found function add_num!" << std::endl;
        return 2;
    }
    PyObject_CallObject(pFunc, nullptr);

    if ( Py_FinalizeEx() < 0 ){
        exit(120);
    }
    PyMem_RawFree(program);
    return 0;
}

3.2 有参

  • CMakeLists.txt
    在3.1的基础上修改开始的program 和最后generate部分就行。

  • add.py

def add_num(a, b):
    print("the result {} + {} is {}".format(a, b, a+b))
    return a + b


if __name__ == "__main__":
    add_num(1, 2)

  • add.cpp
#include <python3.6m/Python.h>
#include <iostream>


int main(int argc, char *argv[])
{
    wchar_t *program = Py_DecodeLocale(argv[0], nullptr);
    if ( program == nullptr ){
        std::cout << "Fatal Error: cannot decode argv[0]!" << std::endl;
        return -1;
    }
    Py_SetProgramName(program);
    Py_Initialize();
    if ( !Py_IsInitialized() ){
        std::cout << "Python init failed!" << std::endl;
return 0;
    }
    
    PyRun_SimpleString("import sys");
    PyRun_SimpleString("sys.path.append('/home/work/C++_python/test/test_add')");
    
    PyObject* pModule = PyImport_ImportModule("add");
    if ( pModule == nullptr ){
        std::cout << "module not found!" << std::endl;
        return 1;
    }
    
    PyObject* pFunc = PyObject_GetAttrString(pModule, "add_num");
    if ( !pFunc || !PyCallable_Check(pFunc) ){
        std::cout << "not found function add_num!" << std::endl;
        return 2;
    }
    
    PyObject* args = Py_BuildValue("(ii)", 28, 103);
    PyObject* pRet = PyObject_CallObject(pFunc, args);
    Py_DECREF(args);
     
    int res = 0;
    PyArg_Parse(pRet, "i", &res);
    Py_DECREF(pRet);
    std::cout << "the res is: " << res << std::endl;

    if ( Py_FinalizeEx() < 0 ){
        exit(120);
    }
    PyMem_RawFree(program);
    return 0;
}

回到顶部

四、调用类

  • CMakeLists.txt

  • test_class.py

class Test(object):
    def __init__(self):
        self.i = 1
        print("init!")

    def modify(self):
        self.i += 1

    def do(self):
        print(self.i)
 
if __name__ == "__main__":
    test_class = Test()
    test_class.do()
    test_class.modify()
    test_class.do()

  • testClass.cpp
#include <python3.6m/Python.h>
#include <iostream>


int main(int argc, char *argv[])
{
    wchar_t *program = Py_DecodeLocale(argv[0], nullptr);
    if ( program == nullptr ){
        std::cout << "Fatal Error: cannot decode argv[0]!" << std::endl;
        return -1;
    }
    Py_SetProgramName(program);
    Py_Initialize();
    if ( !Py_IsInitialized() ){
        std::cout << "Python init failed!" << std::endl;
return 0;
    }
    
    PyRun_SimpleString("import sys");
    PyRun_SimpleString("sys.path.append('/home/work/C++_python/test/test_class')");
    
    // 1. 导入模块
    PyObject* pModule = PyImport_ImportModule("test_class");
    if ( pModule == nullptr ){
        std::cout << "module not found!" << std::endl;
        return 1;
    }

    PyObject* pTestDict = PyModule_GetDict(pModule);
    
    
    // 2. 导入模块中方法或类
    PyObject* pTestClass = PyDict_GetItemString(pTestDict, "Test");

    // 3. 创建实例
    PyObject* pTestInstance = nullptr;
    if ( PyCallable_Check(pTestClass) ){
        pTestInstance = PyObject_CallObject(pTestClass, nullptr);
    }
    
    // 4. 调用类方法
    PyObject_CallMethod(pTestInstance, "do", nullptr);
    PyObject_CallMethod(pTestInstance, "modify", nullptr);
    PyObject_CallMethod(pTestInstance, "do", nullptr);

    if ( Py_FinalizeEx() < 0 ){
        exit(120);
    }
    PyMem_RawFree(program);
    return 0;
}

注:如果类函数有参数,可以参照4.2中方法

标签:__,调用,set,python,nullptr,C++,program,argv,include
From: https://www.cnblogs.com/mxnote/p/16743235.html

相关文章

  • C++指针入门
    C++指针入门目录C++指针入门C++中变量位置C++中指针的声明利用指针实现swap()C++中变量位置#include<iostream>usingnamespacestd;intmain(){intvar1;......
  • 浅析 C++ 调用 Python 模块
    浅析C++调用Python模块作为一种胶水语言,Python能够很容易地调用C、C++等语言,也能够通过其他语言调用Python的模块。Python提供了C++库,使得开发者能很方便......
  • C++ CMake 使用 Python3
    C++CMake使用Python3CMakeListst设置#如果使用的是非系统目录下的Python可以通过指定Python3_ROOT_DIR改变查找路径#set(Python3_ROOT_DIR"${CMAKE_SOURCE_D......
  • Python操作数据库
    python使用MySQL主要有两个模块,pymysql(MySQLdb)和SQLAchemy。pymysql(MySQLdb)为原生模块,直接执行sql语句,其中pymysql模块支持python2和python3,MySQLdb只支持python2,两者......
  • python实现找到列表中第二大的数字
    '''给定一个长度大于3的列表,里面数字是无序的,且数字不重复,如何找到第二大的数字?例如列表a=[1,3,6,2,7,9],找到的结果就应该是:7下面给出六种解决方案'''a=[1,3,6......
  • libcurl 调用 curl_easy_getinfo( ) 返回错误码对照
    curl_easy_getinfo()函数原型声明如下:#include<curl/curl.h>CURLcodecurl_easy_getinfo(CURL*curl,CURLINFOinfo,...);https://curl.se/libcurl/c/curl_easy_ge......
  • Python基础7
    今日内容概要数据类型内置方法理论整型相关操作浮点型相关操作字符串相关操作列表相关操作可变与不可变今日内容详细数据类型内置方法理论数据类型......
  • Python3安装psutil报错
    报错:pip3installpsutilcompilationterminated.error:command'gcc'failedwithexitstatus1解决:yuminstallpython3-devel继续安装:[root@centos......
  • 快手Python笔试题【杭州多测师】【杭州多测师_王sir】
    deftest():foriinrange(10):ifi%2!=0:print("helloworld")else:passtest()#调用函数packagecom.duoc......
  • 学习python-Day66
    今日学习内容前后端开发模式API接口postman使用序列化和反序列化restful规范drf:第三方app>>>快速实现符合restful规范的接口视图类,都是继承APIView......