0

I have two interrogations about using external functions with modelica(via dymola):

External Functions

1/Could we use C++ syntax ? According to my tests, No

I got this error :

C:\Program Files\Microsoft Visual Studio\**\include\yvals_core.h(23): fatal error C1189: #error:  STL1003: Unexpected compiler, expected C++ compiler

2/ Basically, modelica can run external C functions.

Usually, we put all of headers/libraries files needed for compilation into Resources directory.

Here The following tree structure:

  • Data
  • Images
  • Include
    • *.h
  • Library
    • win64
      • *.dll

But, I obtained also an error : dsmodel.obj : error LNK2019: unresolved external symbol * referenced in function *

If you have any idea about this issue !

wolfgunner
  • 119
  • 2
  • 13
  • That error, [C1189](https://learn.microsoft.com/en-us/cpp/error-messages/compiler-errors-1/fatal-error-c1189?view=msvc-170) "Unexpected compiler, expected C++ compiler" is from an [`#error`](https://learn.microsoft.com/en-us/cpp/preprocessor/hash-error-directive-c-cpp?view=msvc-170) directive in the source. It say that you build the code as C, but the header file expected a C++ compiler. So to answer the first question: Yes you can program it in C++. – Some programmer dude Apr 11 '23 at 09:47
  • And please note that there's no such language as "C/C++", You either program in the C language, or in the totally different C++ language. – Some programmer dude Apr 11 '23 at 09:48
  • 2
    Note that you can program and build your external library using C++ but Modelica will call the C compiler on your external C Modelica code. Just make sure that the interface you have in the external Modelica C code is a C one. Basically make your C++ library have a C interface which calls the C++ stuff. – Adrian Pop Apr 11 '23 at 12:17
  • @Someprogrammerdude you possibly argue that the common subset of language constructs between C and C++ could be called "C/C++". I.e. code that can be compiled either in C or in C++ mode. – Dag B Apr 27 '23 at 08:28
  • @DagB The actual common subset of both C and C++ is really very small, so small that it's practically useless. There are often very subtle differences in the C and C++ specifications making things just too different. Things like string constants not being actually constant in C for example, or `const` having the possibility to be compile-time constant in C++ but never in C, type-punning using unions which is allowed in one language but not the other, and many more. Not to mention that such code would just be bad in *both* languages. – Some programmer dude Apr 27 '23 at 12:42
  • 1
    @Someprogrammerdude I respectfully disagree. It is very well possible to write programs that have the same behavior in C and C++. For example, if you do not attempt to modify your string constants, then you have common (well defined) behavior in C and C++. Same if you do not use type punning with unions. I claim that the set of useful functions that can be written is quite big. (As an early member of C++ standardization committee WG21/X3J16 this was a rather common topic.) – Dag B Apr 28 '23 at 14:24

1 Answers1

1

As you say, Modelica has an interface to C and expects to compile C code. You can interface C++ functions by using extern "C" in the API of your C++ library. The remaining "detail" is to make sure you also get the C++ runtime.

Dag B
  • 621
  • 3
  • 8