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: CppPackagesToNamespaces

In the MATLAB Coder app: On the Code Appearance tab, MATLAB package to C++ namespace

Whether to generate C++ namespaces for the packages in the MATLAB code.

In a code configuration object: true (default)| false

In the MATLAB Coder app: On the Code Appearance tab, select or clear the MATLAB package to C++ namespace 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 packages (for example, pkg1 inside pkg2), the generated namespaces have the same nesting.

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

    • Do not create a package 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 package 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 a package myPackage. The function foo accepts a string input and then calls the function bar.

type foo.m
function out = foo(str)
temp = strlength(str);
out = mypackage.bar(temp);
end
type +mypackage/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 the namespace that contains all generated code to be named allcode. Specify the namespace that contains MathWorks® code to be named notmycode.

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.

s = "mystring";
t = coder.typeof(s);
t.Properties.Value = coder.typeof('a',[1 inf]);

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 package, the generated function is declared only inside the namespace allcode that contains all generated code.

type codegen/lib/foo/foo.h
//
// File: foo.h
//
// MATLAB Coder version            : 5.1
// C/C++ source code generated on  : 24-Aug-2020 19:18:24
//
#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 package mypackage, the generated function is declared inside the namespace hierarchy allcode::myPackage.

type codegen/lib/foo/bar.h
//
// File: bar.h
//
// MATLAB Coder version            : 5.1
// C/C++ source code generated on  : 24-Aug-2020 19:18:24
//
#ifndef BAR_H
#define BAR_H

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

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

#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            : 5.1
// C/C++ source code generated on  : 24-Aug-2020 19:18:24
//
#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;
    };
  }
}

#endif

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

See Also

| |

Related Topics