C++调用python训练的pytorch模型(二)-----C++调用python例子

文章目录

        • python代码
        • C++代码

参考:1

参考:2

C++/Python如何在C++中使用一个Python类? (Use Python-defined class in C++)

python代码

# test_python.py
def hello(s):
    print("python: hello: %s " %s)

def add(a,b):
    print("python: a=%d,b=%d" %(a,b))
    return a+b

def add2(s1,s2):
    print("python: s1=%s,s2=%s" %(s1,s2))
    return s1+s2

class ctest: 
    def __init__(self): 
        print("python: test class nit") 
    def say_hello(): 
        print ("python: test class say hello")
    def say(self, name): 
        print ("python: test class hello: %s" % name) 
        return name
hello('zwh')
add(2,4)
add('aaa','bbb')
test = ctest()
test.say('zwt')

C++代码

#include 
#include 
#include 
using namespace std;
void init_python_env(){
	Py_Initialize();
	if(!Py_IsInitialized()){
		cout << "initialize failed"<

你可能感兴趣的:(C/C++,机器学习,linux)