Use C instead of C++?

I’m not sure if I’m missing something, but is it required to write my Code in C++?
When using pure C, I’ll get a few compiler errors/warnings that are C++ specific, so is C++ the only way to make proper use of platformio?

I use Vscode with platformio on a Mac

Depends on the framework. If you e.g. use Arduino, which implements all the crucial in stuff in C++ classes, and you self-restrict yourself to only writing .c files and not a single line of C++ code in a .cpp file, then you can’t even access the Arduino APIs…

“but is it required to write my Code in C++?”

You can write C code in a .cpp file but if you call SDK C-functions you may have to fix some compiler warnings because C++ is sometimes fussy about parameter types. C++ includes C but C does not include C++.

Other than that, I see no reason to avoid basic C++ because you can use all the system code and libraries.

since I’m using a atmega168p microcontroller, the framework is set to arduino. But I just need the various <avr/*.h> header files in my project and don’t need any of the arduino files.

Arduino wants you to at least write setup() and loop() in C++ (with C++ name mangling ;)) So the most minimal you can do is a src/main.cpp with

#include <Arduino.h>

extern "C" void setup_c();
extern "C" void loop_c();

void setup() { setup_c(); }
void loop()  { loop_c(); }

And then implement setup_c() and loop_c() in your C files.

And yes, while you may be able to overwrite the main() function directly, that’s not how Arduino is supposed to work… If you think like that, you may directly go baremetal and throw out Arduino, by deleting the framework = arduino line. Then you’re completely ‘alone’ with just the AVR compiler includes available to you.

I think you may be looking for just platform-atmelavr/examples/native-blink at develop · platformio/platform-atmelavr · GitHub ?

i already changed the .cpp ending to .c and thought the project would be handeled as C.

The main reason to avoid C++ is, that it is required to use C in our project, so if I want to pass this course I have to use plain C :smiley:

I think the best way is to use platformio, at least in this case, as wrapper for easy imports of dependencies (and probably flashing) and rely for the rest on a simple Makefile and the standard gcc compiler

oh, i thought that it is a must to specify the Framework value. Thank you, i deleted this specific line in the .ini file. Thank you

1 Like