linux 下C++调用python返回值(python.so)
2016-09-02 11:44
447 查看
最近项目需要,在linux系统中使用c++ 调用 python,然后需要解析python的返回值(元组),网上一搜例子一大堆,工程中调用ptyhon头文件和库文件,但是linux系统一般自带了python,本人使用的centos7中自带的python版本号为2.7.5,但并没有python.so 文件,因此从python官网上选择下载python2.7.5重新编译一下,生成so文件。
下载地址:https://www.python.org/downloads/source/
./configure --prefix=/usr/local/ --enable-shared CFLAGS=-fPIC
make
make install
linux 测试程序
#include <iostream>
using namespace std;
#include <stdio.h>
#include <stdlib.h>
#include <Python.h>
int main()
{
PyObject *pName, *pModule, *pDict, *pFunc, *pArgs, *pRetVal;
Py_Initialize();
if (!Py_IsInitialized())
return -1;
//load py script filename
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('./')");
pName = PyString_FromString("py_test");// py_test.py
pModule = PyImport_Import(pName);
if (!pModule){
printf("cant find py_test.py");
getchar();
return -1;
}
pDict = PyModule_GetDict(pModule);
if (!pDict) return -1;
//find function name
pFunc = PyDict_GetItemString(pDict, "get_str");
if (!pFunc || !PyCallable_Check(pFunc)){
printf("cant find function [add]");
getchar();
return -1;
}
//push args to stack
pArgs = PyTuple_New(2);
PyTuple_SetItem(pArgs, 0, Py_BuildValue("s", "Hello, "));
PyTuple_SetItem(pArgs, 1, Py_BuildValue("s", "C_Python"));
//call python function
pRetVal = PyObject_CallObject(pFunc, pArgs);
if (pRetVal == NULL){
printf("CalllObject return NULL");
return -1;
}
char* ret_str;
int w = 0 , h = 0;
//解析元组
PyArg_ParseTuple(pRetVal, "s,i,i",&ret_str, &w, &h);
printf("%s, %d, %d\n", ret_str, w, h);
//解析字符串
//printf("function return value:%s\r\n", PyString_AsString(pRetVal));
Py_DECREF(pName);
Py_DECREF(pArgs);
Py_DECREF(pModule);
Py_DECREF(pRetVal);
//close python
Py_Finalize();
getchar();
return 0;
}
python 测试程序
#coding:utf-8
def get_int( ):
a = 10
b = 20
return a + b
def get_str( s1, s2 ):
#return s1 + s2
#return 'Hello , TY'
return ('Hello, World', 10, 20)
取名叫 py_test.py 放到工程目录下
记得工程中加入 python 库
测试结果
Hello, World, 0, 0
下载地址:https://www.python.org/downloads/source/
./configure --prefix=/usr/local/ --enable-shared CFLAGS=-fPIC
make
make install
linux 测试程序
#include <iostream>
using namespace std;
#include <stdio.h>
#include <stdlib.h>
#include <Python.h>
int main()
{
PyObject *pName, *pModule, *pDict, *pFunc, *pArgs, *pRetVal;
Py_Initialize();
if (!Py_IsInitialized())
return -1;
//load py script filename
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('./')");
pName = PyString_FromString("py_test");// py_test.py
pModule = PyImport_Import(pName);
if (!pModule){
printf("cant find py_test.py");
getchar();
return -1;
}
pDict = PyModule_GetDict(pModule);
if (!pDict) return -1;
//find function name
pFunc = PyDict_GetItemString(pDict, "get_str");
if (!pFunc || !PyCallable_Check(pFunc)){
printf("cant find function [add]");
getchar();
return -1;
}
//push args to stack
pArgs = PyTuple_New(2);
PyTuple_SetItem(pArgs, 0, Py_BuildValue("s", "Hello, "));
PyTuple_SetItem(pArgs, 1, Py_BuildValue("s", "C_Python"));
//call python function
pRetVal = PyObject_CallObject(pFunc, pArgs);
if (pRetVal == NULL){
printf("CalllObject return NULL");
return -1;
}
char* ret_str;
int w = 0 , h = 0;
//解析元组
PyArg_ParseTuple(pRetVal, "s,i,i",&ret_str, &w, &h);
printf("%s, %d, %d\n", ret_str, w, h);
//解析字符串
//printf("function return value:%s\r\n", PyString_AsString(pRetVal));
Py_DECREF(pName);
Py_DECREF(pArgs);
Py_DECREF(pModule);
Py_DECREF(pRetVal);
//close python
Py_Finalize();
getchar();
return 0;
}
python 测试程序
#coding:utf-8
def get_int( ):
a = 10
b = 20
return a + b
def get_str( s1, s2 ):
#return s1 + s2
#return 'Hello , TY'
return ('Hello, World', 10, 20)
取名叫 py_test.py 放到工程目录下
记得工程中加入 python 库
测试结果
Hello, World, 0, 0
相关文章推荐
- linux 下python 调用C++ .so如何debug c++代码
- linux 下python 调用C++ .so如何debug c++代码
- c++调用python返回值
- paip.java c# .net php python调用c++ c dll so windows api 总结
- linux下将c++编译成so,调用该so文件
- linux环境c++程序调用shell返回值问题
- C/C++ 生成的.so文件,python 用ctypes调用
- c++调用python解析list返回值
- Linux下C++调用python,gcc和eclipse的编译方法
- c++ 调用 python 函数中文返回值乱码
- 转载 Python调用Linux下的动态库(.so)
- python中调用动态链接库(C++,linux)
- 〖Linux〗Android NDK调用已编译好的C/C++动态连接库(so文件)
- Java在linux下调用C/C++生成的so文件
- Linux使用Python调用C/C++接口
- Linux下python调用C++接口实现图片及文件的AES加解密
- paip.java c# .net php python调用c++ c dll so windows api 总结
- linux下使用swig让python调用C++(复杂版:包括多文件调用和链接库)
- linux下使用swig让python调用C++(复杂版:包括多文件调用和链接库)
- linux QT creater配置c++调用python