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.
With Kinetis Design Studio, I can create four types of project using the menu File > New > Kinetis Design Studio:
❗ 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
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:
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.
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:
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:
- Add a prototype to _start() function
- Remove the zero_fill_bss() call, as this will be performed by the library in _start()
- Instead of calling main(), call _start() from the GNU library
That should it be. But wait! You might run into a hard fault 😦
💡 See “Oh my! An Interrupt…” how to generate an interupt handler for each interrupt vector, and “A Processor Expert Component to Help with Hard Faults” how to debug hard faults.
The issue is with the newlib-nano libraries in KDS which need an excessive amount of heap memory (see “Switching ARM GNU Tool Chain and Libraries in Kinetis Design Studio“). The solution is to add at least 1 KByte of Heap memory. This is configured in the Build options tab of the CPU component of the project:
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 :-).
KDS with Launchpad and C++
If using KDS with the launchpad tools (see “Switching ARM GNU Tool Chain and Libraries in Kinetis Design Studio“), the GNU library code, it is necessary to add the following symbol __stack to the linker file:
__stack = _estack;
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.
Happy C++ Constructing 🙂
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.
LikeLike
Hi Liviu, thanks for these points. They are applicable as well the KDS wizard generated projects. Thanks for sharing!
LikeLike
Hi
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.
Thanks
LikeLike
Hi Eduardo,
I might have responded to the same question in the Freescale community/forum. I guess your linker file is wrong (I saw a report in the forums about something like that). I have posted my linker file
Erich
LikeLike
Is there any way that you could post an example project with Processor Expert and c++ support and files for KDS? still struggling to get it working.
LikeLike
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?
Erich
LikeLike
I was trying to enable c++, but could not get any c++ code to run on the board. Turns out it was a gdb server problem and not the code (I’m still very new with Processor Expert and KDS).
LikeLike
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?
LikeLike
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 😦
LikeLike
Ok, I’ve already started on that and porting over everything. Thanks for helping everyone in the community out like this!
LikeLike
Pingback: Semihosting with GNU ARM Embedded (Launchpad) and Kinetis Design Studio | MCU on Eclipse
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?
(I attach the main.c and .cpp file, in case you want to check what I have done:)
https://www.dropbox.com/s/lypxlqhge1agsj9/K43Z-%20example%20for%20PIT.zip?dl=0
As usual, Many thanks for your time and effort!
Domingos
LikeLike
Hi Domingos,
if you want to call C++ member functions, you have to do this from C++ code. You cannot do this from normal C, unless you create wrapper functions to this.
LikeLike
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?
Many thanks,
Joao
LikeLike
Hi Joao,
I don’t have an exact example for this, as this is a very generic thing. You need to debug/step through your code so you can see what is going on. Have you tried that?
Erich
LikeLike
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.
Many thanks again for your time!
LikeLike
C++ stepping might not work correctly in the context of inlined functions, not sure if this is your case here. You might try assembly stepping instead (see https://mcuoneclipse.com/2012/10/27/assembly-instruction-stepping/).
As for the free heap memory: that depends on the heap implementation. With the standard heap you won’t have much visibility here. But you could check in the debugger the heap_end variable (see https://mcuoneclipse.com/2014/03/16/freertos-malloc-and-sp-check-with-gnu-tools/): if you know where the heap starts, you can use this to calculate the free heap.
LikeLike
HI Guys, I’m trying to do something simple. I want include some files .c with some rotines. The KDS doens’t compile them. What’s wrong?
Regards
LikeLike
Hi Julio,
And what does the compiler say? I think this is not a KDS problem, but more a general C/C++ programming error?
Erich
LikeLike
Hi Erich, it not recognize data types like uint8_t. I not sure about what adicional includes I must declare.
Regards.
LikeLike
Add
#include “stdint.h” (use ‘less than’ and ‘greater than’ instead of the double quotes, it does not allow me to put them here).
LikeLike
Pingback: From C to C++: Converting Eclipse C Projekts | MCU on Eclipse