cancel
Showing results for 
Search instead for 
Did you mean: 

mixing code c and c++

vbruno1
Associate III
Posted on December 12, 2016 at 12:03

Hi guys.

I'm trying to write a mixing project code c and c ++.

The aim would be to take advantage of high-level libraries written in C ++ and we link  these in c.

You have experience about? When having to import in C and libraries c++ and classes, where you use polymorphism and subclasses are defined, you have ideas or experience on how to act?

I work in the IAR environment and am facing this problem by

https://www.iar.com/support/tech-notes/compiler/mixing-c-and-c/

For example I should link the library in the attached file.

Thank you for answers.

#class #mix-c-and-c++
13 REPLIES 13
Posted on February 13, 2017 at 09:46

You make a curmudgeon happy, Valentin. I've been pushing C++ for embedded projects for at least 10 years, with only moderate success.

Posted on February 14, 2017 at 02:30

I guess the next step now is to get proficient enough in C++ to use it safely in embedded environments.

I just skimmed an article explaining the do's and don'ts and how EmbeddedC++ was supposed to solve that.

I gues the fact that ec++ is 'dead?' means that regular c++ has matured enough to handle memory allocation etc manually if required?

Is there any good introduction somewhere for c++ in embedded (low memory) environments?

Posted on February 14, 2017 at 09:58

Using C++ safely is easier than using C safely, in any environment.

I don't know about any particular issues with C++ (as a language) for embedded projects. It's possible that exceptions are not implemented very efficiently and entail a significant runtime penalty. Or not. This may depend on the compiler. I would advise against using RTTI under any almost circumstances (embedded or otherwise). Code using this has always seemed fragile to me.

As with C, dynamic memory allocation can be an issue. Using new and delete is no worse than using malloc() and free(). I never rely on any generic allocator, but use memory pools of fixed sized objects when the need arises. I wrote a really simple template containing an array of T and a singly linked list of pointers to those objects to be the free list. Even better, if you care about such things, overload operator new for T to hide the memory pool implementation from the rest of the code...

I generally avoid the STL for embedded. I'm sure it's very clever and possibly even very space efficient, but I really don't know what's going on under the hood. Tons of dynamic allocation from the heap, I imagine. Obviously you can write your own allocators, but that is a path I am yet to tread.

What do you mean by low memory? I recently wrote something for an MSP430 with 512B RAM and 8kB ROM. I used C. I would like to have tried C++, but it wasn't my call. I don't *know* that C++ would be have been too expensive... Classes are no bigger than equivalent C structs, except they'll have a pointer to the vtable if the class has virtuals. Member functions have a hidden parameter (this), which is equivalent to passing the address of a struct object to a C function. I guess there are default constuctors and whatnot, but these replace the initialisation code you'd put after every struct declaration... It would be an interesting experiment.

Posted on July 11, 2017 at 23:20

A short update:

I am now excessively using C++ and it's simply working great. One project is using member-threads (FreeRTOS) for dynamically created objects and is currently using ~120kb (F205) while the other is simply using 2 structs with member functions to make my live easier - no RTOS and 24kB code size (F042).

I love how one can easily mix C and C++ and pick just the wanted features from C++ to keep the code small.