Archived OpenModelica forums. Posting is disabled.

Alternative forums include GitHub discussions or StackOverflow (make sure to read the Stack Overflow rules; you need to have well-formed questions)


Forgot password? | Forgot username? | Register

Calling external C function

Calling external C function

Hi,

I'm trying to build a model which uses external C functions (OM 1.9.0 on Win7 64bit), however the problem is that those functions are using another library for communicating with USB devices. So far I have something like this

function udaq_command
  input Real lamp;
  input Real fan;
  input Real led;
  output String result;
external 
  result = command_ext(lamp,fan,led)   annotation(Library = "udaq.o", Include = "#include \"udaq.h\"");
end udaq_command;

What is the correct syntax to include also external libraries when defining functions? Thanks for your time and help.

Re: Calling external C function

Code:

annotation(Library="udaq")

Don't include anything (the C header is automatically generated by OpenModelica). Keep a library called libudaq.a libudaq.so udaq.lib (I guess, on Windows). Basically, something that "-L/path/to/lib/Resources/Library -ludaq" will find and link against.

Re: Calling external C function

The problem is that I don't know where and how to link this library. Error I'm getting is

Code:

c:/openmodelica1.9.0/mingw/bin/../lib/gcc/mingw32/4.4.0/../../../../mingw32/bin/ld.exe: cannot find -ludaq

Should I go to c:/openmodelia1.9.0/mingw/bin and then run

Code:

ar rcs libudaq.a d:\path\to\folder\udaq.o

or do something else?

Re: Calling external C function

Yes, you will need to create the .a-file and put it in the search path for the library.

Re: Calling external C function

I was able to link my udaq library, however now the problem is that the udaq is using another library and now I'm getting these errors:

Code:

c:/openmodelica1.9.0/mingw/bin/../lib/gcc/mingw32/4.4.0/../../../libudaq.a(udaq.o):udaq.c:(.text+0x18): undefined reference to `libusb_init@4'

c:/openmodelica1.9.0/mingw/bin/../lib/gcc/mingw32/4.4.0/../../../libudaq.a(udaq.o):udaq.c:(.text+0x49): undefined reference to `libusb_get_device_list@8'
c:/openmodelica1.9.0/mingw/bin/../lib/gcc/mingw32/4.4.0/../../../libudaq.a(udaq.o):udaq.c:(.text+0x9a): undefined reference to `libusb_get_device_descriptor@8'
c:/openmodelica1.9.0/mingw/bin/../lib/gcc/mingw32/4.4.0/../../../libudaq.a(udaq.o):udaq.c:(.text+0x100): undefined reference to `libusb_open@8'
c:/openmodelica1.9.0/mingw/bin/../lib/gcc/mingw32/4.4.0/../../../libudaq.a(udaq.o):udaq.c:(.text+0x140): undefined reference to `libusb_kernel_driver_active@8'
c:/openmodelica1.9.0/mingw/bin/../lib/gcc/mingw32/4.4.0/../../../libudaq.a(udaq.o):udaq.c:(.text+0x169): undefined reference to `libusb_detach_kernel_driver@8'
c:/openmodelica1.9.0/mingw/bin/../lib/gcc/mingw32/4.4.0/../../../libudaq.a(udaq.o):udaq.c:(.text+0x191): undefined reference to `libusb_set_configuration@8'
c:/openmodelica1.9.0/mingw/bin/../lib/gcc/mingw32/4.4.0/../../../libudaq.a(udaq.o):udaq.c:(.text+0x1c8): undefined reference to `libusb_claim_interface@8'
c:/openmodelica1.9.0/mingw/bin/../lib/gcc/mingw32/4.4.0/../../../libudaq.a(udaq.o):udaq.c:(.text+0x22f): undefined reference to `libusb_control_transfer@32'
c:/openmodelica1.9.0/mingw/bin/../lib/gcc/mingw32/4.4.0/../../../libudaq.a(udaq.o):udaq.c:(.text+0x458): undefined reference to `libusb_bulk_transfer@24'
c:/openmodelica1.9.0/mingw/bin/../lib/gcc/mingw32/4.4.0/../../../libudaq.a(udaq.o):udaq.c:(.text+0x4cb): undefined reference to `libusb_bulk_transfer@24'
c:/openmodelica1.9.0/mingw/bin/../lib/gcc/mingw32/4.4.0/../../../libudaq.a(udaq.o):udaq.c:(.text+0x678): undefined reference to `__chkstk_ms'
c:/openmodelica1.9.0/mingw/bin/../lib/gcc/mingw32/4.4.0/../../../libudaq.a(udaq.o):udaq.c:(.text+0x731): undefined reference to `libusb_release_interface@8'
c:/openmodelica1.9.0/mingw/bin/../lib/gcc/mingw32/4.4.0/../../../libudaq.a(udaq.o):udaq.c:(.text+0x741): undefined reference to `libusb_close@4'
c:/openmodelica1.9.0/mingw/bin/../lib/gcc/mingw32/4.4.0/../../../libudaq.a(udaq.o):udaq.c:(.text+0x759): undefined reference to `libusb_free_device_list@8'
c:/openmodelica1.9.0/mingw/bin/../lib/gcc/mingw32/4.4.0/../../../libudaq.a(udaq.o):udaq.c:(.text+0x768): undefined reference to `libusb_exit@4'

Should I link the libusb in a same way as I did udaq and do not change anything in OM function files or something else is needed?

Re: Calling external C function

Code:

annotation(Library={"udaq","somethingusb"})

Keep in mind that the library also has to be compiled with mingw especially since it seems to use c++ stuff (it probably has to be the same gcc version as in the OpenModelica mingw).

There are 0 guests and 0 other users also viewing this topic