I am trying to use C++ on my Arduino uno board with the Arduino program and other programs, but have found myself stuck. I've looked into this online but have found no specific instructions on how to do so. I understand that the Arduino program uses C/C++ but I just want to use C++. I have also read a topic on the forums about removing the preprocessor so that you would be able to use C++ only. All of this information I have found I feel is wrong, because I am new to coding, and I am hoping that someone with a better understanding than I could help me.
The Arduino IDE uses C++ but of course the physical environment is limited so not all C/C++ features can be used and the Arduino environment has helper functions to enable you to use the hardware easily and the Serial monitor for input/output.
Using the Arduino to learn C++ will be difficult as all general C/C++ text books and tutorials will expect a very different environment.
Having said that, if you write your own main() function the IDE will compile it and other functions and not compile its own hidden main() and init() functions into the code.
What is it that you want to use the Arduino for ?
I was planning to use the Arduino to learn the basics of coding and eventually learn C++ on my own. I'm not sure what everyone means when they say that the arduino program uses c/c++ when the commands are completely different.
I'm not sure what everyone means when they say that the arduino program uses c/c++ when the commands are completely different.
The commands are not completely different to standard C/C++ but there are Arduino explicit extensions to control the Arduino hardware.
Almost without exception the whole of the C/C++ language is available on the Arduino but any program will need to interact with the user an/or external hardware hence the Arduino extensions. The Arduino reference page details these extensions as well as some standard C/C++ functions but the list is not exhaustive, nor could it be.
One thing that the Arduino IDE does is to hide some the the mechanics of C/C++ by not requiring there to be a main() function written by the user. Rather it is added during the compilation process but as I said in my previous reply you can write your own but you will then also be responsible for initialising the Arduino hardware environment which the Arduino compilation process also does behind the scenes.
Well even if the Arduino commands are not completely different from the commands from C/C++, is there a third party program that is supported by arduino that i could use without the different commands?
JohnIguana:
Well even if the Arduino commands are not completely different from the commands from C/C++, is there a third party program that is supported by arduino that i could use without the different commands?
You need a PC to write a program for an Arduino so why not just learn C++ on your PC and leave the Arduino for later?
...R
is there a third party program that is supported by arduino that i could use without the different commands?
The biggest bar to this is input and output to/from the Arduino. As Robin suggests you would be better to focus your efforts to learn C/C++ on the PC unless/until you need the functionality that the Arduino hardware can provide.
There is not much difference between C and C++. C++ is a super-set of C and derived from it; the language basics are the same (and hence C/C++) but C++ added the object oriented 'stuff'. Variable types, control structures, functions, it's all the same in C and C++.
If you really want to learn C/C++, learn it on a PC (as Robin indicated). It teaches you how to properly construct a program. No Arduino Builder that takes a little bit of work out of your hands but sometimes drops a stitch and you have no idea what is wrong.
If you use Linux (and the same probably applies to the Mac), it comes with C and C++ compilers. For Windows, you can download a free edition of Visual Studio; it used to be called Express, I think it's now 'Community Edition'); just check if it supports C++, I think it does. There are plenty other free compilers available for Windows.
Next when you start using Arduino, you have a solid foundation but there are minor differences. There ain't things as reading keyboard and writing to screen (as the Arduino does not have them). And there are a few libraries added so you can read pins and control outputs and the likes.
One of the reasons why I started using Arduino was that I had never done C++; C yes, C# yes, several scripting languages and assembly yes, but never C++. Also I still don't use the OOP part much, I do have a better understanding of C++ than I had before.
In this context by "arduino" you must mean your Arduino Uno. The compiler used by the Arduino IDE for the Uno is avr-gcc. So if you want to program your Uno with C++ but don't want any of the Arduino libraries then just use avr-gcc directly. Or you could use the Arduino IDE but not use any of the Arduino libraries.
But you need to understand that Arduino sketches are C++. There is no Arduino language, only some helpful libraries, mostly in C++. If you start writing programs with the Arduino IDE I guarantee you will learn C++, even if you take advantage of the many useful libraries available to you. You're always welcome to open up the library source files and learn from them instead of pretending they don't exist.
As has been mentioned, the main
function is hidden away in the Arduino core library but you are welcome to override it by defining your own.
You may notice that Arduino sketches have the extension .ino
, which is not a standard extension used for C++ files. The reason for this is that there is a minor amount of preprocessing done on .ino
files before they are renamed with the .cpp
extension and compiled with the C++ compiler. The preprocessing is:
- Concatenate all
.ino
files in the sketch, starting with the file that matches the folder name, then the rest in alphabetical order. - Add the line:
This brings in the Arduino core library functions.#include <Arduino.h>
- Generate function prototypes for any functions that don't already have prototypes.
If you don't want prototypes to be generated then you just need to declare them yourself. - Add
#line
directives so that compiler output will match the sketch.
So valid C++ is always fine in a sketch but the Arduino IDE does allow you to skip a couple minor aspects of C++ for the sake of being beginner friendly
Sketch preprocessing is only done on .ino
files. You can also use .h
, .cpp
, .c
, .S
, etc. files in your sketch and these will not be modified in any way.
How do I only use avr-gcc?
Have you looked into Atmel Studio?
Yes I actually have but unfortunately I have no idea how to use it and I cant find any tutorials on how to use it.
"How do I only use avr-gcc?"
In the IDE, open File:Preferences and turn on Verbose outputs.
Compile a simple sketch.
You will see all the commands that are being executed, some of which are the avr-gcc commands.
Copy & save those, experiment with running them with the avr-gcc environment.
I am stuck. . .
I can’t . . .
I am not sure. . .
I won’t. . .
I shan’t. . .
I am unable to. . .
Is there something simpler. . .
JohnIguana:
How do I only use avr-gcc?
If you want to learn C++ on a PC, you can use other free C++ IDE's like Visual Studio (for Microsoft), Genie, and Eclipse. I use Visual Studio 2015 Community and I love it.
JohnIguana:
How do I only use avr-gcc?
What problem are you trying to solve?