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 foo and bar in two separate files foo.m and bar.m. Place the file bar.m in the MATLAB namespace myNamespace. The function foo accepts a string input and then calls the function bar.

type foo.m
function out = foo(str)
temp = strlength(str);
out = myNamespace.bar(temp);
end
type +myNamespace/bar.m
function out = bar(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.

t = coder.typeof("string");
t.StringLength = Inf;
% Setting t.StringLength to Inf automatically sets t.VariableStringLength to
% true

codegen -config cfg foo -args {t} -report
Code generation successful: To view the report, open('codegen/lib/foo/html/report.mldatx')

Inspect Generated Code

Open the code generation report and inspect the generated code.

The file foo.h contains the declaration of the generated function foo. Because the MATLAB function foo 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/foo/foo.h
//
// File: foo.h
//
// MATLAB Coder version            : 24.1
// C/C++ source code generated on  : 12-Feb-2024 21:01:59
//

#ifndef FOO_H
#define FOO_H

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

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

}

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

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

type codegen/lib/foo/bar.h
//
// File: bar.h
//
// MATLAB Coder version            : 24.1
// C/C++ source code generated on  : 12-Feb-2024 21:01:59
//

#ifndef BAR_H
#define BAR_H

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

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

}
} // namespace allcode

#endif
//
// File trailer for bar.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/foo/string1.h
//
// File: string1.h
//
// MATLAB Coder version            : 24.1
// C/C++ source code generated on  : 12-Feb-2024 21:01:59
//

#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