Mixing C and C++

Technical Note 46551

Arkitekturer:

All

Komponent:

compiler

Uppdaterad:

2015-11-06 13:16

Introduction

You want to call C functions from your C++ project.
Alternatively, you want to call C++ functions from your C project.

This Technical Note lists a few things to consider when mixing C and C++. Note that this is not a complete guide, but an attempt to highlight some common obstacles, and their solution.

Things to consider

1.

Use C-linkage. Put extern "C" { // your function declarations} in header files.
Note that this is required for both C++ and C header files:

  • For a C function to be able to call a C++ function.
  • For a C++ method to be able to call a C function.

2.

C++ has stronger type checking. For example, see the following C function:

void LCD_DisplayString(const uint8_t *text);

It can be called like this in C:

LCD_DisplayString("RTOS START");

With C++, you need to cast RTOS START to uint8_t* (from char*) to be able to compile. Even better than a cast, see (3) below.

3.

Wrap C-interfaces into C++ classes. For example, the LCD_DisplayString function above can be wrapped into class LCD::DisplayString().


Using C++ overloading, the cast between char* and uint8_t* can be hidden inside the LCD class methods:

void LCD::DisplayString(const char *text);
void LCD::DisplayString(const uint8_t *text);

In other words, you can avoid casting everywhere in your C++ code.

4.

In some areas, C++ has different behaviour. For example:

const int myglobal

..is not global in C++, since C++ global constants have static linkage.
Also, for example, this volatile access is not necessarily executed in C++:

void access(void) {
   *((volatile uint8_t*)0x20000000);
}

While in C, the volatile access is made. (The C and C++ language specifications differ when an object is accessed in a void context). To avoid problems, make sure to compile pure C-code as C (for example RTOS or BSP code with filename extensions ".c"). Please note that the C++ compiler will issue a warning for the code above: "Warning[Pe174]: expression has no effect."

5.

If you want to use C code from your C++ application, consider building the C code in a separate (library) project. You can place the C (library) project in the same workspace as your C++ project for fast access. Compared with having the C code in a group in your C++ project, some of the advantages are:

  • Clear distinction between compiler options.
  • Faster application build times (No need to rebuild fixed code).

6.

If your C library is an RTOS, there is an extra challenge with RTOS heap usage, RTOS startup etc. See Technical Note 90383. Often, the RTOS vendor can help with this area by providing C++ examples, documentation and/or code. And again, if the RTOS code is written in C, compile it as C to avoid problems.

Extra information

See the screenshot below and the Example project for IAR Embedded Workbench for ARM.

Workspace "mixing" contains two projects


All product names are trademarks or registered trademarks of their respective owners.

Det här innehållet finns tyvärr inte på svenska.

Vår webbplats finns främst på vårt koncernspråk engelska, förutom det innehåll för investerare som vi är lagstadgade att kommunicera på svenska. Vi rekommenderar att du besöker vår globala webbplats på engelska för att få en bättre upplevelse.

Vi stöder inte längre Internet Explorer. För att få bästa möjliga upplevelse av iar.com rekommenderar vi att du uppgraderar till en modern webbläsare som Chrome eller Edge.