Issue
In short, my question is: How do I build a DLL in Windows 11 using mingw-gcc that can be used as an external function for a Modelica simulation in OpenModelica?
I am able to get the simulation to compile and link, but as soon as it starts execution, the program returns exit code 0xFFFFFFFFC0000135
, which I believe is caused by a problem with the DLL.
I have the following directory structure for the simulation and other files:
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)
I have built the DLL using GCC in Windows 11 using gcc (mingw64, Rev5, Built by MSYS2 project 10.2.0) command:
gcc -fPIC -shared -o Resources/Library/libMyExtLib.dll Resources/Src/myExtLib.c
I have also tried adding the options -falign-functions -mstackrealign -msse2 -mfpmath=sse
, which I noticed that OpenModelica uses when compiling its own source files, as well as adding -m64
. I have also tried switching to use the same compiler used by OpenModelica in its tools/mingw64
subdirectory to build the DLL.
When I run the simulation, everything compiles and links properly, but the simulation crashes immediately with the message:
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.
A colleague was able to get everything to work in Linux using the following command to build a .so
DLL:
gcc -Wall -fPIC -shared -o Resources/Library/libmyFunction.so Resources/Src/myFunction.c
I tried this using both Modelica v1.18.0 and v1.18.1 (64-bit).
If anyone can shed some light it would be greatly appreciated!
The contents of 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;
The contents of myExtLib.c
:
#include "../Include/myExtLib.h"
void extFunction(const double input, double * const output)
{
*output = 2 * input;
}
The contents of myExtLib.h
:
#ifndef MY_EXT_LIB_H__
#define MY_EXT_LIB_H__
void extFunction(const double input, double * const output);
#endif
Solution
Follow up:
The problem was related to an issue in OpenModelica version 1.18 where the DLL was not being added to the path prior to executing the simulation, as noted in the comments by Adrian Pop. By upgrading to the latest stable development release, 1.19.0, the problem resolved itself.
Answered By - Jonathon S. Answer Checked By - Candace Johnson (WPSolving Volunteer)