Hello all, because the "official" Eclipse tutorial has some holes, and is missing some directories and flags, here's my tutorial (based on that one) of how you replicate all the functions of the Arduino IDE in Eclipse. The forum kind of mangles the formatting because I'm too lazy to put in all the markup, but you get the idea. There are some more aggressive compiler and linker flags that you can use, but I tried to closely replicate the flags the Arduino IDE uses here.
This guide is aimed at people who have used Eclipse before and know at least a tiny bit about how a C/C++ program is compiled.
My system is a Debian Linux system, with Eclipse (Helios/3.6) and an Arduino Uno. Most instructions will be the same, but your paths may vary... You need the CDT plugin for Eclipse, you can download the prepackaged version or you can add the CDT to your current install through Eclipse's update manager.
-
Install System Prerequisites
Get the AVR programmer and toolchain for cross-compilation and debugging. You probably already have some of this from the Arduino IDE prerequisites:
apt-get install avrdude binutils-avr gcc-avr avr-libc gdb-avr -
Install Eclipse Plugins
AVR tools for programming, compiling, etc:
AVR Eclipse Update Site
Get the plugin: CDT Optional Features->AVR Eclipse Plugin and install it and any dependencies.
RXTX library for the serial monitor:
http://rxtx.qbang.org/eclipse/
Get the latest runtime (currently RXTX 2.1-7r4->RXTX End-User Runtime) and install it and any dependencies.
GUI for the serial monitor:
It's in the default Helios update site.
Get Mobile and Device Development->Target Management Terminal and install it and any dependencies.
- Creating a Template Project
In almost all embedded development, especially on constricted architectures like AVR, you will find yourself knee-deep in cryptic compiler flags. The Arduino IDE is very good at doing this in the background, but you can see what's going on if you get the sources. So we'll set this up once and just copy a "base" project for all new projects.
First of all, create a new workspace just for your Arduino development so you can set workspace global defaults.
Create a new C/C++ project. Let's call it "Template". The project type should be AVR Cross Target Application->Empty Project, and the toolchain should be AVR-GCC Toolchain. Hit Next twice.
For the Uno the MCU type is ATMega328p, the MCU frequency is 16000000. Your hardware may vary. Hit Finish to create the project.
-
Importing the Core Arduino Library
All projects using the Arduino environment need the precompiled Arduino library, if you aren't compiling together with your own custom core. Open an example project in the Arduino IDE and "verify" it. Now in your Eclipse project, import the library from the filesystem, which is probably in /tmp/buildXXXXXXXXXXXXXXXXXXXXX.tmp/core.a. Put it in a new subfolder "arduino" within the project, and rename it libcore.a. -
Compiler Settings
Open your project properties, edit from the defaults and remember to Apply at the end:
Under C/C++ Build->Settings:
Additional Tools in Toolchain
Check Generate HEX file for FLASH memory
Check Print Size
Check AVRDude if you want to flash automatically every time you build
AVR Compiler
Directories
Add the directory of the Arduino header files, located in the Arduino IDE directory /hardware/arduino/cores/arduino . Alternately, you can copy all these files into your template project, perhaps in the /arduino where the core library is, and point to that location in the Workspace instead.
Add the directory of the Arduino libraries, located in the Arduino IDE directory /libraries . Again, you can copy these directly into your project if desired.
Symbols
Unnecessary, but you can add a define of ARDUINO=22 (or whatever your version is)
Optimization
Set Optimization Level to Size Optimizations (-Os) . This tells the compiler to turn on optimizations that generate smaller code sizes.
Uncheck Pack Structs, this aligns structs in the binary in a way that may be longer and the core is not compiled with it on.
Uncheck Short Enums, this allocates enums only enough space to cover the possible range, but since this is not how the rest of the Arduino core is compiled you should turn it off.
Other Optimization Flags: -ffunction-sections -fdata-sections
These flags separate functions and data into their own sections, which allows the linker to do certain optimizations.
Miscellaneous
Other flags: --pedantic -c
The -c flag tells it to just compile, not link yet. The –pedantic causes even the most inane thing to generate a warning, it can be very helpful if you are tracing an obscure issue but can generally be ignored.
AVR C++ Compiler
Make exactly the same changes as the previous section.
AVR C++ Linker
Command: avr-gcc
General
Other Arguments: -Wl,--gc-sections
This flag allows linker optimization using the results of -ffunction-sections and -fdata-sections from compilation.
Libraries
Libraries: add c, m, printf_flt, scanf_flt, and core.
These are the C core library, the math library, printf and scanf libraries with float support (the default versions don't have it), and the Arduino core library. Not all are necessary for all projects, but they are frequently needed and it's easy to add or remove them if you want.
Libraries Path: click add, select Workspace... and select the folder where you saved the libcore.a. It will end up like "${workspace_loc:/${ProjName}/arduino}". -
Template Code Setup
The Arduino IDE does a little bit of source mangling before compilation which we need to replicate. Add this code to a header file like cppsupport.h and #include it in your main.cpp source file, or have it inline. It provides some C++ style wrappers for malloc and the guards protect against accessing invalid memory locations. There is also a handler to catch calls to purely virtual functions.
#include <stdlib.h>
extern "C" void __cxa_pure_virtual()
{
cli();
for (;;);
}
extension typedef int __guard attribute((mode (DI)));
void * operator new(size_t size)
{
return malloc(size);
}
void operator delete(void * ptr)
{
free(ptr);
}
void * operator new[](size_t size)
{
return malloc(size);
}
void operator delete[](void * ptr)
{
if (ptr)
free(ptr);
}
int __cxa_guard_acquire(__guard g) {return !(char *)(g);}
void __cxa_guard_release (__guard g) {(char *)g = 1;}
void __cxa_guard_abort (__guard *) {}
Now create or edit main.cpp and paste the following skeleton code - you don't see it, but the IDE rearranges your sketch to look like this before compilation:
// The Arduino core
#include <Wprogram.h>
// Uncomment if you need it for C++ style new/delete and pointer protection
//#include "cppsupport.h"
void setup()
{
}
void loop()
{
}
int main(void)
{
init();
setup();
for (;
loop();
return 0;
}
Technically you don't need to use setup and loop anymore... Ok, now put in some simple blink code or something into setup() and loop(), and build it. In the build console you'll see all the steps and eventually a printout of the code size. For the Blink example program, you should come in just under 1000 bytes. If you are significantly higher, you probably missed a flag in the settings.
- Flashing to Arduino
Hook up your Arduino to your computer and figure out the address of the serial port. For the Uno, it's always /dev/ttyACM0, or 1,2,3... if you have multiple devices or unplug and plug it in again quickly. Go into Window->Preferences->AVR->AVRDude and Add... a programmer configuration. Name it Arduino, select Arduino in the Programmer Hardware (-c) list, and set Override default port (-P) to your serial port. Hit OK and Apply.
Back in Project->Properties->AVR->AVRDude, select the Programmer Configuration you just created and Apply.
Now build the blink template project, and AVR->Upload Project to Target Device. Make sure nothing else is using that port, like the Arduino IDE serial monitor. Hooray, you have just flashed your Arduino from Eclipse.
-
Serial Monitor
The last piece is a serial monitor inside Eclipse. To access it, open Window->Show view...->Other and select Terminal->Terminal, it will probably open where your compiler console is. Click the Settings icon; set Connection Type to Serial and supply the appropriate Port and Baud Rate, the rest should be fine. Click OK and hopefully the terminal should connect, try some test code with Serial.print. Be sure to disconnect before flashing because it will interfere. -
Cleanup
Set your main.cpp back to the skeleton, run a clean (but not a build) in your Template project to flush out the binaries, and save it. Now whenever you need a new Arduino project, just copy the Template project so you don't have to change all the settings. Enjoy!