Post navigation

C++ with Kinetis Design Studio

Unlike CodeWarrior, the Kinetis Design Studio (at least in V1.1.1) does not offer a choice between C and C++ projects. That makes sense with the GNU ARM Eclipse plugins, other than the CodeWarrior gcc integration, there is no need for setting up a special tool chain for C++ (see “Compiling C Files with GNU ARM G++“). While this is great, things are not perfect yet, so I’m providing in this post the information needed to properly setup a C++ project with Kinetis Design Studio V1.1.1.

Debug View of Startup Code Calling C++ Constructors

With Kinetis Design Studio, I can create four types of project using the menu File > New > Kinetis Design Studio:

New Kinetis Design Studio Project

❗ The usual way in Eclipse to create a project with File > New > C Project of File > New C++ Project is not supported, as KDS is has implemented its own wizard.

The project type selection is implemented with two check boxes:

Kinetis SDK

Processor Expert

KDS Project Type

The combination of the two check boxes gives four different project types:

Bare projects: no Kinetis SDK, no Processor Expert, just a plain project, available for all Kinetis devices: Both options

Processor Expert projects, available for all Kinetis Devices listed

Kinetis SDK projects (for devices where the Kinetis is supported)

Processor Expert projects with Kinetis SDK (for Kinetis SDK devices)

Bare Projects with C++

If using C++ with a bare project type, then everything is setup correctly. You only need to make sure that header files included for C++ files are listed in the compiler include path settings:

C++ Include Path Settings

If you are *NOT* seeing the C++ Compiler and Linker options (like below), then you probably have loaded a project from the KDS beta version. As the beta projects are not fully supported in the non-beta version of KDS, I recommend you create a new project in KDS V1.1.1.

KDS Project from Beta KDS does not show C++ Options

Processor Expert Projects

The same point as above (C++ Include Path settings) apply to Processor Expert projects as well. However, there is the first issue with the provided startup code that the constructors for the static class objects are not called in startup.c:

Startup Code not calling static constructors

Simply enabling __call_static_initializers() will not help, as this function is not defined :-(. Instead, the usual _start() function of the GNU library needs to be called. For this, I’m doing the following changes to the Processor Expert startup code:

Fixed Processor Expert Startup Code for C++

Add a prototype to _start() function

Remove the zero_fill_bss() call, as this will be performed by the library in _start()

With this, the project runs as expected and initializes all static object constructors.

Kinetis SDK Projects and Kinetis SDK with Processor Expert Projects

For Kinetis SDK projects (with or without Processor Expert), no other changes are needed as adding the C++ header file include paths, as for the bare projects. The Kinetis SDK startup code already calls _start(), and a heap size should be already configured. All good :-).

Otherwise there will be a crash during _start() function in the library.

Summary

While I do not need to select C or C++ project type during project creation, the current V1.1.1 Kinetis Design Studio created projects still need some tweaking to properly support C++ projects, most for the Processor Expert (only) projects as they use a different startup code. It is not clear why, but for sure the SDK startup code is handling things as it should be done. I hope the above steps help you to get C++ projects running with Kinetis Design Studio.

About Erich Styger

21 thoughts on “C++ with Kinetis Design Studio”

My experience with the KDS wizard and PE is limited, so please excuse me if this does not apply.

For C++ projects it is recommended to also add “-fno-exceptions -fno-rtti -fno-use-cxa-atexit -fno-threadsafe-statics”.

It might also be useful to redefine __gnu_cxx::__verbose_terminate_handler() and the “C” function __cxa_pure_virtual(). An example of such redefinitions is in /system/src/newlib/_cxx.cpp for projects generated with the GNU ARM Eclipse C++ templates.

I’m having similar problems with the KL05Z and Kinetis Design Studio. Under KDS 1.1.1, I’ve created a new Kinetis Design Studio Project as bare metal (options KINETIS SDK and Processor Expert disabled). The generated project compiles correctly, but hangs when arrives to _start. I’ve reviewed the include directories, and they are OK. By replacing the _start call by a main call, the application can run, but there’re still problems, because variables (global, local or static) are not being initialized. Must be a problem related to the startup files, but I don’t know how to fix it. Any suggestion? All help would be welcome.

Hi Bryan,
Well, I would need to find some time for this right now, I see if I can spend some time on the week end on it, but chances are rather low right now 😦
What is the problem you are running into?

Hi Erich, I’m running KDS 2.0.0 and using the httpsrv_frdmk64f example project from KSDK 1.1.0. When I go into the project settings, I do not see C++ in the list of tools. From what you’ve said, the best thing to do is to create a new project from scratch. I was hoping to avoid this, and since I’m using a more recent version of the KSDK, I thought I’d ask you if you know of any way to add the C++ settings to Tool Settings, or of any conversion process that would work?

I have no idea why the Kinetis SDK projects are not including the C++ panels. I guess they probably have generated the projects, and somehow missed the C++ settings in the generated project files. The easiest way is probably to create a new project 😦

Hi Erich!
I’m running KDS 3.1.0 and KSDK 1.2 Processor-Expert using FRDM-KL43Z, and trying to call C++ member functions (typical FIR filter) from main.c so I can filter a test signal, but I am having trouble doing this. My problem may be in the wrapper functions. To be honest this is the first time I try it, and I am assuming it is possible to do this.
Do you have any tutorial on how to call C++ member functions from main.c?

Right. So in main.c I just declared the wrapper functions I wrote in the .cpp. But when it runs, they seem to be ignored and in the end I end up with an array with the same values as before the filtering(C++). Do you have any clear example showing how to do this?

Hi again Erich,
I have been able to finally call and run C++ code from C, but always had trouble in debugging since I cannot simply step into the C++ functions from main.c. But if I place a breakpoint inside the C++ functions, it will stop there. I wonder why this happen?
Anyway, what is the typical way to check the amount of SRAM being used currently at any given point during debugging? I am running some RAM-consuming C++ functions and the mcu SRAM is very limited, so for code optimization it would be nice to know how much SRAM is being used at any given breakpoint when debugging.