0

How to implement UPPAAL external function files in calls to python scripts. Functions that use python scripts in dynamically linked cpp files, such as Py_Initialize(), will report an error. Syntax checking shows that the xxx.so file cannot be found.

The external functions linked in the UPPAAL tool are as follows:

// Place global declarations here.
int a = 3;
int b = 5;

import "/home/lyt/Uppaal/project/case2/demo.so" {
    int linkPythonfunc(int a,int b);
};

External functions are written and compiled in c++, where python scripts are called:

#include<python3.8/Python.h>
#include<iostream>

using namespace std;

extern "C" int linkPythonfunc(int a,int b);

extern "C" int linkPythonfunc(int a,int b) {
      
      Py_Initialize();//使用python之前,要调用Py_Initialize();这个函数进行初始化
      
      PyRun_SimpleString("import sys");
      PyRun_SimpleString("sys.path.append('./')");
      
      PyObject* pModule = PyImport_ImportModule("func");
      
      PyObject* pFunc = PyObject_GetAttrString(pModule,"add");
      
      PyObject* pArgs = PyTuple_New(2);
      
      PyTuple_SetItem(pArgs,0,Py_BuildValue("i",a));
      PyTuple_SetItem(pArgs,1,Py_BuildValue("i",b));
      
      PyObject* pReturn = PyEval_CallObject(pFunc,pArgs);
      
      int nResult;
      
      PyArg_Parse(pReturn, "i", &nResult);

      Py_Finalize();
      return nResult;
}

To compile a create a shared object file:

lyt@ubuntu:~/Uppaal/project/case2$ g++ -std=c++17 -fPIC -c -o demo.o demo.cpp -lpython3.8
lyt@ubuntu:~/Uppaal/project/case2$ gcc -shared -o demo.so demo.o

However, UPPAAL displays that ”Could not load library named /home/lyt/UPPAAL/project/case2/demo.so“

lynn
  • 1
  • Perhaps Uppaal could not load the libraries that `demo.so` depends on. Could you post the output of `ldd demo.so` ? – mariusm Apr 07 '23 at 10:00
  • I am also confused by the letter cases in your path: source and prompt uses `Uppaal`, but the error message says `UPPAAL`. Could you check that your paths are correctly spelled out? Linux paths are case sensitive. – mariusm Apr 07 '23 at 10:04
  • Hi, I am sorry to see your reply so late. The output of `ldd demo.so` is as follows: `lyt@ubuntu:~/Uppaal/project/case2$ ldd demo.so linux-vdso.so.1 (0x00007fff19bcd000) libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fb994ff2000) /lib64/ld-linux-x86-64.so.2 (0x00007fb9951fd000)` – lynn Jun 20 '23 at 14:19
  • could you please look at the updated documentation here: https://docs.uppaal.org/language-reference/system-description/declarations/external-functions/ in particular debugging with `strace` which may provide more clues why the library loading fails. I think code like yours is a good candidate to be included into https://github.com/UPPAALModelChecker/uppaal-libs unfortunately it's an exam week for me, so I have no time. – mariusm Jun 20 '23 at 15:15
  • Your python code seems to be loading a library `func`, which is supposed to be in the current path (`sys.path.append("./")`). The current directory in path is relative and is often the source of a lot of confusion, I would recommend to replace all relative paths with absolute paths. Then another common issue is the `libc` version mismatch: make sure that all binaries (verifyta, your library and the `func` library) use the same or newer `libc` that they were compiled against. – mariusm Jun 20 '23 at 15:23
  • Your python code does not do anything special, but loads yet another shared library. I think, it would be better to create a library which loads the `func` library directly and avoid python altogether, unless you actually compute something in python, but this code does not do anything interesting. – mariusm Jun 20 '23 at 15:25

0 Answers0