The following code gets stuck with the last line when compiling on windows (haven't tried in Linux). What can I do to try to find out what is stopping from fnishing the compilation?
import fmutemplate
# Porting inc.c (a particular model)
const
MODEL_IDENTIFIER="inc"
MODEL_GUID="{8c4e810f-3df3-4a00-8276-176fa3c9f008}"
#define model size
NUMBER_OF_REALS= 0
NUMBER_OF_INTEGERS=1
NUMBER_OF_BOOLEANS=0
NUMBER_OF_STRINGS=0
NUMBER_OF_STATES=0
NUMBER_OF_EVENT_INDICATORS=0
const
counter = 0
proc setStartValues(comp:ptr ModelInstance) {.exportc: "MODEL_IDENTIFIER_$1".} =
comp.i[counter] = 1
proc calculateValues(comp:ptr ModelInstance) {.exportc: "MODEL_IDENTIFIER_$1".}=
if comp.state == modelInitializationMode:
# set first time event
comp.eventInfo.nextEventTimeDefined = fmi2True
comp.eventInfo.nextEventTime = 1 + comp.time
proc eventUpdate(comp:ptr ModelInstance, eventInfo:ptr fmi2EventInfo, timeEvent:cint, isNewEventIteration:cint) {.exportc: "MODEL_IDENTIFIER_$1".} =
if timeEvent != 0:
comp.i[counter] += 1;
if comp.i[counter] == 13:
eventInfo.terminateSimulation = fmi2True
eventInfo.nextEventTimeDefined = fmi2False
else:
eventInfo.nextEventTimeDefined = fmi2True
eventInfo.nextEventTime = 1 + comp.time
{.passC: "-Ifmi/shared/include".}
#[
{.emit: """
/*INCLUDESECTION*/ // if you want this placed near the top of the generated code
#include "fmi/fmuTemplate.h"
""".}
]#
{.compile: "fmi/fmuTemplate.c".}
When I comment the last line it finishes fine. But if not it gets stuck as follows:c:\Users\c\Documents\nim\fmi>nim c prueba.nim Hint: used config file 'C:\Users\c\Documents\instalado\nim\v1.0.0\config\nim.cfg' [Conf] Hint: system [Processing] Hint: widestrs [Processing] Hint: io [Processing] Hint: prueba [Processing] Hint: fmutemplate [Processing] Hint: types [Processing] Hint: time_t [Processing] c:\Users\c\Documents\nim\fmi\prueba.nim(6, 4) Hint: 'MODEL_GUID' is declared but not used [XDeclaredButNotUsed] c:\Users\c\Documents\nim\fmi\prueba.nim(11, 4) Hint: 'NUMBER_OF_BOOLEANS' is declared but not used [XDeclaredButNotUsed] c:\Users\c\Documents\nim\fmi\prueba.nim(10, 4) Hint: 'NUMBER_OF_INTEGERS' is declared but not used [XDeclaredButNotUsed] c:\Users\c\Documents\nim\fmi\prueba.nim(13, 4) Hint: 'NUMBER_OF_STATES' is declared but not used [XDeclaredButNotUsed] c:\Users\c\Documents\nim\fmi\prueba.nim(12, 4) Hint: 'NUMBER_OF_STRINGS' is declared but not used [XDeclaredButNotUsed] c:\Users\c\Documents\nim\fmi\prueba.nim(5, 4) Hint: 'MODEL_IDENTIFIER' is declared but not used [XDeclaredButNotUsed] c:\Users\c\Documents\nim\fmi\prueba.nim(14, 4) Hint: 'NUMBER_OF_EVENT_INDICATORS' is declared but not used [XDeclaredButNotUsed] c:\Users\c\Documents\nim\fmi\prueba.nim(9, 4) Hint: 'NUMBER_OF_REALS' is declared but not used [XDeclaredButNotUsed] CC: fmuTemplate CC: stdlib_io.nim CC: stdlib_system.nim CC: prueba.nim
I have tried in Linux and it failed "nicely".
The first problem was related with a wrong path (I will check in Windows).
The second problem that I see is that fmuTemplate.c needs the values defined above in the nim code. For instance, it needs MODEL_GUID.
How can I pass that MODEL_GUID from nim to the compilation of fmuTemplate.c as a define
Your problem might related this issue: https://github.com/nim-lang/Nim/issues/8648
When backend C compiler output large message to stdout, Nim compiler hangs. It seems Nim compiler hangs with smaller message on Windows than on Linux.
This code passes const string in Nim code to C code as define.
test.nim:
const
FOO = "abcd"
proc xyz() {.importc.}
xyz()
{.passC: "-DFOO=\\\"" & FOO & "\\\"".}
{.compile: "test.c".}
test.c:
#include <stdio.h>
void xyz() {
printf("%s\n", FOO);
}
You're hitting an issue on Windows that has been known for a little while: https://github.com/nim-lang/Nim/issues/8648
Basically, the stderr pipe gets overflowed and Windows hangs. You can mitigate this by specifying that the compiler should stop after a certain number of errors. For gcc, it's -fmax-errors=N, where N is the number of errors you should stop after.
For example, to disable warnings and set max errors to 5, do the following:
{.passC: "-Ifmi/shared/include -w -fmax-errors=5".}
For your other problem, you have to define the symbols by calling passC with a -DSYMBOL=VAL. For example, it looks like you need this:
{.passC: "-Ifmi/shared/include -w -fmax-errors=5 -DMODEL_GUID={8c4e810f-3df3-4a00-8276-176fa3c9f008} -DNUMBER_OF_BOOLEANS=0".}
# Keep defining the rest of them as required
In Windows is failing due to one of the C macros. There is macro to rename functions:
#ifndef DISABLE_PREFIX
#define pasteA(a,b) a ## b
#define pasteB(a,b) pasteA(a,b)
#define FMI2_FUNCTION_PREFIX pasteB(MODEL_IDENTIFIER, _)
#endif
The failure that I am having in Windows is:
c:\Users\c\Documents\nim\fmi>nim c --nimcache:.cache --app:lib -o:inc.dll inc.nim Hint: used config file 'C:\Users\c\Documents\instalado\nim\v1.0.0\config\nim.cfg' [Conf] Hint: system [Processing] Hint: widestrs [Processing] Hint: io [Processing] Hint: inc [Processing] Hint: fmutemplate [Processing] Hint: types [Processing] Hint: time_t [Processing] CC: fmuTemplate CC: inc.nim Error: execution of an external compiler program 'gcc.exe -c -w -mno-ms-bitfields -Ic:/Users/c/Documents/nim/fmi/fmi -Ic:/Users/c/Documents/nim/fmi/fmi/shared/include -Ifmi/shared/include -w -fmax-errors=2 -DMODEL_IDENTIFIER=\"inc\" -DMODEL_GUID=\"{8c4e810f-3df3-4a00-8276-176fa3c9f008}\" -DNUMBER_OF_REALS=0 -DNUMBER_OF_INTEGERS=1 -DNUMBER_OF_BOOLEANS=0 -DNUMBER_OF_STRINGS=0 -DNUMBER_OF_STATES=0 -DNUMBER_OF_EVENT_INDICATORS=0 -IC:\Users\c\Documents\instalado\nim\v1.0.0\lib -Ic:\Users\c\Documents\nim\fmi -o c:\Users\c\Documents\nim\fmi\.cache\inc.nim.c.o c:\Users\c\Documents\nim\fmi\.cache\inc.nim.c' failed with exit code: 1 In file included from c:\Users\c\Documents\nim\fmi\.cache\inc.nim.c:10:0: <command-line>:0:18: error: pasting ""inc"" and "_" does not give a valid preprocessing token c:/Users/c/Documents/nim/fmi/fmi/fmuTemplate.h:14:25: note: in definition of macro 'pasteA' #define pasteA(a,b) a ## b ^
I know that this is a C question, but just in case anybody knows.