Modelica外部C函数调用DLL导致退出,代码为0xffffffffc0000135



总之,我的问题是: 我如何使用mingw-gcc在Windows 11中构建一个DLL,可以用作OpenModelica模拟的外部函数?

我能够得到模拟编译和链接,但一旦它开始执行,程序返回退出代码0xFFFFFFFFC0000135,我认为这是由DLL的问题引起的。

模拟和其他文件的目录结构如下:

root
L ExternalFunctionTest.mo (model)
L Resources (folder)
L Include (folder)
L myExtLib.h (external function declaration)
L Library (folder)
L libMyExtLib.dll (build output for myExtLib.c)
L Src (folder)
L myExtLib.c (external function definition)

我在Windows 11中使用GCC (mingw64, Rev5, built by MSYS2 project 10.2.0)命令构建了DLL:

gcc -fPIC -shared -o Resources/Library/libMyExtLib.dll Resources/Src/myExtLib.c

我还尝试添加选项-falign-functions -mstackrealign -msse2 -mfpmath=sse,我注意到OpenModelica在编译自己的源文件时使用,以及添加-m64。我还尝试切换到使用OpenModelica在其tools/mingw64子目录中使用的相同编译器来构建DLL。

当我运行模拟时,所有内容都编译并正确链接,但是模拟立即崩溃并显示消息:

C:/ ... /modelica_workspace/ExternalFunctionTest.TestModel/TestModel.exe -port=49874 -logFormat=xmltcp -override=startTime=0,stopTime=1,stepSize=0.002,tolerance=1e-6,solver=dassl,outputFormat=mat,variableFilter=.* -r=C:/ ... /modelica_workspace/ExternalFunctionTest.TestModel/TestModel_res.mat -w -lv=LOG_STATS -inputPath=C:/ ... /modelica_workspace/ExternalFunctionTest.TestModel -outputPath=C:/ ... /modelica_workspace/ExternalFunctionTest.TestModel
Process crashed
Process crashed
Simulation process failed. Exited with code 0xffffffffc0000135.

一个同事能够在Linux中使用以下命令来构建.soDLL:

gcc -Wall -fPIC -shared -o Resources/Library/libmyFunction.so Resources/Src/myFunction.c

我尝试使用Modelica v1.18.0和v1.18.1(64位)。

如果有人能透露一些光,将不胜感激!

ExternalFunctionTest.mo:

内容
package ExternalFunctionTest
model TestModel

Real x(start=1);
Real y(start=2);

equation

der(x) = 1;

y = testFunction(x);

end TestModel;

function testFunction

input Real x;

output Real y;

external "C" extFunction(x, y) annotation(Library="libMyExtLib", Include="#include "myExtLib.h"");

end testFunction;

end ExternalFunctionTest;

myExtLib.c:

内容
#include "../Include/myExtLib.h"
void extFunction(const double input, double * const output)
{
*output = 2 * input;
}

myExtLib.h:

内容
#ifndef MY_EXT_LIB_H__
#define  MY_EXT_LIB_H__
void extFunction(const double input, double * const output);
#endif

跟进:

该问题与OpenModelica版本1.18中的一个问题有关,其中DLL没有在执行模拟之前添加到路径中,正如Adrian Pop在评论中所指出的那样。通过升级到最新的稳定开发版本1.19.0,问题自行解决了。

最新更新