一、python3库准备
安装python的时候要加 --enable-shared,否则不会生成和python相关的xxx.so动态链接库文件,
而且最好安装在/usr下面,而不是/usr/local/,这样的话需要的Python.h头文件就会在/usr/include/Python3.7m,除此之外,还需要svdpi.h头文件,这个文件目录在${VCS_HOME}/include下面。
./configure --prefix=/usr/local/python3 --enable-shared
//./configure --prefix=/usr/python3 --enable-shared
二、编译
<A. vcs动态加载 .so>
文件./soft/c_call_py.cpp
//./soft/c_call_py.cpp
#include<Python.h>
#include<svdpi.h>
#include<stdlib.h>
#include<iostream>
//#define PYTHON_PATH "/home/ICer/workspace/systemverilog-python-main/1.systemverilog_demo/soft"
//#ifndef PYTHON_PATH
//#error You must define the PYTHONPATH to the python file in gcc compiler with -D
//#endif
static PyObject *pModule = NULL;
static PyObject *pFunc = NULL;
static PyObject *pDict = NULL;
static PyObject *pReturn = NULL;
static PyObject *PyList = NULL;
static PyObject *ArgList = NULL;
static PyGILState_STATE py_state;
//extern "C" must be needed if code is C++
extern "C" void c_py_init(){
char *append_path = (char *)malloc(sizeof(char) * 1000);
//append_path = PYTHON_PATH":.";
append_path = "/home/ICer/workspace/systemverilog-python-main/1.systemverilog_demo/soft:.";
setenv("PYTHONPATH",append_path,1);//Set PYTHONPATH TO working directory https://www.ibm.com/support/knowledgecenter/en/SSLTBW_2.1.0/com.ibm.zos.v2r1.bpxbd00/setenv.htm
printf("PYTHONPATH is:%s !\n", append_path);
Py_Initialize();
py_state = PyGILState_Ensure();
pModule = PyImport_ImportModule("py_