Help - Search - Members - Calendar
Full Version: Does anyone help for testing C external functions?
Modelica Forum > Modelica > Dymola
new_wave
I want to use C external functions in Dymola. I try many examples in the book.
I got the error messages somethings look like this

Compiling the model with GCC.
October 19, 2006 11:22:09
gcc -c -O1 -mpentium -march=pentium -ffast-math -DDYMOSIM dsmodel.c
C:\Dymola5.0\source\dsblock1.c: In function `equations_':
C:\Dymola5.0\source\dsblock1.c:210: warning: This file contains more `{'s than `}'s.
C:\Dymola5.0\source\dsblock2.c: In function `dsblock_':
C:\Dymola5.0\source\dsblock2.c:98: warning: This file contains more `{'s than `}'s.
C:\Dymola5.0\source\dsblock3.c:57: warning: This file contains more `{'s than `}'s.
C:\Dymola5.0\source\dsblock4.c: In function `declare_':
C:\Dymola5.0\source\dsblock4.c:91: warning: This file contains more `}'s than `{'s.
C:\Dymola5.0\source\dsblock5.c: At top level:
C:\Dymola5.0\source\dsblock5.c:278: warning: This file contains more `}'s than `{'s.
Linking the model to Dymosim.
October 19, 2006 11:22:09
gcc -s -o dymosim.exe dymosim.o dsmodel.o -L. -lds -lm
dsmodel.o(.text+0xe73b):dsmodel.c: undefined reference to `Chrip'
Error generating Dymosim.
October 19, 2006 11:22:10

--------------------------------------------------------------------------------------------------------
Compiling and linking the model for DDE (Visual C++).
October 19, 2006 11:23:38
Setting environment for using Microsoft Visual C++ tools.
dsmodel.c
dsmodel.obj : error LNK2001: unresolved external symbol _Chrip
dymosim.exe : fatal error LNK1120: 1 unresolved externals
Error generating Dymosim.
October 19, 2006 11:23:38


This is my first time to use C external function. Does anyone help me to identify the errors? Please try to run my codes.

new_wave







I forgot another file for C code.
I don't know why I can not post a file wih .c extension. However, the code is simple.
Please include this in the file "Chirp.c"


#include <stdlib.h>
#include <string.h>
#include <math.h>

double Chirp(double w1, double w2, double A, double M, double time)
{
double res;
res=A*cos(w1*time+(w2-w1)*time*time/(2*M));
return res;
}


new_wave
jce
Hi new-wave,

besides from the GCC warnings, there is one big typo and GCC and Visual C++ clearly point it out:

ZITAT

Linking the model to Dymosim.
October 19, 2006 11:22:09
gcc -s -o dymosim.exe dymosim.o dsmodel.o -L. -lds -lm
>>>> dsmodel.o(.text+0xe73b):dsmodel.c: undefined reference to `Chrip' <<<<
Error generating Dymosim.
October 19, 2006 11:22:10

---------------------------------------------------------------------

Compiling and linking the model for DDE (Visual C++).
October 19, 2006 11:23:38
Setting environment for using Microsoft Visual C++ tools.
dsmodel.c
>>>> dsmodel.obj : error LNK2001: unresolved external symbol _Chrip <<<<
dymosim.exe : fatal error LNK1120: 1 unresolved externals
Error generating Dymosim.
October 19, 2006 11:23:38


The unresolved symbol comes from a typo in your Modelica file. In your Modelica Chirp function you have the following line with the reference to the external function:

QUELLTEXT

external "C" Chrip(w_start, w_end, A, M, t, u);

This should read "Chirp"... smile.gif

I don't know where the rest of the GCC msgs exactly come from, but I have seen them during my experiments, too. If I remember correctly, they didn't seem to do anything bad, my model just worked fine.

I tested the use of external functions successfully with GCC and Visual C++ by using a similar simple function that just adds two numbers ("add2.c"):

QUELLTEXT

#include "add2.h"
double add2(double x, double y)
{
  return x + y;
}


And here is the corresponding header file "add2.h":

QUELLTEXT

#ifndef ADD2_H
#define ADD2_H
  extern double add2(double x, double y);
#endif


To include this into Modelica, I used the "annotation"-scheme from the Dynasim manual and made a library of the C file. (libvctest.[a|lib] - depends on the use of GCC or Visual C++):

QUELLTEXT

function add2 "Sum of two numbers"
  input Real x;
  input Real y;
  output Real sum;
  external "C";
  annotation(Include="#include <add2.h>", Library="libvctest");
end add2;


So, good luck! smile.gif

One last thing, for what purpose do you include the "stdlib.h" and "string.h" in your C file? You don't use any functions in them, so I suggest you remove the includes.

Best regards,
Jonathan
new_wave
Thanks Jonathan. It works. However, I couldn't run it in another PC. So, I guess I have a problem of Dymola installation.

new_wave
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2012 Invision Power Services, Inc.