Introduction
Note: The Wind River C++ compiler is incompatible with GNU C++ binaries.
VxWorks Configuration for C++
By default, VxWorks includes only minimal support for C++. You can support all or part of C++ functionality by including all or some components.
INCLUDE_CTORS_DTORS
This component is included by default. Ensure the compiler generates initialization functions, including the initialization of C++ static objects, called at kernel startup.
INCLUDE_CPLUS
Includes basic support for C++ applications. Typically used together with INCLUDE_CPLUS_LANG.
INCLUDE_CPLUS_LANG
Includes support for C++ language features such as new, delete, and exception handling.
INCLUDE_CPLUS_IOSTREAM
Includes all library functionalities.
INCLUDE_CPLUS_DEMANGLER
This component is automatically added when using INCLUDE_CPLUS and INCLUDE_SYM_TBL components.
C++ Header Files
Each compiler has its own header files and libraries, and users do not need to specify the folder where they reside; the compiler will look in specific directories.
Using C++ to Start Tasks
Any task started using C++ must use the VX_FP_TASK option. By default, tasks initiated from host tools automatically come with this option (e.g., Wind Shell).
Note: Not using this option will lead to difficult-to-debug and unpredictable floating-point register corruption at runtime. The C++ exception handler will use floating-point registers. This option ensures that the floating-point registers are saved and restored when an exception occurs.
Calling Between C and C++ Code
If calling C++ code from C, you must use extern “C” to export the prototype. The following method allows the code to be called by both C and C++.
Notes on the C++ Compiler
Small data area (SDA) reallocation is only supported by PowerPC and MIPS; C++ code will not work. Do not use the -Xsmall-const=8 -Xsmall-data=8 options of the Wind River Diab compiler, or the -G8 -msdata=eabi options of the GNU compiler.
Using C++ in Signal Handlers and ISRs
Using C++ code in signal handlers and ISRs must be done with special care. For more information, see the signal handling and writing ISR chapters.
Downloading C++ Kernel Modules
C++ code to be downloaded to the VxWorks kernel should be linked into a single downloadable object module. It must be munched, and any COMDAT and linkonce sections must be filled.
Using a Single C++ Module
The VxWorks loader only supports self-contained C++ modules. A self-contained C++ module is one that does not use classes from other modules, nor is its classes used by other modules. Specifically, the module must either contain a copy of the standard library or not use the C++ standard library at all.
To produce a self-contained module, all downloadable C++ objects must be linked into a single downloadable object module.
Unloading a non-self-contained C++ module may lead to corruption of references to objects or data structures created in other modules. This can happen if part of the iostream standard library is initialized from a module that is unloaded later. In this case, any further use of iostream will lead to kernel exceptions (accessing illegal addresses).
Munching an Application Module
Before the C++ module is downloaded to the VxWorks kernel, it must undergo an additional step called munching due to historical reasons. Munching performs the following tasks:
-
Support for static object initialization.
-
Ensures that the correct constructors and destructors for all static objects are called in order.
-
For the Wind River compiler, automatically fills COMDAT sections; for the GNU compiler, automatically fills linkonce sections.
Munching must be performed after compilation and before downloading.
Example of Munching
For each toolchain, the following example compiles a C++ application source code hello.cpp, runs munch on the .o file, compiles to generate the ctdt.c file, and links the application and ctdt.o to produce a downloadable module, hello.out.
Using the Wind River Toolchain
Note: The -r4 option fills any COMDAT sections included in the input files.
Using the GNU Toolchain
Note: The VxWorks kernel object loader does not directly support linkonce sections. Instead, linkonce sections must be merged and filled into the standard text and data sections before loading. The -T option is used to fill any included linkonce sections.
Using Generic Makefile Rules
If using a makefile, you can write simple munch rules (defining the appropriate CPU and TOOL) that can support both GNU and Wind River toolchains.
After munching, downloading, and linking, static constructors and destructors will be called.
Calling Static Constructors and Destructors
The kernel loader provides automatic and manual options to call static constructors and destructors.
Automatic calling is the default policy. Static constructors are executed only after the module has been downloaded and before the module loader returns to the caller. Static destructors are executed before the module is unloaded.
Manual calling means that the user must explicitly call static constructors after the module is loaded, but before running the application. The user must also explicitly call destructors after the task is completed and before the module is unloaded.
Static constructors can be called by querying cplusCtors. Static destructors can be called by cplusDtors. These functions take a separate module name as a parameter. However, you can also call these functions without parameters to call the static constructors and destructors of all objects loaded into the current system.
Note: When using the manual method, each module’s constructor must only be called once.
The calling strategy can be changed at runtime by calling the cplusXtorSet function. The current calling strategy can be obtained using cplusStratShow.
Differences in C++ Compilers
The Wind River C++ compiler uses the EDG style (Edison Design Group) and fully complies with the ANSI C++ standard. The GNU compiler supports most language features of the ANSI C++ standard. The binary files of both are not compatible. Therefore, the toolchain used to compile the VxWorks image and the toolchain used to compile downloadable kernel applications must be the same.
Template Instantiation
Runtime Type Information
Namespaces
No special considerations.
C++ Exception Handling
C++ exception handling is provided by the INCLUDE_CPLUS_LANG component. To disable C++ exception handling, you must:
-
Define the CXX_STL_VARIANT=abridged environment variable before creating the VIP project. This ensures the correct library is linked (exceptions disabled).
-
For GNU, add -mc++-abr; for Diab, add -Xc++-abr options.
C++ Demonstration
The factory example demonstrates various features of C++, including STL, user-defined templates, runtime type information (RTTI), and exception handling. The code is located in /target/src/demo/cplusplus/factory.
Using this example program, the kernel must include the following components:
Additionally, for the GNU compiler, the following components must also be included:
Compile the code as a downloadable kernel module, download it to the target board, and call testFactory to start. For example, use the following command in the shell: