Problem with include files in platformio with lvgl

Hi there,

So i have a project with lvgl on platformio and whenever i want to add a library to one of my header files, it doesn’t compile and outputs thousands of lines of error codes. They mostly look like this:

In file included from src/main.cpp:121:
src/ui.h:10:1: note: 'extern "C"' linkage started here
 extern "C" {

I tried so many things now and I got kinda tired of trying…

I think the line before the error lines you posted might also be required for these errors. I’ve not com e across the error myself, but a quick DuckDuckGo seems to imply that there’s a construct like:

extern "C" {
    // C++ "stuff" like templates and such like used here
    // but this is the C stuff here, not C++.

The problem could be that there is C++ specific stuff between the braces for the extern "C" and it should only be C stuff there, not C++.

Can you post your platformio.ini and a small source file that recreates the problem?



Hmm might be, since i include c++ libraries in the braces… duh… didn’t even think about that. well to be fair I’m not too familiar with c++ yet…

Anyway that’s my platformio.ini:

platform = espressif32
board = um_tinys3
framework = arduino
upload_speed = 921600
monitor_speed = 115200
board_build.partitions = max_app_8MB.csv
;board_build.partitions = no_ota.csv
build_flags = 
	-I src/ui/
	-I src/
lib_deps = 

and for the source file i don’t have anything “small” and couldn’t get something up that put out the same error…

How am I supposed to include that stuff then?

Ok so i tried the obvious and made the #include outside of the extern “c” braces… and it worked… kind of at least.
Seems like if i include the file in lets say “filex.h” which is included in the extern “c” braces in “filey.h” it also takes it as c code, hence can’t compile it and puts an error out… makes sence to me… but how am i supposed to include libraries if there’s a huge spider web with back and forth linkages through the whole lvgl library?
I guess I’ll just spend a few hours with trial and error…

It sounds like you have found the problem. The fix will be harder I’m afraid.

Basically, In plain C, if you write a function called “Xxx(int x)” then the linker will look for a module containing the function called “Xxx”, regardless of the number and type of parameters the function takes.

In C++, however, you can overload functions so that they take different parameter types etc. In this case, the function name will be “mangled”, so “Xxx(int x)” and “Xxx(long y)” will have different names. It’s usually based on the parameter types. The linker will look for the module containing the appropriate function, with the correct mangled name, to suit the parameters passed in the actual function call.

If you have code written in C, which you need to use in a C++ application, you need to define the calling convention as extern "C" {...} so that the compiler doesn’t mangle the names, and so that C++ can call the plain C functions – which might be in a library that you don;t have source for.

So, your C++ application needs to #include all it’s own required C++ headers and C++ library headers as if the plain C stuff wasn’t being used.

It then also needs to do something like:

#ifdef __cplusplus
extern "C" {

// All the required C stuff here. This can be function declarations, function definitions, variables etc

#ifdef __cplusplus

What you must avoid is having anything related to C++ within the extern "C" braces, otherwise, you will get the errors that are plaguing you.

lvgl/lvgl claimes to be Written in C and compatible with C++ so I suspect you need something like this:

#ifdef __cplusplus
extern "C" {

#include <lvgl.h>

#ifdef __cplusplus

But I don’t use lvgl, so I might be wrong here.

Oh, there’s a C++ wrapper for lvgl at GitHub - vpaeder/lvglpp: A C++ wrapper for LVGL whihc might be useful?