繁体   English   中英

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

[英]Modelica external C function call to DLL results in exit with code 0xffffffffc0000135

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

我能够让模拟进行编译和链接,但是一旦它开始执行,程序就会返回退出代码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)

我使用 gcc (mingw64, Rev5, Built by MSYS2 project 10.2.0) 命令在 Windows 11 中使用 GCC 构建了 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.

一位同事使用以下命令构建了一个.so DLL,让所有东西都可以在 Linux 中运行:

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,问题自行解决。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM