Main Content

Organize Generated C++ Code into Namespaces

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 ParameterDescriptionHow to specify

In a code configuration object: CppNamespace

In the MATLAB® Coder™ app: On the Code Appearance tab, 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)| character vector

In the MATLAB Coder app: specify in a text field

In a code configuration object: CppNamespaceForMathworksCode

In the MATLAB Coder app: On the Code Appearance tab, Namespace for MathWorks functions

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 vector

In the MATLAB Coder app: specify in a text field

In a code configuration object: CppPreserveNamespaces

In the MATLAB Coder app: On the Code Appearance tab, 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)| false

In the MATLAB Coder app: On the Code Appearance tab, select or clear the Generate C++ namespaces from MATLAB namespaces check box

Additional notes about namespace generation:

  • When you specify the CppNamespace property (or the corresponding setting in the app), the code generator packages all the generated functions and type definitions into the namespace, except for the generic type definitions contained in tmwtypes.h and the hardware-specific definitions in rtwtypes.h. The example main file and function are not packaged into the namespace.

  • If your MATLAB code has nested namespaces (for example, nmsp1 inside nmsp2), the namespaces in the generated code have the same nesting.

  • When creating namespaces for your MATLAB code that is intended for code generation, follow these guidelines:

    • Do not create a namespace that has the name 'coder'.

    • If you set the CppNamespaceForMathworksCode property (or the equivalent parameter in the app) to a nondefault name, do not create a namespace that has that name.

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.

type callAddOne.m
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  : 05-Sep-2024 13:48:51
//

#ifndef CALLADDONE_H
#define CALLADDONE_H

// Include Files
#include "rtwtypes.h"
#include "string1.h"
#include <cstddef>
#include <cstdlib>

// 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  : 05-Sep-2024 13:48:51
//

#ifndef ADDONE_H
#define ADDONE_H

// Include Files
#include "rtwtypes.h"
#include <cstddef>
#include <cstdlib>

// 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  : 05-Sep-2024 13:48:51
//

#ifndef STRING1_H
#define STRING1_H

// Include Files
#include "rtwtypes.h"
#include "coder_array.h"
#include <cstddef>
#include <cstdlib>

// 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

| |

Related Topics