Dynamic Linking

For a better understanding of this page you should know how to:

Dynamic linking of objects in external libraries with systems in MLDesigner is possible. This is a useful feature if you want to include functions from a variety of different libraries without rewriting the function.

These objects encompass object files (.o), static libraries (.a), and shared objects (.so or .dll). MLDesigner checks to see if the object files exist in the specified directory. With previous versions of MLDesigner it was only possible to link precompiled objects. Now if the specified files no longer exist MLDesigner will look for a makefile in the location where the object files were specified. If a makefile does not exist you must create it in the correct location. MLDesigner will then compile the object files thus ensuring that any changes made to the object are updated.

You can dynamically link on a primitive level where the function defined in the external library is defined as extern in a code item of the primitive source before being called in the go method. You can also link on a system level where an external library is defined.

For this example we need to create a small system containing two Ramp primitives, a user defined add primitive and a dump primitive to write the simulation results to a file. This system can be found at MLD Examples→Tutorials→Dynamic Linking→DynamicLinking.

  1. Create a new system in the SDF domain called dynamLinking in a library where you have write permission.
  2. Add two model instances of the Ramp primitive from MLD Libraries→SDF→Sources to your system.
  3. Save your system.

You must now create the add primitive called AddExt:

  1. Create a new primitve in the SDF domain called AddExt.
  2. Add two input ports and one output port.
  3. Change the Data Type of each port from anytype to int.
  4. Save the primitive.

You must now open the primitives source code and modify the code manually to look like the following:

ccinclude {}
code
{
  extern int externalLibraryAdd( int, int );
}
constructor
{
}

The primitive now tells the parser to look in an external library for the described function. The next step is to describe the behavior of the primitive during simulation, that is, call the function of the external library.

go
{
  int a = (int)(Input1%0);
  int b = (int)(Input2%0);
  int c = externalLibraryAdd( a, b );
  Output1%0 << c;
}


The next step is to show MLDesigner where the external file is physically saved.

  1. Click on the background of the primitive in the Model Editor Window to activate the Primitive Properties plane in the Property Editor. Click the folder icon in the Linked Objects field to open the Select Multiple Files dialog window.
  2. Open libextadd.so or libextadd.dll, depending on your operating system, from <MLDesigner installation directory>/Examples/Tutorials/DynamicLinking/LinkedObjects, Add it to the list of Selected Files and confirm the dialog with OK.
  3. Save the primitive and drag it into the dynamLinking system.

The next step is to add the dump primitive to write the simulation results to a file.

  1. Add a DmpNInt primitive with one input from MLD Libraries→SDF→Sinks to the dynamLinking system.
  2. Optionally, you can edit the instance properties of the DmpNInt instance. You may change ods_OutData_FileName to a location where you would like the simulation results to be saved. If the result should be displayed in a graph you may set ShowGraph to TRUE.
  3. Finally, connect the ports as shown below.

Linked Objects and External Simulations

It is important in to pay attention to the order in which linked objects are used by a system when the system is being simulated Extern. The objects should be listed in the correct order otherwise the simulation will not run. To change the order in which linked objects are listed, select the appropriate object in the Select Multiple Files dialog of the Linked Objects property and move the object Up or Down in the list.

Permanently linking objects to MLDesigner

Currently this feature is only available in MLDesigner on Linux.

Permanently loading code when entering Simulation Mode in order to extend the functionality of MLDesigner such as including a new domain or your own interface for interaction with other tools for co-simulation is possible.Open the file $HOME/.mld/.ptclshrc, it should be present if you have switched to Simulation Mode at least once. You will see place holders for filenames with extension .o (object files), .a (static libraries), and .so (shared objects). You must enter the full path of the file you wish to dynamically link to MLDesigner in the appropriate place holder. Before these setting become active you need to re-enter Simulation Mode.