Main Content

sltest.CodeImporter class

Import C or C++ code into Simulink for testing

Description

Use objects of the sltest.CodeImporter class to import a C library or a subset of a library into Simulink® for modeling and testing. When you import the code, a Simulink library and a test file are created. Each C-compatible function maps to a C Caller block in the library and each C Caller block, by default, has an attached internal test harness. The created MLDATX test file contains test cases for each imported function. For unit tests, you can only import C code. Additionally, for unit tests, a sandbox is created to isolate the imported C code.

Note

If your code library contains C++ class methods, only the C++ methods that are wrapped in valid C function wrappers are imported into Simulink using the CodeImporter.

Alternatively, you can use a wizard to set up and import your code into Simulink. In the Test Manager, use New > Test for C/C++ Code to open the wizard.

The sltest.CodeImporter class is a handle class.

Class Attributes

HandleCompatible
true

For information on class attributes, see Class Attributes.

Creation

Description

example

codeimport_obj = sltest.CodeImporter creates a Simulink Test™ code importer object with untitled as the Simulink library file name and creates four additional objects, which you access using the SandboxSettings, ParseInfo, and CustomCode, and Options properties.

example

codeimport_obj = sltest.CodeImporter(libraryfile) creates a code importer object and uses the specified libraryfile as the name of the created Simulink library. It sets the LibraryFileName property to libraryfile.

Properties

expand all

Type of test, specified as a string or character vector. To test a subset of your custom code library in isolation, use "UnitTest". The code for a unit test might not be complete and might have undefined symbols. The CodeImporter automatically creates stubs for the undefined symbols. When you specify "UnitTest", a sandbox is also created. To test your entire code library, use "IntegrationTest".

Example: codeimport_obj.TestType = "IntegrationTest"

Attributes:

GetAccess
public
SetAccess
public

Sandbox configuration for unit tests, specified as a SandboxSettings object. This property applies only if TestType is UnitTest. If TestType is IntegrationTest, sandbox settings are ignored. A default SandboxSettings object is created when you create a code importer object. Before using the createSandbox method, you can change the default property values. For information on changing the sandbox mode, copying source files to the sandbox folder, and removing pragmas and variable definition header properties, see sltest.CodeImporter.SandboxSettings.

Example: codeimport_obj.SandboxSettings.CopySourceFiles = true

Attributes:

GetAccess
public
SetAccess
private

Name of the created Simulink library file and related artifacts, specified as a string or character vector. The related artifacts include the data dictionary, sandbox folder, and test file. For example, if LibraryFileName is "myCodeTest"

  • The Simulink library name is myCodeTest.slx.

  • The data dictionary name is myCodeTest.sldd.

  • The sandbox folder name is myCodeTest_sandbox.

  • The test file is myCodeTest.mldatx.

If you do not specify a library file name, it defaults to untitled.

Example: codeimport_obj.LibraryFileName = "myCodeTest"

Attributes:

GetAccess
public
SetAccess
public

Folder for the created library and related artifacts files, specified as a string or character vector. The default is an empty string, which is interpreted as the current working folder. You can alternatively specify the OutputFolder using MATLAB® path commands, enclosed in $, such as $pwd$, which is the current working folder.

Example: codeimport_obj.OutputFolder = "C:/myMATLABFiles/CodeImports"

Example: codeimport_obj.OutputFolder = "$pwd"

Attributes:

GetAccess
public
SetAccess
public

C or C++ code files and their associated properties to import, specified as a CustomCode object. Use this property to change code importing options, such as source and header files, folder paths, libraries, and compiler and linker flags. For information, see Simulink.CodeImporter.CustomCode.

Attributes:

GetAccess
public
SetAccess
private

Additional import options, specified as a Simulink.CodeImporter.Options object. Use this object to change the default values for the size of an argument passed by a pointer to a function, creating a test harness when importing code, or changing the Simulink library browser name. For information, see Simulink.CodeImporter.Options.

Attributes:

GetAccess
public
SetAccess
private

The ParseInfo object properties have empty values before you call the parse method. After parsing, ParseInfo includes whether the code successfully parsed, and information about the available functions, entry functions, and types in the code. For information, see Simulink.CodeImporter.ParseInfo.

Attributes:

GetAccess
public
SetAccess
private

Methods

expand all

Examples

collapse all

This example assumes that you have existing C code files to test and you do not want to test your entire C code library.

  1. Create an sltest.CodeImporter object and specify myCodeTest as the Simulink library file name.

    codeimport_obj = sltest.CodeImporter("myCodeTest");

  2. Assign the source files to the CustomCode property.

    codeimport_obj.CustomCode.SourceFiles = {"myCode1.c"};

  3. Create the sandbox. The createSandbox method creates stub files automatically for any undefined symbols.

    codeimport_obj.createSandbox;
    After you create the sandbox, you can inspect the created stubs and, if desired, manually stub the symbols using the files in the manualstub subfolder of the sandbox folder. Then, repeat the createSandbox command to update the sandbox.

  4. Parse the generated sandbox

    codeimport_obj.parse;

  5. Import the code to a Simulink library model. The import method creates a test file by default.

    codeimport_obj.import;

  6. Open the Test Manager. Then, open the created myCodeTest.mldatx test file, and run the created test cases.

This example assumes you have an existing C or C++ code library to test and that all C++ methods are in C wrapper functions.

  1. Create an sltest.CodeImporter object and specify myCodeLibTest as the Simulink library name. Set the TestType to IntegrationTest.

    codeimport_obj = sltest.CodeImporter("myCodeLibTest");
    codeimport_obj.TestType = "IntegrationTest";

  2. Create a CustomCode object that specifies the header files. Use custcode.<propertyname> = <value> syntax to specify other types of files and properties. Then, assign the CustomCode object to the CustomCode property.

    codeimport_obj.CustomCode.SourceFiles = {"myCode1.c", "myCode2.c"};
    codeimport_obj.InterfaceHeaders = {"myCode_a.h","myCode_b.h"};

  3. Parse the specified code.

    codeimport_obj.parse;

  4. Import the code to a Simulink library model. The import method creates a test file by default.

    codeimport_obj.import;

  5. Open the Test Manager. Then, open the created myCodeLibTest.mldatx test file, and run the created test cases.

Introduced in R2021a