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 the Code Generation Settings dialog box: C++ namespace 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) or character vectorIn the Code Generation Settings dialog box: specify in a text field
In a code configuration object:CppNamespaceForMathworksCodeIn the Code Generation Settings dialog box: Namespace for MathWorks code 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) or a character vectorIn the Code Generation Settings dialog box: specify in a text field
In a code configuration object:CppPreserveNamespacesIn the Code Generation Settings dialog box: Generate C++ namespaces from MATLAB namespaces Specify whether to generate C++ namespaces for the namespaces in your MATLAB® code. In a code configuration object: true (default) or falseIn the Code Generation Settings dialog box, select or clear theGenerate C++ namespaces from MATLAB namespaces 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: View report

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

// // Prerelease License - for engineering feedback and testing purposes // only. Not for sale. // File: callAddOne.h // // MATLAB Coder version : 25.1 // C/C++ source code generated on : 01-Feb-2025 07:30:24 //

#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

// // Prerelease License - for engineering feedback and testing purposes // only. Not for sale. // File: addOne.h // // MATLAB Coder version : 25.1 // C/C++ source code generated on : 01-Feb-2025 07:30:24 //

#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

// // Prerelease License - for engineering feedback and testing purposes // only. Not for sale. // File: string1.h // // MATLAB Coder version : 25.1 // C/C++ source code generated on : 01-Feb-2025 07:30:24 //

#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

Topics