Skip to content

Commit f251a60

Browse files
adrpoOpenModelica-Hudson
authored andcommitted
fix simulation code compilation on Windows
- use g++ to link the executable on Windows with -static-libstc++ - remove c++ includes from the SimulationRuntime/c/simulation/simulation_runtime.h - guard some files with extern "C" so we can always use g++ to compile the generated code Belonging to [master]: - OpenModelica/OMCompiler#2569
1 parent a32b622 commit f251a60

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

Compiler/Template/CodegenC.tpl

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ end translateModel;
110110
/* Simulation code for <%dotPath(modelInfo.name)%> generated by the OpenModelica Compiler <%getVersionNr()%>. */
111111
#if !defined(<%modelNamePrefixStr%>__MODEL_H)
112112
#define <%modelNamePrefixStr%>__MODEL_H
113+
113114
#include "openmodelica.h"
114115
#include "openmodelica_func.h"
115116
#include "simulation_data.h"
@@ -122,6 +123,10 @@ end translateModel;
122123
#include "simulation/solver/nonlinearSystem.h"
123124
#include "simulation/solver/mixedSystem.h"
124125

126+
#if defined(__cplusplus)
127+
extern "C" {
128+
#endif
129+
125130
#include <string.h>
126131
127132
#include "<%fileNamePrefix%>_functions.h"
@@ -200,7 +205,12 @@ end translateModel;
200205
#define omp_get_max_threads() 1
201206
#endif
202207
208+
#if defined(__cplusplus)
209+
}
203210
#endif
211+
212+
#endif /* !defined(<%modelNamePrefixStr%>__MODEL_H) */
213+
204214
<%\n%>
205215
>>
206216
end match
@@ -1021,6 +1031,11 @@ template simulationFile(SimCode simCode, String guid, String isModelExchangeFMU)
10211031
>>
10221032
<<
10231033
/* Main Simulation File */
1034+
1035+
#if defined(__cplusplus)
1036+
extern "C" {
1037+
#endif
1038+
10241039
<%simulationFileHeader(simCode.fileNamePrefix)%>
10251040
#include "simulation/solver/events.h"
10261041
@@ -1038,9 +1053,6 @@ template simulationFile(SimCode simCode, String guid, String isModelExchangeFMU)
10381053
/* dummy VARINFO and FILEINFO */
10391054
const FILE_INFO dummyFILE_INFO = omc_dummyFileInfo;
10401055
const VAR_INFO dummyVAR_INFO = omc_dummyVarInfo;
1041-
#if defined(__cplusplus)
1042-
extern "C" {
1043-
#endif
10441056
10451057
<%functionInput(simCode, modelInfo, modelNamePrefixStr)%>
10461058
@@ -1127,10 +1139,6 @@ template simulationFile(SimCode simCode, String guid, String isModelExchangeFMU)
11271139

11281140
<%functionInitializeDataStruc(modelInfo, fileNamePrefix, guid, delayedExps, modelNamePrefixStr, isModelExchangeFMU)%>
11291141

1130-
#ifdef __cplusplus
1131-
}
1132-
#endif
1133-
11341142
static int rml_execution_failed()
11351143
{
11361144
fflush(NULL);
@@ -1165,6 +1173,11 @@ template simulationFile(SimCode simCode, String guid, String isModelExchangeFMU)
11651173
EXIT(res);
11661174
return res;
11671175
}
1176+
1177+
#ifdef __cplusplus
1178+
}
1179+
#endif
1180+
11681181
<%\n%>
11691182
>>
11701183
%>
@@ -5774,7 +5787,7 @@ case SIMCODE(modelInfo=MODELINFO(varInfo=varInfo as VARINFO(__)), delayedExps=DE
57745787
# Makefile generated by OpenModelica
57755788

57765789
# Simulations use -O3 by default
5777-
CC=<%if boolOr(Flags.isSet(Flags.PARMODAUTO),acceptParModelicaGrammar()) then 'g++' else '<%makefileParams.ccompiler%>'%>
5790+
CC=<%if boolOr(boolOr(Flags.isSet(Flags.PARMODAUTO),acceptParModelicaGrammar()), boolOr(stringEq(makefileParams.platform, "win32"),stringEq(makefileParams.platform, "win64"))) then 'g++' else '<%makefileParams.ccompiler%>'%>
57785791
CXX=<%makefileParams.cxxcompiler%>
57795792
LINK=<%makefileParams.linker%>
57805793
EXEEXT=<%makefileParams.exeext%>

SimulationRuntime/c/simulation/simulation_runtime.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,15 @@
4646
#include <stdlib.h>
4747
#include <string.h>
4848

49-
#ifdef __cplusplus
50-
#include <string>
51-
5249
#include "linearization/linearize.h"
5350
#include "simulation/results/simulation_result.h"
5451

52+
#ifdef __cplusplus
5553
extern "C" {
54+
#endif /* cplusplus */
5655

5756
extern int initializeResultData(DATA* simData, threadData_t *threadData, int cpuTime);
5857

59-
#endif /* cplusplus */
60-
6158
extern int modelTermination; /* Becomes non-zero when simulation terminates. */
6259
extern int terminationTerminate; /* Becomes non-zero when user terminates simulation. */
6360
extern int terminationAssert; /* Becomes non-zero when model call assert simulation. */

0 commit comments

Comments
 (0)