Any G++ CPP demo project running on KL?

cancel
Showing results for 
Search instead for 
Did you mean: 

Any G++ CPP demo project running on KL?

Jump to solution
1,092 Views
kai_liu
Senior Contributor I

I am trying to support FRDM-KL25Z with WIRING API, aka Arduino/Wiring projects. And it must be build with GCC. Therefore, I've downloaded many projects build with gcc-arm-none-eabi (GCC-ARM-Embedded). Most of the code can be built and run successfully. Thanks to these project owners of thest open source projects.

 

It is still headache to port these demo to CPP. I referred an application note from state-machine.com, Building bare-metal ARM with GCC.pdf. It seems we have to modify a lot of things in makefile/linker script/source with patches and custom functions.

 

Actually I can build it with g++, but when I met following error report.

 

c:/gcc-arm/gcc-arm-none-eabi-4_7-2013q1-20130313-win32/bin/arm-none-eabi-gcc ./Project_Settings/Startup_Code/startup.o . /Sources/sa_mtb.o ./Project_Settings/Startup_Code/kinetis_sysinit.o ./Sources/main.o ./Sources/mini_cpp.o -mcpu=cortex-m 0 -mthumb -nostartfiles -T./Project_Settings/Linker_Files/MKL25Z128_flash.ld -Wl,-Map=LED_Toggle_flash.map,-cref,-no-war n-mismatch    -o LED_Toggle_flash.elf c:/gcc-arm/gcc-arm-none-eabi-4_7-2013q1-20130313-win32/bin/../lib/gcc/arm-none-eabi/4.7.3/../../../../arm-none-eabi/lib/ armv6-m\libc.a(lib_a-sbrkr.o): In function `_sbrk_r': sbrkr.c:(.text._sbrk_r+0xc): undefined reference to `_sbrk' collect2.exe: error: ld returned 1 exit status make: *** [LED_Toggle_flash.elf] Error 1

 

Does anyone knows what should I to fix it? Thanks in advance.

 

toolchain: gcc-arm/gcc-arm-none-eabi-4_7-2013q1-20130313-win32

platform: Windows XP

 

 

I have attached my project in this thread (the original source comes from mcueclipse.com).

 

Allan K Liu

Original Attachment has been moved to: mcuoneclipse.zip

Tags (1)
1 Solution
332 Views
kai_liu
Senior Contributor I

BlackNight

A working demo available on Eclipse + arm-gcc-embedded + FRDM + Windows XP.

After reading blog tutorial from Erich (mcuoneclipse.com), I changes the C version LED blink demo to CPP by changing its linker script, test classes, gcc/g++ options, libraries.... And debug it on Eclipse + PEmicro gdbserver. Now it is working.

Next step, I will prepare a makefile for it and port Arduino/Wiring API on it.

Thanks for your support.

Allan K Liu

View solution in original post

0 Kudos
5 Replies
332 Views
Kan_Li
NXP TechSupport
NXP TechSupport

Hi Allan K Liu,

Seems _sbrk is not defined in the libc included by your project, and you might have to define it in your project. Please kindly refer to the following link for more details.linker error undefined ref to `_sbrk' - EmbDev.net

Hope that helps,

B.R

Kan

333 Views
kai_liu
Senior Contributor I

BlackNight

A working demo available on Eclipse + arm-gcc-embedded + FRDM + Windows XP.

After reading blog tutorial from Erich (mcuoneclipse.com), I changes the C version LED blink demo to CPP by changing its linker script, test classes, gcc/g++ options, libraries.... And debug it on Eclipse + PEmicro gdbserver. Now it is working.

Next step, I will prepare a makefile for it and port Arduino/Wiring API on it.

Thanks for your support.

Allan K Liu

0 Kudos
332 Views
Monica
Senior Contributor III

Kai,

Thanks a lot for sharing and contributing!

We're happy to have you being part of Community!

Keep rocking! :smileywink:

Monica.

0 Kudos
332 Views
kai_liu
Senior Contributor I

Thanks Kan,

I have gone through that page already.

The problem is caused by mini_cpp.cpp, which includes stdlib.h to refer malloc() and free() for new and delete operators in C++. If I remove the mini_cpp.cpp, compile + link is all right. Otherwise it reports such error.

To fix that, I have to offer sbrc by default or by myself. To write my own code as sbrc is one option. Howerer I have to enable some options during linking. I just want to find if I have done anything wrong here. because I think sbrc should be available somewhere in the toolchain lib.

Yours sincerely

Allan K Liu

0 Kudos
331 Views
kai_liu
Senior Contributor I

Dear Kan & FSL team,

Thank you for your information. Now I got it why it reports that. According to its document resides in

C:\gcc-arm-none-eabi-4_7-2013q1-20130313-win32\share\doc\gcc-arm-none-eabi\pdf\libc.pdf

Chapter 12. System Calls

The C subroutine library depends on a handful of subroutine calls for operating system services. If you use the C library on a system that complies with the POSIX.1 standard (also known as IEEE 1003.1), most of these subroutines are supplied with your operating system.

If some of these subroutines are not provided with your system—in the extreme case, if you are developing software for a “bare board” system, without an OS—you will at least need to provide do-nothing stubs (or subroutines with minimal functionality) to allow your programs to link with the subroutines in libc.a.

After cloning some source file from other project and updating linker script, it can be built by extra 1KB overhead  than C version.

c:/gcc-arm-none-eabi-4_7-2013q1-20130313-win32/bin/arm-none-eabi-size LED_Toggle_flash.elf
   text    data     bss     dec     hex filename
   2964    2140    2104    7208    1c28 LED_Toggle_flash.elf

Now my new problem is, the orginal C LED blink demo doesn't work anymore. Well, it is really an issue. since now I need something like gdb to debug it.

I know C version is good enough for embedded system. However, CPP will be more and more popular.

It comes around to the same point, if FSL can offer any working CPP demo code for GCC on KL, that will be a great help.

After struggling some hours, now I have a build successfully demo (but LED doesn't blink). In current attached file. I have made following modifications:

  • Makefile (CPPFLAGS for -fno-rtti -fnoexceptions)
  • linker script (sections for heap)
  • source file (main.cpp, mini_cpp.cpp for new and delete operators, myclasses as basic CPP test)

I don't know if my modification is correct or not. Any comments are highly appreciated.