How to import external c types with namespaces?
56 views (last 30 days)
Show older comments
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.
0 Comments
Answers (2)
Yao Ren
on 9 May 2023
Importing struct and enum types inside of C++ namespaces is supported in R2023a. Please upgrade to MATLAB R2023a.
14 Comments
Yao Ren
on 22 Jan 2025 at 17:27
Thanks for reporting these issues! Please see my comments below:
Error: "modelname.rtw Line: 601 Column: 17 syntax error".
This appears to be an ERT code generation issue. We hope it will be resolved in the general release.
Slow Import to .sldd File.
I attempted to reproduce the import speed issue and compared it with version 24b. In my tests, importing to the DesignData section of the data dictionary is twice as slow in version 25a. Importing to the ArchitecturalData section is five times slower compared to importing to the DesignData section in 24b. However, this isn't a direct comparison since the ArchitecturalData section is a new feature in 25a. Are you experiencing similar slowdowns? Note that keeping the Data Dictionary open during import can increase the time, particularly when the ArchitecturalData section is visible. The UI rendering during type creation can be time-consuming. We will conduct profiling and aim to optimize the speed for the general release.
Error Message: "because there are multiple types of the same name under different namespaces. Specify only one type using 'Names' option with the namespace-qualified type name."
If you have two types, "ComponentA::Settings_t" and "ComponentB::Settings_t", in header files, Simulink can only have one Simulink Type named "Settings_t" in scope at a time. A single data dictionary can contain only one "Settings_t" type, which is why this error occurs. You can import only one of the two types by using 'Name', 'ComponentB::Settings_t' to remove the ambiguity. This is a known limitation not addressed in 25a. Simulink has plan to introduce native namespaces for modeling, allowing one data dictionary to hold two types named "Settings_t" in different Simulink namespaces.
Question: In your use case, do you need to use both "ComponentA::Settings_t" and "ComponentB::Settings_t" types in one model?
Enum Class with Element Named "all" Causes Import Crash.
We will reproduce this issue and fix it in the general release.
Marcus
on 23 Jan 2025 at 13:07
Edited: Marcus
on 23 Jan 2025 at 13:08
Hi @Yao Ren
Regarding Slow Import to .sldd file i got the following metrics
2025a
Simulink.importExternalCTypes('myHeader.h', 'Language', 'C++', 'DataDictionary', 'myDictionary.sldd', 'DataDictionarySection','ArchitecturalData');
Elapsed time: 104,36 seconds
2025a
Simulink.importExternalCTypes('myHeader.h', 'Language', 'C++');
Elapsed time: 2.16 seconds
2024a
Simulink.importExternalCTypes('myHeader.h', 'Language', 'C++');
Elapsed time: 1.91 seconds
As you can see it is a magnitude of 50 times the time of a standard importExternalCTypes which will make this tool tedious to use.
Regarding Error Message: "because there are multiple types of the same name under different namespaces. Specify only one type using 'Names' option with the namespace-qualified type name."
Yes right now we need to do workarounds for handling this. But from our side i would say that simluink handles this correctly is a requirement to make the most of c++ as the generation target language.
Tomorrow i will report how the import of mutiple headers with the same enumerations header file test is going.
See Also
Categories
Find more on Code Interface Definitions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!