Convert project to C++

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Convert project to C++

Jump to solution
4,695 Views
davenadler
Senior Contributor I

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

Labels (1)
1 Solution
2,143 Views
davenadler
Senior Contributor I

I just had to update a project from C to C++, so here's the complete answer:

(0) Before starting, make a copy of the initial project!
    Verify you really got a complete copy (as opposed to links to the same files on disk)!
    Verify you can access and build both copies within Eclipse!
    Close Eclipse.

(1) Edit .project file:
    in <natures> section, add:
    <nature>org.eclipse.cdt.core.ccnature</nature>

For C++, all the include paths and defines from the C portions of the project will be missing.
You'd like to be able to set common include paths and defines for (debug/release)x(C/C++).
Unfortunately, there's no way to enforce a common heirarchy for these in Eclipse (short of using your own make file).
Editing characteristics for multiple configurations in Eclipse is guaranteed to not do what you want.
So...

(2) In Eclipse, insert dummy include path and define for each of the (C++ debug/release).
    Close Eclipse.
    Edit .cproject to copy required paths and defines to each configuration (overwriting dummy created above).

Now it should compile (including any C++ modules you've added or changed from C).
But it won't yet link, because C-link is implicitly replaced with C++-link,
which looses all your linker settings (flags, libraries).
So...

(3) Copy the linker settings from your original project's settings
    Add any required C++-specific settings (ie don't use exceptions)

Hope that helps!
Best Regards, Dave

View solution in original post

6 Replies
2,144 Views
davenadler
Senior Contributor I

I just had to update a project from C to C++, so here's the complete answer:

(0) Before starting, make a copy of the initial project!
    Verify you really got a complete copy (as opposed to links to the same files on disk)!
    Verify you can access and build both copies within Eclipse!
    Close Eclipse.

(1) Edit .project file:
    in <natures> section, add:
    <nature>org.eclipse.cdt.core.ccnature</nature>

For C++, all the include paths and defines from the C portions of the project will be missing.
You'd like to be able to set common include paths and defines for (debug/release)x(C/C++).
Unfortunately, there's no way to enforce a common heirarchy for these in Eclipse (short of using your own make file).
Editing characteristics for multiple configurations in Eclipse is guaranteed to not do what you want.
So...

(2) In Eclipse, insert dummy include path and define for each of the (C++ debug/release).
    Close Eclipse.
    Edit .cproject to copy required paths and defines to each configuration (overwriting dummy created above).

Now it should compile (including any C++ modules you've added or changed from C).
But it won't yet link, because C-link is implicitly replaced with C++-link,
which looses all your linker settings (flags, libraries).
So...

(3) Copy the linker settings from your original project's settings
    Add any required C++-specific settings (ie don't use exceptions)

Hope that helps!
Best Regards, Dave

682 Views
GanesanGuru
Contributor IV

Hi @davenadler ,

Can you explain it further - as I can't get it.Let'me explain where I'm stuck.

I've converted project into C/C++ like this and I checked MCU C++ Libraries for the include files/search paths, its completely empty as attached below imgScreenshot from 2023-11-08 15-21-57.png

 

So, whether we have to include the path additionally? If so, let me know which else should be added so @davenadler ...Thanks in advance

0 Kudos
2,143 Views
davenadler
Senior Contributor I

I see 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/

Checking an example C++ enabled project created by new project wizard, I see that this is set in the linker miscellaneous options.

Adding -specs=nosys.specs to my upgraded project links....

0 Kudos
2,143 Views
ZhangJennie
NXP TechSupport
NXP TechSupport

-specs=nosys.specs is to tell the linker not to include any special system library functionality.
it is normally used when project use other RTOS that need not use system library.
for example, if your C++ project includes SDK, option -specs=nosys.specs is required.


Have a great day,
Jennie Zhang

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos
2,143 Views
davenadler
Senior Contributor I

Thanks Jennie - Unfortunately its an incomplete answer. I updated the question above to add the next parts of the question. It would be great if you can answer both of the above questions!

Thanks!
Best Regards, Dave

0 Kudos
2,142 Views
ZhangJennie
NXP TechSupport
NXP TechSupport

I didn't really see your project. But from your file name "lwip_httpsrv_freertos_frdmk64f", this is from our SDK demo code under SDK install folder. if it is true, what's your SDK version? I need to get your confirmation to check it further. Thanks.


Have a great day,
Jennie Zhang

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos