python 调用 C/C++ 详解

2022年12月10日08:59:31

关于gcc以及python的版本配置建议大家去看我之前的一篇文章,里面比较详细地点明了关于32位和64的一些坑。

接下来将简单介绍python如何调用C和C++。包括调用整型,数组等情况。

python 调用C

C函数返回整型int

c代码 test.c

#include <stdio.h>
#include <stdlib.h>
int func(int a, int b)
{
	int ans = a + b;
	printf("You input %d and %d\n", a, b);
	printf("The result is %d\n", ans);
	return ans;
}

编译成so包

gcc -o testpycll_64.so -shared -fPIC test.c

其中参数‘-fPIC’:当产生共享库的时候,应该创建位置无关的代码,这会让共享库使用任意的地址而不是固定的地址,要实现这个功能,需要使用-fPIC参数。

python代码

import ctypes
import platform

ll = ctypes.cdll.LoadLibrary
lib = ll("./testpycll_64.so")
lib.func(1,3)
# platform.architecture()

运行之后的结果:
python 调用 C/C++ 详解
这里有一个问题,我是在jupyter notebook下的,只显示了C函数的返回值,对于过程打印语句并没有输出。但在命令行下是可以输出的,如下所示:
python 调用 C/C++ 详解
mypy.py 是python文件名。

C函数返回整型数组 int[]

C代码:

#include <stdio.h>
#include <stdlib.h>
int* func(int a, int b)
{
	int* res = (int *)malloc(3*sizeof(int));
	int ans = a + b;
	printf("You input %d and %d\n", a, b);
	printf("The result is %d\n", ans);

	res[0] = a;
	res[1] = b;
	res[2] = ans;
	return res;

	//return ans;
}

python 代码:需要用到numpy包
(这里原先一直返回失败,也是试了非常多的方法才解决)

import ctypes
import platform
from numpy.ctypeslib import ndpointer

ll = ctypes.cdll.LoadLibrary
lib = ll("./testpycll_64.so")

lib.func.restype = ndpointer(dtype=ctypes.c_int, shape=(3,))
res = lib.func(1,3)
print(res)

运行之后的结果:
python 调用 C/C++ 详解

C函数传入整型数组 int[]

C代码

#include <stdio.h>
#include <stdlib.h>
int func(int a[], int n)
{
	int sum = 0;
	for(int i=0; i<n; i++)
		sum += a[i];
	
	return sum;
}

python代码:
有多种写法,这里给出几种:
(1)

ll = ctypes.cdll.LoadLibrary
lib = ll("./testpycll_64.so")

INPUT = ctypes.c_int * 4;
# 实例化一个长度为2的整型数组
inputdata = INPUT();
inputdata[0] = 1;
inputdata[1] = 2;
inputdata[2] = 3;
inputdata[3] = 4;

lib.func.restype = ctypes.c_int;
res = lib.func(inputdata, 4);
# res = lib.func2(1,2);
print(res)

(2)

ll = ctypes.cdll.LoadLibrary
lib = ll("./testpycll_64.so")

pyarray = [1, 2, 3, 4, 5]
carray = (ctypes.c_int * len(pyarray))(*pyarray) 
res = lib.func(carray, len(pyarray))
print(res)

(3)搭配numpy数组

import ctypes
import platform
from numpy.ctypeslib import ndpointer
import numpy as np

ll = ctypes.cdll.LoadLibrary;
lib = ll("./testpycll_64.so");


pyarray = np.array([1,2,3,4,5,6,7,8], dtype="int32");
lib.func.argtypes = [ndpointer(ctypes.c_int, shape=(8, )), ctypes.c_int];
lib.func.restype = ctypes.c_int;
res = lib.func(pyarray, len(pyarray))
# print (np.array(pyarray))

print(res)

其余的数据类型就举一反三啦,ctypes下有很多数据类型,例如c_int, c_bool等,大家可以自己去尝试。

python 调用C++

调用方法和C类似,只是需要加上

extern "C" 
{
    ...
}

C++ 代码

#include <iostream>
#include <string>
using namespace std;
class TestClass
{
public:
    char* func();
};

char* TestClass::func() 
{
    cout << "In func without argument" << endl;
    char* temp = "In func without argument";
    return temp;
   
}


extern "C" 
{
    TestClass A;
    char* myfunc1() 
    {
        return A.func(); 
    }

}

生成so包指令:注意变成g++

g++ -o testpycll_64.so -shared -fPIC test.cpp

python代码

import ctypes
import platform
from numpy.ctypeslib import ndpointer
import numpy as np

ll = ctypes.cdll.LoadLibrary;
lib = ll("./testpycll_64.so");

lib.myfunc1.restype = ctypes.c_uint64;
res = lib.myfunc1();
res = ctypes.string_at(res);
print(res);

运行结果:
python 调用 C/C++ 详解

以上大致是python调用C/C++的方法,有不足之处还望大家指出。

  • 作者:布丁的自我修养
  • 原文链接:https://blog.csdn.net/budding0828/article/details/89354033
    更新时间:2022年12月10日08:59:31 ,共 2676 字。