jit即使编译语言调用c++

时间:2022-07-25
本文章向大家介绍jit即使编译语言调用c++,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

python

  • ctypes调用dll、so到处函数
import ctypes  
ll = ctypes.cdll.LoadLibrary   
lib = ll("./libpycall.so")    
lib.foo(1, 3) 
  • python的c++扩展,三种基本函数形式
static PyObject *MyFunction( PyObject *self, PyObject *args );
static PyObject *MyFunctionWithKeywords(PyObject *self,  PyObject *args, PyObject *kw);
static PyObject *MyFunctionWithNoArgs( PyObject *self );

#include <Python.h> Py_RETURN_NONE返回空

#include <Python.h>
static PyObject *SpamError;
static PyObject *
spam_add(PyObject * self,PyObject *args)
{
	long a;
	//将python中传入的变量转化成C中的变量
	if(!PyArg_ParseTuple(args,"l",&a))
		return NULL;
	long long tmp=0;
	for(long i = 1;i<=a;i++)
		tmp +=i;
	//将C中的变量转化成python中的变量
	return Py_BuildValue("L",tmp);
}
 
//方法定义
static PyMethodDef SpamMet