Organize Generated C++ Code into Namespaces - MATLAB & Simulink (original) (raw)

Main Content

Namespaces help organize your code into logical parts, prevent name collisions, and enable you to more easily integrate your generated C++ code into a larger C++ project. Namespaces also increase compliance with the MISRA C++ standards for safety-critical code. This topic explains how to use the code generation settings to customize the organization of your generated C++ code into namespaces.

Settings That Control Namespace Structure

These are the code generation settings that enable you to control the creation of namespaces in the generated code:

Code Configuration Parameter Description How to specify
In a code configuration object:CppNamespaceIn theMATLAB® Coder™ app: On the Code Appearance tab, Namespace that contains the generated C++ code.If this parameter is empty, the code generator does not create such a namespace. In a code configuration object: '' (default)| character vectorIn the MATLAB Coder app: specify in a text field
In a code configuration object:CppNamespaceForMathworksCodeIn the MATLAB Coder app: On the Code Appearance tab, Namespace that contains code generated for all MathWorks® code (for example, code for the sparse data type).If this parameter is empty, the code generator does not create such a namespace. In a code configuration object: 'coder' (default)| character vectorIn the MATLAB Coder app: specify in a text field
In a code configuration object:CppPreserveNamespacesIn theMATLAB Coder app: On the Code Appearance tab, Specify whether to generate C++ namespaces for the namespaces in your MATLAB code. In a code configuration object: true (default)| falseIn theMATLAB Coder app: On the Code Appearance tab, select or clear the check box

Additional notes about namespace generation:

Example: Generate C++ Code with Namespaces

This example shows how to use these code generation settings to create namespaces.

Define MATLAB® Functions

Define two MATLAB functions addOne and callAddOne in two separate files, addOne.m and callAddOne.m. Place the file addOne.m in the MATLAB namespace myNamespace. The function callAddOne accepts a string input and then calls the function addOne.

function out = callAddOne(str) temp = strlength(str); out = myNamespace.addOne(temp); end

type +myNamespace/addOne.m

function out = addOne(in) coder.inline('never'); out = in + 1; end

Define Code Configuration Object

Create a code configuration object for a static library. Set the target language to C++. Specify a namespace allcode that contains the generated code. Specify a namespace notmycode that contains MathWorks® code.

cfg = coder.config('lib'); cfg.TargetLang = 'C++'; cfg.CppNamespace = 'allcode'; cfg.CppNamespaceForMathworksCode = 'notmycode';

Generate Code

Generate a static C++ library and a code generation report. Specify the input type to be string scalar that can have any length. Setting the StringLength property of the string scalar to Inf to automatically sets the VariableStringLength property of the string scalar to true.

t = coder.typeof("string"); t.StringLength = Inf;

codegen -config cfg callAddOne -args {t} -report

Code generation successful: To view the report, open('codegen/lib/callAddOne/html/report.mldatx')

Inspect Generated Code

Open the code generation report and inspect the generated code.

The file callAddOne.h contains the declaration of the generated function callAddOne. Because the MATLAB function callAddOne that you created is not inside a MATLAB namespace, the generated function is declared only inside the C++ namespace allcode that contains the generated code.

type codegen/lib/callAddOne/callAddOne.h

// // File: callAddOne.h // // MATLAB Coder version : 24.2 // C/C++ source code generated on : 22-Jan-2025 23:10:38 //

#ifndef CALLADDONE_H #define CALLADDONE_H

// Include Files #include "rtwtypes.h" #include "string1.h" #include #include

// Function Declarations namespace allcode { extern double callAddOne(const notmycode::rtString *str);

}

#endif // // File trailer for callAddOne.h // // [EOF] //

The file addOne.h contains the declaration of the generated function addOne. Because you created the MATLAB function addOne inside the MATLAB namespace myNamespace, the generated function is declared inside the C++ namespace hierarchy allcode::myNamespace.

type codegen/lib/callAddOne/addOne.h

// // File: addOne.h // // MATLAB Coder version : 24.2 // C/C++ source code generated on : 22-Jan-2025 23:10:38 //

#ifndef ADDONE_H #define ADDONE_H

// Include Files #include "rtwtypes.h" #include #include

// Function Declarations namespace allcode { namespace myNamespace { double addOne(double in);

} } // namespace allcode

#endif // // File trailer for addOne.h // // [EOF] //

The file string1.h contains the declaration of the generated class rtString that implements the MATLAB string data type. Because you instructed the code generator to place all code produced for MathWorks code inside the namespace notmycode, the generated class rtString is declared inside the namespace hierarchy allcode::notmycode.

type codegen/lib/callAddOne/string1.h

// // File: string1.h // // MATLAB Coder version : 24.2 // C/C++ source code generated on : 22-Jan-2025 23:10:38 //

#ifndef STRING1_H #define STRING1_H

// Include Files #include "rtwtypes.h" #include "coder_array.h" #include #include

// Type Definitions namespace allcode { namespace notmycode { class rtString { public: void init(const ::coder::array<char, 2U> &b_Value); rtString(); ~rtString(); ::coder::array<char, 2U> Value; };

} // namespace notmycode } // namespace allcode

#endif // // File trailer for string1.h // // [EOF] //

See Also

coder.CodeConfig | coder.EmbeddedCodeConfig | coder.MexCodeConfig