DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • C/C++ Is Where Vulnerability Programs Go to Guess
  • Building a Java 17-Compatible TLD Generator for Legacy JSP Tag Libraries
  • Extracting Clean Excel Tables From PDFs Using Python + Docling
  • Crypto Agility for Developers: Build Agile Encryption Now

Trending

  • AI in Software Development: A Mirror, Not a Magic Wand
  • Optimizing Databricks Spark Pipelines Using Declarative Patterns
  • Top JavaScript/TypeScript Gen AI Frameworks for 2026
  • Product-Led Software Delivery: Intelligent Platforms for DevOps at Scale
  1. DZone
  2. Coding
  3. Frameworks
  4. From C to C++: Converting Eclipse C Projects

From C to C++: Converting Eclipse C Projects

In this article, look at an article on how to convert Eclipse C projects.

By 
Erich Styger user avatar
Erich Styger
·
Jul. 30, 20 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
12.6K Views

Join the DZone community and get the full member experience.

Join For Free

Creating a new project with Eclipse for a microcontroller these days is fairly easy, and I have the choice if I want to start the project with C or C++:

Choice of C and C++ for a new project

Still the embedded microcontroller world is dominated by C and not C++. So while it is easy to start with a C++ project, most vendor provided example or tutorial project are C projects. So how can I transform such project to C++?

To have C++ support in an Eclipse CDT project, I need basically two things:

  1. The project needs the C++ 'nature': with this I get the C++ compiler/build options
  2. The startup code needs to call the global C++ constructors
  3. Change the library settings

With this, I can add my C++ files to a former C project. The steps described here should be applicable to any Eclipse CDT project. Things might be different in details depending on your Eclipse distribution or SDK. I'm showing here screenshots using the NXP MCUXpresso IDE V11.1.1 using an SDK project provided as C language project (frdmk22f_led_blinky).

If a project has been created as C project, then usually it only shows the options for the C compiler:

Only C Compiler Options

Only C Compiler Options

To enable the project for C++, the following nature has to be added to the .project file.

Java
 




x


 
1
<nature>org.eclipse.cdt.core.ccnature</nature>



One way is to use the File > New menu and then select 'Convert to a C/C++ Project (Adds C/C++ Nature):

Convert to C++

Convert to C++

Another way to to this is using a text editor, either inside or outside Eclipse:

Be careful with using Eclipse built-in Editor. Close the project and re-open it afterwards.

Alternatively, the Eclipse built-in XML editor can be used to add the nature:

Added ccnature

Added ccnature

With latest CDT it is possible to add/remove a nature in the project properties too (although it is confusing as the C++ (CC) nature is shown as 'C Nature') (thanks to Jld00 for his comment!):

Adding C++ Nature in Project Properties

Adding C++ Nature in Project Properties

With this the settings include the C++ compiler options (close and re-open the project if the change has been made inside Eclipse):

C++ Compiler Options

C++ Compiler Options

With this, I can set compiler options for the C++ files in the project.

C++ Startup Code

Locate the C startup code in the project:

C startup code

C startup code

The easiest approach is to rename it to a C++ file with .cpp extension:

renamed startup code

Renamed startup code

With this the startup code gets compiled in C++ mode. This assumes that the startup code is prepared in written in a way that it actually can be compiled in C++ mode and that it uses the appropriate '__cplusplus' macros). The important thing is that in C+ mode it calls the global constructors. With the NewLib/Newlib-Nano libraries this is a call to the __libc_init_array() function before calling main():

Constructor Call

Constructor Call

The __libc_init_array() basically is a list of functions prepared by the linker with global initializers to initialize the library and call all the global constructors.

�� to verify that the constructors get called, have a global object defined and set a breakpoint in the constructor code.

Constructor Call during startup

Constructor Call during startup

With this the proper library initialization is done and constructors for global objects are called.

Library

The last thing is to make sure that the correct library is used. Reduced libraries like the 'RedLib' in MCUXpresso does not support C++ and cannot be used. NewLib-nano is optimized for size and does support C++ applications, but does not include exception handling by default (see links at the end of the article).

Check the library used: newlib-nano is fine if not using exceptions, otherwise switch to newlib (but be prepared for a code size increase):

Library Usage

Library Usage

Another way to select/switch the library is in the linker settings:

Library Selection

Library Selection

With this, the library can be used with C++.

Summary

It requires a few steps to turn a normal C project into a C++ enabled one: make sure the C++ nature is selected, the startup code is calling the global constructors and a C++ capable library is used.

Happy C++ing!

Links

  • C++ with Kinetis Design Studio
  • Processor Expert, gcc C++ and Kinetis-L with MQXLite
  • Using C++ Exceptions: https://miscsolutions.wordpress.com/2019/12/27/using-exceptions-in-c-embedded-software/
Eclipse Library

Published at DZone with permission of Erich Styger. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • C/C++ Is Where Vulnerability Programs Go to Guess
  • Building a Java 17-Compatible TLD Generator for Legacy JSP Tag Libraries
  • Extracting Clean Excel Tables From PDFs Using Python + Docling
  • Crypto Agility for Developers: Build Agile Encryption Now

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook