AnsweredAssumed Answered

Convert project to C++

Question asked by Dave Nadler on Oct 23, 2016
Latest reply on May 22, 2017 by Dave Nadler

I have a large project to which I would like to add some C++ modules.
No, I do not want to recreate the project from scratch.
Per this link I added the C++ 'nature' to the .project file:
KDS - add C++ support to existing C proj

Unfortunately, the project fails to link with the following error:

Building target: lwip_httpsrv_freertos_frdmk64f.elf
Invoking: Cross ARM C++ Linker
arm-none-eabi-g++ -mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 -O0 -fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections -Wall  -g -Xlinker --gc-sections -Wl,-Map,"lwip_httpsrv_freertos_frdmk64f.map" -o "lwip_httpsrv_freertos_frdmk64f.elf"  ./utilities/fsl_debug_console.o ./utilities/fsl_sbrk.o  ./startup/startup_MK64F12.o ./startup/system_MK64F12.o  ./source/DAC_sine_wave.o ./source/heapsort.o ./source/lwip_httpsrv_freertos.o  ./lwip/src/netif/ppp/auth.o ./lwip/src/netif/ppp/chap.o ./lwip/src/netif/ppp/chpms.o ./lwip/src/netif/ppp/fsm.o ./lwip/src/netif/ppp/ipcp.o ./lwip/src/netif/ppp/lcp.o ./lwip/src/netif/ppp/magic.o ./lwip/src/netif/ppp/pap.o ./lwip/src/netif/ppp/ppp.o ./lwip/src/netif/ppp/ppp_md5.o ./lwip/src/netif/ppp/ppp_oe.o ./lwip/src/netif/ppp/randm.o ./lwip/src/netif/ppp/vj.o  ./lwip/src/netif/etharp.o ./lwip/src/netif/slipif.o  ./lwip/src/core/snmp/asn1_dec.o ./lwip/src/core/snmp/asn1_enc.o ./lwip/src/core/snmp/mib2.o ./lwip/src/core/snmp/mib_structs.o ./lwip/src/core/snmp/msg_in.o ./lwip/src/core/snmp/msg_out.o  ./lwip/src/core/ipv4/autoip.o ./lwip/src/core/ipv4/icmp.o ./lwip/src/core/ipv4/igmp.o ./lwip/src/core/ipv4/inet.o ./lwip/src/core/ipv4/inet_chksum.o ./lwip/src/core/ipv4/ip.o ./lwip/src/core/ipv4/ip_addr.o ./lwip/src/core/ipv4/ip_frag.o  ./lwip/src/core/def.o ./lwip/src/core/dhcp.o ./lwip/src/core/dns.o ./lwip/src/core/init.o ./lwip/src/core/lwip_timers.o ./lwip/src/core/mem.o ./lwip/src/core/memp.o ./lwip/src/core/netif.o ./lwip/src/core/pbuf.o ./lwip/src/core/raw.o ./lwip/src/core/stats.o ./lwip/src/core/sys.o ./lwip/src/core/tcp.o ./lwip/src/core/tcp_in.o ./lwip/src/core/tcp_out.o ./lwip/src/core/udp.o  ./lwip/src/api/api_lib.o ./lwip/src/api/api_msg.o ./lwip/src/api/err.o ./lwip/src/api/netbuf.o ./lwip/src/api/netdb.o ./lwip/src/api/netifapi.o ./lwip/src/api/sockets.o ./lwip/src/api/tcpip.o  ./lwip/port/ethernetif.o ./lwip/port/sys_arch.o  ./freertos/portable/port.o  ./freertos/croutine.o ./freertos/event_groups.o ./freertos/heap_4.o ./freertos/list.o ./freertos/queue.o ./freertos/tasks.o ./freertos/timers.o  ./drivers/fsl_adc16.o ./drivers/fsl_clock.o ./drivers/fsl_common.o ./drivers/fsl_dac.o ./drivers/fsl_dmamux.o ./drivers/fsl_edma.o ./drivers/fsl_enet.o ./drivers/fsl_gpio.o ./drivers/fsl_pit.o ./drivers/fsl_sim.o ./drivers/fsl_smc.o ./drivers/fsl_uart.o  ./board/board.o ./board/clock_config.o ./board/fsl_phy.o ./board/pin_mux.o   
c:/freescale/kds_v3/toolchain/bin/../lib/gcc/arm-none-eabi/4.8.4/../../../../arm-none-eabi/lib/armv7e-m/fpu\libg.a(lib_a-exit.o): In function `exit':
exit.c:(.text.exit+0x16): undefined reference to `_exit'
collect2.exe: error: ld returned 1 exit status

Next, I saw from this post that link option -specs=nosys.specs may be required:

https://mcuoneclipse.com/2014/06/15/tutorial-diy-kinetis-sdk-project-with-eclipse-startup/

I added this option to the linker miscellaneous options, and the project builds but I've not yet instantiated any C++ classes. Unfortunately...

 

Next, I added a test instantiation of a class I defined. The link fails as follows:

Invoking: Cross ARM C++ Linker
arm-none-eabi-g++ -mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 -O0 -fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections -Wall  -g -Xlinker --gc-sections -Wl,-Map,"lwip_httpsrv_freertos_frdmk64f.map" --specs=nosys.specs -o "lwip_httpsrv_freertos_frdmk64f.elf"  ./utilities/fsl_debug_console.o ./utilities/fsl_sbrk.o  ./startup/startup_MK64F12.o ./startup/system_MK64F12.o  ./source/DAC_sine_wave.o ./source/heapsort.o ./source/lwip_httpsrv_freertos.o  ./lwip/src/netif/ppp/auth.o ./lwip/src/netif/ppp/chap.o ./lwip/src/netif/ppp/chpms.o ./lwip/src/netif/ppp/fsm.o ./lwip/src/netif/ppp/ipcp.o ./lwip/src/netif/ppp/lcp.o ./lwip/src/netif/ppp/magic.o ./lwip/src/netif/ppp/pap.o ./lwip/src/netif/ppp/ppp.o ./lwip/src/netif/ppp/ppp_md5.o ./lwip/src/netif/ppp/ppp_oe.o ./lwip/src/netif/ppp/randm.o ./lwip/src/netif/ppp/vj.o  ./lwip/src/netif/etharp.o ./lwip/src/netif/slipif.o  ./lwip/src/core/snmp/asn1_dec.o ./lwip/src/core/snmp/asn1_enc.o ./lwip/src/core/snmp/mib2.o ./lwip/src/core/snmp/mib_structs.o ./lwip/src/core/snmp/msg_in.o ./lwip/src/core/snmp/msg_out.o  ./lwip/src/core/ipv4/autoip.o ./lwip/src/core/ipv4/icmp.o ./lwip/src/core/ipv4/igmp.o ./lwip/src/core/ipv4/inet.o ./lwip/src/core/ipv4/inet_chksum.o ./lwip/src/core/ipv4/ip.o ./lwip/src/core/ipv4/ip_addr.o ./lwip/src/core/ipv4/ip_frag.o  ./lwip/src/core/def.o ./lwip/src/core/dhcp.o ./lwip/src/core/dns.o ./lwip/src/core/init.o ./lwip/src/core/lwip_timers.o ./lwip/src/core/mem.o ./lwip/src/core/memp.o ./lwip/src/core/netif.o ./lwip/src/core/pbuf.o ./lwip/src/core/raw.o ./lwip/src/core/stats.o ./lwip/src/core/sys.o ./lwip/src/core/tcp.o ./lwip/src/core/tcp_in.o ./lwip/src/core/tcp_out.o ./lwip/src/core/udp.o  ./lwip/src/api/api_lib.o ./lwip/src/api/api_msg.o ./lwip/src/api/err.o ./lwip/src/api/netbuf.o ./lwip/src/api/netdb.o ./lwip/src/api/netifapi.o ./lwip/src/api/sockets.o ./lwip/src/api/tcpip.o  ./lwip/port/ethernetif.o ./lwip/port/sys_arch.o  ./freertos/portable/port.o  ./freertos/croutine.o ./freertos/event_groups.o ./freertos/heap_4.o ./freertos/list.o ./freertos/queue.o ./freertos/tasks.o ./freertos/timers.o  ./drivers/fsl_adc16.o ./drivers/fsl_clock.o ./drivers/fsl_common.o ./drivers/fsl_dac.o ./drivers/fsl_dmamux.o ./drivers/fsl_edma.o ./drivers/fsl_enet.o ./drivers/fsl_gpio.o ./drivers/fsl_pit.o ./drivers/fsl_sim.o ./drivers/fsl_smc.o ./drivers/fsl_uart.o  ./board/board.o ./board/clock_config.o ./board/fsl_phy.o ./board/pin_mux.o   
./utilities/fsl_sbrk.o: In function `_sbrk':
C:/K64/SDK_2.0_FRDM-K64F/devices/MK64F12/utilities/fsl_sbrk.c:65: undefined reference to `__HeapLimit'
collect2.exe: error: ld returned 1 exit status

Apparently, as soon as I instantiate an instance of a class, some memory management code is dragged in...

 

Background: As is normal for classes in systems with no dynamic memory, the test class overrides new, new[], delete, and delete[] with versions that do not use free storage. The compile options for the example cpp file were set to no exceptions and no RTTI. So, I expected no use of free storage runtime...

 

This leaves two important questions:

  1. If I want to use the runtime's free storage, what project options are required to link and run ?
  2. Most importantly: To avoid dragging in any runtime free storage (as normal for our projects), what additional steps are required?

 

Thanks!
Best Regards, Dave

 

PS: I've attached the example code for you to try. Just add this to a project converted from C as above.

Original Attachment has been moved to: Example_for_NXP_heapsort.zip

Outcomes