I'd like to write my sketches so that I can either build/upload them using the Arduino IDE, or optionally using GCC and a makefile.
I know about including the function declarations at the top, but is there anything else to do in order for my sketch to be considered valid C++ by my compiler?
Understanding what the Arduino IDE does to .ino and .pde files is fine, but extraneous to my question, so this is not a duplicate. What I want to know is "how do I write a program such that it is considered valid both by the Arduino IDE and g++.
The official(?) makefile available here explains what to do if using the makefile instead of the IDE:
# The Arduino environment does preliminary processing on a sketch before # compiling it. If you're using this makefile instead, you'll need to do # a few things differently: # # - Give your program's file a .cpp extension (e.g. foo.cpp). # # - Put this line at top of your code: #include <WProgram.h> # # - Write prototypes for all your functions (or define them before you # call them). A prototype declares the types of parameters a # function will take and what type of value it will return. This # means that you can have a call to a function before the definition # of the function. A function prototype looks like the first line of # the function, with a semi-colon at the end. For example: # int digitalRead(int pin);
...but this doesn't explain how use both the IDE and a makefile.
I recently found PlatformIO which doesn't answer this question directly, but does automate a lot of the process (generates Scons files for you) and so far I prefer the workflow over both the Arduino IDE and the source+makefile approach. Good support from the authors as well.