Search code examples
c++cinteropaccelerometermotion-detection

Mixing C and C++ on an embedded system


So I'm having the following problem: I have an MEMS board that runs on FreeRTOS and includes gyro, accelerometer and magnetometer.

I cannot change any existing code (All in C). Now I have a basic motion detection library written in C++ and I extended this library with some functions (All in C++).

I thought I can just use a C++ compiler and compile everything but I'm getting hundreds of errors.

I found some solution how to use C functions inside C++ but I don't know how to use C++ functions (or libraries) inside C. Is there a feasible way? Can I somehow wrap all my C++ code in an easy way?

I'm using Keil uvision to compile the code for my embedded system in case that is important.


Solution

  • There are a few differences between C and C++ that may make a compiler stumble over some of the code. See for example the wikipedia page on this topic.

    I'd suggest that you split your project into two projects, one being the RTOS and application in C, the other being the motion detection library. Then you have to write a C wrapper around your C++ library API. Here's a good SO post on writing a C wrapper for C++ code.

    Then you would have to link your RTOS + application project to your library, which you compiled in your other uVision project.