Upload C++ To Arduino

I'm currently working on a project in C++ which makes use of the RC522 RFID controller along with an LCD display. I'm coding this in C++. My question is, how do I take my C++ code and upload that to the Arduino? My board is the Elegoo Mega.

Avoid exceptions. Use the heap sparingly. Upload when you’re ready to test.

1 Like

You might start by spending a few minutes looking at ANY basic Arduino tutorial...

1 Like

Once you have your C++ code compiling and converted to a .hex file, upload it to your Mega with avrdude, using a command similar to the one the Arduino IDE produces.

avrdude  -patmega2560 -cwiring -PCOM3 -b115200 -D -Uflash:w:/tmp/myfile.hex:i

Or use a device programmer connected to the ISP pins and whatever software it uses (Microchip Studio, MPLABX, etc.)

You could also use CMake to build and upload your C++ project: GitHub - tttapa/Arduino-AVR-CMake: Compile Arduino AVR programs using CMake.

This may be easier than restructuring your code to be compatible with the Arduino IDE, especially if you already have a build system.

Hi @tadams79. The Arduino programming language is essentially superset of C++. This means that it should be possible to use any valid C++ code in the .ino file of an Arduino sketch. So you can simply create a new sketch in any of the Arduino development tools (e.g., Arduino IDE, Arduino Cloud Editor, Arduino CLI), paste your C++ code into the sketch, then upload the sketch to the board as usual.

If you want the code to be compiled as pure C++, without the minimal preprocessing that is done on the .ino files of a sketch, then add a .cpp file to the sketch folder and put your C++ code in that file. It is mandatory for the sketch to contain a .ino file, but the file can be empty if you like.

If this approach of using the standard Arduino development tools sounds interesting to you, I recommend just giving it a try. If you have any questions or problems then you can add a detailed description in a reply here on the forum thread and we'll provide assistance.

That is incorrect. The language, as it relates to C++, is a subset of C++ and a user friendly library. The lack of exceptions and the necessity to avoid the heap clearly makes it a subset.

And a preprocessor that always manages to mess up my templates and user-defined types by silently inserting function declarations :smile:

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.