How to import external c types with namespaces?

39 views (last 30 days)
Tobias Pape
Tobias Pape on 22 Jun 2022
Edited: Yao Ren on 18 Jan 2024
I try to import external c header files to my simulink model.
I am using the function ImportExternalCTypes() (Matlab2021b) to import the header file.
Call of the function:
Simulink.importExternalCTypes('test.h', 'Language', 'C++', 'Overwrite', 'on', 'DataDictionary', 'Types.sldd')
Content of the header file test.h:
#include <string>
#include <vector>
#include <stdexcept>
namespace System {
namespace Command {
enum Type {
On,
Off
};
}
enum class Subystem {
Stop,
Start
};
The enumeration within the namespace will be ignored.
Only the enumeration Subystem will be added to the Data Dictionary.
Is there a wag that Matlab recognizes the Enumeration within the namespace?
I get an auto generated header file with the namespaces. All Enumerations are defined within namespaces in the original header file.

Answers (2)

Nitanshu
Nitanshu on 28 Jun 2022
Hi Tobias,
You can take help from this Matlab documentation.
Hope it helps!
  1 Comment
Tobias Pape
Tobias Pape on 4 Jul 2022
Hi Nitanshu,
thank you for your answer. I already read the documentation.
The documentation does not explain why namespaces will be ignored.

Sign in to comment.


Yao Ren
Yao Ren on 9 May 2023
Importing struct and enum types inside of C++ namespaces is supported in R2023a. Please upgrade to MATLAB R2023a.
  5 Comments
Brandon
Brandon on 16 Jan 2024
Edited: Brandon on 16 Jan 2024
There are two issues here.
1) Importing C++ enums that reside inside namespace (which I can confirm does work fine in R2023a)
2) Generating code from a Simulink model that uses this enum. This does not seem to work (in R2023a at least) because the generated code does not prepend the enum types or enum values with the original namespace. Indeed the "Type.m" matlab enum file you show above does contain any information about the original namespace, so there's no way for the generated code to know that it should be namespacing that enum. Ergo, this seem like a broken workflow in matlab.
Yao Ren
Yao Ren on 18 Jan 2024
Edited: Yao Ren on 18 Jan 2024
Regarding issue 2), we are currently developing a feature that will introduce the concept of "namespace" to Simulink modeling. With this feature, the function Simulink.importExternalCTypes() will be capable of importing a C data type as a Simulink type into the Simulink Data Dictionary (.sldd), organizing them under the appropriate namespaces. This will ensure that model code generation can properly recognize and utilize these namespaces.
In the meantime, for users of R2023a or later, there is a workaround that allows the generation of C++ code from a model in such a way that the generated code respects the namespaces of the imported C++ data types.
Please follow the steps below to apply this workaround:
1) Navigate to model's "Configuration Parameters" and then to the "Simulation Target" section. Here, you need to include the C++ code headers containing the types in the "Include headers" field. It's important to ensure that the "Include directories" field lists all the directories needed to search for any header files that are directly or indirectly included. If all headers are located in the current working directory, this field can be left empty, as the current directory is always included in the search path by default. Note that the "Include directories" can be specified as relative paths to the location of the model file. The screenshot below demonstrates how to enter the include headers, using the previous "test.h" example as a reference.
The model will parse the header files specified in step 1) and recognize all the types, such as "enum Type" from our example, noting that it is encapsulated within the "System::Command::" namespace.
2) Ensure that the Code Generation "Language" is set to "C++". If you are importing enum class types, also make sure that the "Language standard" is set to "C++11 (ISO)".
3) Proceed with generating the code for your model. The code generation process will be able to match the imported Simulink.EnumType "Type" with the C++ data type "System::Command::Type" found in "test.h". Consequently, it will generate the correct code that includes namespaces for type references.
By following these steps, you should be able to generate C++ code that honors the namespaces of imported C++ data types, even before the new feature is officially released.

Sign in to comment.

Categories

Find more on LEGO MINDSTORMS EV3 Hardware in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!