Skip to main content
vbruno1
Associate III
December 12, 2016
Question

mixing code c and c++

  • December 12, 2016
  • 13 replies
  • 6134 views
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++
    This topic has been closed for replies.

    13 replies

    Alan Chambers
    Associate III
    December 12, 2016
    Posted on December 12, 2016 at 13:33

    I write all of my code on STM32s in C++. I'm using IAR. There are no problems calling C APIs such as CubeHAL or the Standard Peripheral Library, or <Whatever>OS. This is not surprising because C is almost always valid C++. I don't use any third party C++ libraries, but can't imagine that being difficult. Basically, just write a C++ project and it'll all work out. I would avoid STL. I don't use exceptions or RTTI either, but suspect these are not as expensive some people claim. 

    I typically implement drivers and other application objects (FSMs, protocols, etc.) as Myers singletons to ensure that they are initialised once, in the correct order, and globally available, but this is just preference. I usually give them abstract interfaces to hide the implementation and facilitate unit testing.

    vbruno1
    vbruno1Author
    Associate III
    December 12, 2016
    Posted on December 12, 2016 at 14:19

    Thank you for answer.

    But in my case I write the main in c and not in cpp. I think that this complicate the write queues, wrapper code.

    Alan Chambers
    Associate III
    December 12, 2016
    Posted on December 12, 2016 at 14:26

    Why is main() in C? I think there is no advantage to this. Writing C wrappers for C++ APIs is a pain. It can be done, of course, but I would not recommend it.

    valentin
    Associate III
    February 11, 2017
    Posted on February 11, 2017 at 21:06

    Update: This is actually even easier than I thought.

    After creating the original C project (and having worked with it for 2 months now) I found out that I can just convert it using the eclipse/truestudio built in converter feature (file - new - other - convert project).

    This worked perfectly well with a full blown HAL + FreeRTOS + USB + FATFS project.

    All I had to do was copy the symbols and directories from the C compiler over to C++. And I changed the main.c name to main.cpp. Don't know yet whether that makes a difference though.

    But the C++ compiler is so much better as it is more tedious about clean&proper coding. I found some places where I've been a bit slack before and the C compiler just didn't bother.

    So in short, the workflow:

    1. Create CubeMX project

    2. Import to TrueStudio (in my case, haven't tested anything else yet)

    3. Convert to C++ and copy compiler settings from C. All easily doable in the GUI settings part

    4. rename main.c to main.cpp

    5. done, start coding/including C++ code

    6. Don't forget to add the C++ wrapper to your header files (that got me a couple of times).

    Alan Chambers
    Associate III
    February 13, 2017
    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.

    valentin
    Associate III
    February 14, 2017
    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?