Hi everyone

I'm trying to get some external C-code working in openmodelica. Some
pointers would be nice.


I read this not so old example which states that the openmodelica syntax
differs somewhat from the modelica syntax:
http://openmodelica.org/index.php/forum/topic?id=14

Cut ----------------------
external "C" ... = myExt(...) annotation(Library = "myExt.o"); // Note
the required .o, not present in Dymola... We really should fix this
Cut ----------------------


Now, what i did was something simple. I have a dynamically linked
library libgetone.so with the function  double get_one() {return 1.0;}


I made a library using  

gcc -Wall -fPIC -c -c getone.c
gcc -shared -Wl,-soname,libgetone.so.1 -o libgetone.so.1.0 getone.o
ln -sf libgetone.so.1.0 libgetone.so    
ln -sf libgetone.so.1.0 libgetone.so.1  


I have a non modelica test file which works and it's containing 

Cut ------------------------------------
int main(int argc, char** argv) {
  printf("Get data returns: %f\n", get_one());
  return 0;
}
Cut ------------------------------------

It compiles using "gcc test.c -L. -lgetone -o test"

Now to the modelica code (this is what I ended up with after some
trials) I'm simply not certain on what to write for
"annotation(Library="")" but I discovered where it was put in the gcc
compile command so I changed it to something that seemed appropriate for
the gcc compilation.

Cut ------------------------------------
function returnone
input Integer k_in;
output Real y;
external "C" y = getone() 
annotation(Library= "-L/home/oberg/...../Modelica_externalC_example
-lgetone"); 
// Note the required .o, not present in Dymola... We really should fix
this
end returnone;

class Test
parameter Integer k = 1;
Real k_new = returnone(k);
Real x;
equation
der(x) = k_new;
end Test; 
Cut ------------------------------------


This is the output from omc, after I HAND COPIED libgeone.so*
to /tmp/OpenModelica

Cut ------------------------------------
.
.
.
Error: OMC unable to load `./returnone.so': libgetone.so.1: cannot open
shared object file: No such file or directory.

Error: Error building simulator. Buildlog: gcc -O3 -falign-functions
-march=native -mfpmath=sse -fPIC -I
\"/opt/OpenModelicaStuff/OpenModelica/include/omc\" -I. -c -o Test.o
Test.c

gcc -O3 -falign-functions -march=native -mfpmath=sse -fPIC -I
\"/opt/OpenModelicaStuff/OpenModelica/include/omc\" -I. -c -o
Test_records.o Test_records.c

g++ -I. -o Test Test.o Test_records.o -I
\"/opt/OpenModelicaStuff/OpenModelica/include/omc\" -I.
-L/home/oberg/Documents/Project/VTI-Simulering/Modelica_externalC_example 
-lgetone -O3 -falign-functions -march=native -mfpmath=sse -fPIC 
-L\"/opt/OpenModelicaStuff/OpenModelica/lib/omc\" -lSimulationRuntimeC -lexpat 
-linteractive -lrt -lpthread -Wl,-Bstatic -lf2c -Wl,-Bdynamic

Test.o: In function `_returnone':

Test.c:(.text+0x7): undefined reference to `getone'

collect2: ld returned 1 exit status

make: *** [Test] Error 1
Cut ------------------------------------


Can someone please tell me 

1) Why oh why is there an undefined reference to getone when the
compilation seems to find libgetone.so

2) What is the correct approach for paths if I want a home-developed
library that isn't part of the /usr/lib etc... Hand-copying isn't really
an option.

3) What am i supposed to write in annotation(Library="")?

Best regards
Per Öberg



Reply via email to