9

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?

Update 1

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.

Update 2

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.

  • Thus question is way beyond my knowledge, but some things you'd have to consider is the include of the Arduino.h file. Incorporating the bootloader; and whatever else. I'll be following this question :) – Madivad May 2 '14 at 15:17
  • I'd suggest you take a look at Arduino-CMake (github.com/queezythegreat/arduino-cmake). – jfpoilpret May 2 '14 at 17:25
  • inotool.org – jippie May 2 '14 at 21:09
  • 1
    You can't write a "program" that is considered valid by both the IDE and G++ directly unless you're willing to make a number of sacrifices. Deal with Ino. – Ignacio Vazquez-Abrams May 17 '14 at 1:22
  • 1
    Found my answer here, in the comments of the attached makefile. – hoosierEE May 17 '14 at 13:38
0

By gcc/g++ I think you're referring specifically to avr-gcc/avr-g++. Your arduino code probably won't be considered valid C/C++ code because gcc will try to compile with your PC as the target system. Many of the Macros in "WProgram.h" will refer to inaccessible memory on your target system.

Providing you include the header file "WProgram.h" and you build your code with the Arduino Makefile above (which uses avr-g++ not g++), your cpp files should compile and link correctly. Likewise, you will be able to open your Arduino code within the IDE and Verify your code there. The IDE puts in the "WProgram.h" file for you, so technically it would be included twice. However, the include guards will prevent any duplicate code from being compiled.

1

Generally you'll need to state the setup() and loop() functions, they are just wrappers of the IDE:

void setup() {}
void loop() {}

int main() {
  setup();
  while (1)
    loop();

  return 0; // never reached
}

This works both in the IDE and on the command line. As you specify the Board and SerialPort in the IDE, you'll need also to specify both in the Makefile. So you'll end up with two configurations.

Many people leave the IDE because they prefer use different editors or have more configuration options by setting the compiler and linker switches.

To make it really easy, you can use Arduino Makefile. Today starred with 347.

Here an example Makefile:

# try: make help
#
include /usr/share/arduino/Arduino.mk
ARDUINO_DIR = /usr/share/arduino
# list boards with: make show_boards
BOARD_TAG = promicro16
ARDUINO_LIBS = EEPROM 
# MONITOR_CMD = picocom -b 9600
MONITOR_CMD = moni -config ACM0-9600.cfg
ARDUINO_PORT = /dev/serial/by-id/usb-Arduino_LLC_Arduino_Leonardo-if00
# ARDUINO_PORT = /dev/ttyACM0
# ARDUINO_PORT = /dev/ttyUSB0
OPTIMIZATION_FLAGS = -Os -fexpensive-optimizations -fstrength-reduce
#

The '*.ino' files don't need to be changed and the Makefile file is to be placed in the same directory.

Meanwhile I prefer the Makefile way, but I still use the IDE for small projects.

1

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++.

See: How to avoid the quirks of the IDE sketch file pre-preprocessing.

Disregarding for the moment what the Makefile will look like, the simple answer to your question, as discussed in the above link, is to put everything into .cpp and .h tabs in the IDE, and leave the main "sketch" (.ino file) blank. That will still compile under the IDE, and will also therefore be normal C++.

Make sure you start your .cpp files with:

#include <Arduino.h>

If you use libraries (eg, SPI) you must include them in the main sketch file, which triggers the IDE to copy them into the temporary project build file. The Makefile won't care about that, as you will make sure your Makefile includes all the necessary library files.

Also see my answer here: Classes and objects: how many and which file types do I actually need to use them?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.