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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • While Performing Dependency Selection, I Avoid the Loss Of Sleep From Node.js Libraries' Dangers
  • Understanding ldd: The Linux Dynamic Dependency Explorer
  • Practical Use of Weak Symbols
  • Python and Open-Source Libraries for Efficient PDF Management

Trending

  • How to Convert XLS to XLSX in Java
  • Microsoft Azure Synapse Analytics: Scaling Hurdles and Limitations
  • It’s Not About Control — It’s About Collaboration Between Architecture and Security
  • Recurrent Workflows With Cloud Native Dapr Jobs
  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.0K 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, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • While Performing Dependency Selection, I Avoid the Loss Of Sleep From Node.js Libraries' Dangers
  • Understanding ldd: The Linux Dynamic Dependency Explorer
  • Practical Use of Weak Symbols
  • Python and Open-Source Libraries for Efficient PDF Management

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!