External C library includes "mbed.h" and fails to compile...options

Hi,
I am using LVGL - Light and Versatile Graphics Library in my Mbed-os program and it works great. I have implemented a LVGL GC9A01 driver for a display I have and for it to work it has to be a C++ (.cpp) file, even though it contains no C++ specifics. This is because it needs to include the “mbed.h” file to define the pins for DigitalOutput.
I created a pull request to pull my new driver into the master and first comment was to rename the file back to a .c from a .cpp file. This of course means that it no longer will work in Mbed-os. To be fair it should be a .c file like all the other drivers.

To my question…is there anyway to include mbed.h into a C file?
Can I force the GCC compiler to compile a specific C file as C++?
Right now my fix is to add the library to the Mbed project and then rename GC9A01.c to GC9A01.cpp and build. Could this be best/simplest solution?
Other suggestions are appreciated.

Thanks
Mark

Hello Mark,

In such case it should be sufficient to include only PinNames.h rather than mbed.h. It’s a C header file where C++ name mangling is already prevented by using

#ifdef __cplusplus
extern "C" {
#endif

/* ... */

#ifdef __cplusplus
}
#endif

Best regards, Zoltan

1 Like