Arduino on Eclipse : Quick and dirty tutorial

As I said, here's a little how-to on how to setup Eclipse to program the Arduino. It is fairly incomplete, so I'm posting it here for interested people to maybe comment and extend it in order to make a complete step-by-step.

So, first : This have been done on Eclipse 3.3.2 (Europa), on Mac OS X.4. It might work as well on any unix, but I can't guarantee anything for Windows platforms.

Configuring Eclipse and AVR Plugin

Eclipse
C Developpement Toolkit
AVR Plugin (Don't hesistate to go for 2.2 beta 1 instead of 2.1 release)
Target Management Project (optionnal)

and the AVR-GCC toolchain compiled and ready to go. Tutorials and binaries exists everywhere for many platforms, I'm not going to double them here.

First, configure the AVR Plugin in the Preferences window. This means setting up folders on where to find the different avr binaries and config files. It usually is :

/usr/local/bin for binaries
/usr/local/etc for avrdude.conf

You'll also need a folder named PartDescriptionFiles which you can get from an installation of AVR Studio. YOu'll have to install it on a Windows machine, anyway. A friend can easily do it for you and give you back the folder.

Creating an "Arduino Lib" project

Create a new C/C++ project and copy the following files into it :

From your Arduino installation, the following files located on /hardware/cores/arduino :

HardwareSerial.h
WProgram.h
wiring.h
WConstants.h
binary.h
pins_arduino.h
wiring_private.h

From any Arduino project, get the core.a file in the applet subdir and add it, to. I renamed it decimilia_core.a to prevent mistaking and allow handling of multiple Arduino boards. Note you can also build everytime, but this may be a bit longer and is mostly useless, except if you're planning to hack into it.

You're not going to even build your project, it's just a library for your Arduino projects.

Your first Arduino project

Create a new C or C++ project with "AVR cross-target executable" as a target. Go to your project's preference and set them as the following :

AVR Target Hardware : For Decimila, Atmel168 with a MCU Clock frequency of 16000000

C++ Build/Settings/AVR C++ Compiler/Optimization : Optimize for size (-Os)
(same for the assembler)
C++ Build/Settings/AVR C++ Compiler/Directories : Add the folder of the previously created ARduino Library to the list of include dirs

On Assembler and Compiler, you might also want to disable debugging symbols

AVR C++ Linker, change the command line pattern to :

${COMMAND} --cref -s -Os ${OUTPUT_FLAG}${OUTPUT_PREFIX}${OUTPUT} ${INPUTS} -lm ${FLAGS}

And the "Command" to avr-gcc instead of avr-g++ (may be already be the default if you're making a C project instead of a C++ one)

It works with default settings, too.

AVR C++ Linker/Objects : Add the core.a file you've imported and maybe renamed from the Arduino library.

Writing code

You must #include "WProgram.h"
No loop() and setup() here, only
int main(void);
You should NEVER return from main(). The easiest way to do this is :

int main(void) {
/* Setup instructions */
    for (;;) {
        /* main loop */
    }
}

You must always add a call to init(); as your first instructions. Not doing this will prevent any time-related functions to work.

Uploading to the Arduino

For a Decimilia, the avrdude command looks like this :

avrdude -F -pm168 -cstk500v1 -P/dev/cu.usbserial-A4001uwp -b19200 -D -Uflash:w:./your_executable_name.hex:i

You can add it as a post-build command

Serial port monitor

If you have Target Management Project installed, just go to the Window menu / Show View / OTher.. And choose Terminal/Terminal. Open it with a serial link and choose the serial port your Arduino is connected to. Notice you'll probably have to disconnect the terminal while uploading.

As the RXTX library is a troublemaker for many people, there is a workaround : Just start a SSH or Telnet (please check your security setup before doing this !!!) server on your computer and open it in the Eclipse terminal. Then, on any unix, just type
cat /dev/your_serial_port/

This tutorial sucks

Yes, a lot of things are still to do :

– Creating a default Arduino project witch will prevent from doing a complete config of each project.
– Getting a deeper integration of avrdude

btw, please especially reports if your programs built within Eclipse are bigger than the ones built with the Arduino env. The modified linker call should prevent this, and in fact makes even smaller executables than Arduino.

PLEASE, do not reports problems or questions about Arduino on Eclipse anywhere else in this forum than on this thread. Arduino/Eclipse is just a geek's hack, and I don't think Arduino people will want to discuss it or search solutions for it. (Owners/Moderators, if you don't think this is needed, just remove it ^_^)

Cool! I'm an Eclipse fan, but still something of a PIC nube, so forgive the questions:

  1. would you still have all the arduino commands available in eclipse? And the automatic includes/whatnot? There might be a java class that does that in the arduino file directory that could be invoked from eclipse if not.

  2. Hows the code completion (ctrl space)

  3. If you install the arduino IDE, can you get all the "toolchains" or whatever from the arduino directory structure without a lot of seperate installs?

  1. would you still have all the arduino commands available in eclipse?

Commands, but not language structure. Arduino software is a preprocessor AND a runtime library. Using Eclipse, you don't have the preprocessor anymore, so forget about void setup() and void loop() and not having to declare your functions, but you'll still have access to all the Arduino functions, like digitalWrite() and such

  1. If you just install the arduino IDE, can you get all the "toolchains" or whatever from the arduino directory structure?

Haven't tried, might work, might not work, it depends ^^ I really suggest you make a clean installation of the avr-gcc toolchain. But you can try using the one provided with your Arduino installation.

btw, about the AVR Eclipse plugin : Really install 2.2 beta instead of 2.1. It rocks. And solves the problem I've had about uploading directly within Eclipse to the board. Now you can go build, print size, then upload, with just one button.

Seems you cannot edit your posts after a certain amount of time, so you'll find the latest version of this tutorial here : Arduino on Eclipse : Quick and dirty tutorial

Nice tutorial.

You should consider writing this up in the Development Tools section of the playground so other people can update and improve it: Arduino Playground - DevelopmentTools.

Also, you should, I think be able to link against the main.cxx file (which has the definition of main()) or a modified version of it, if you'd rather keep setup() and loop() so it's easier to share code with people using the Arduino IDE.

Nice tutorial.

You should consider writing this up in the Development Tools section of the playground so other people can update and improve it: Arduino Playground - DevelopmentTools.

Also, you should, I think be able to link against the main.cxx file (which has the definition of main()) or a modified version of it, if you'd rather keep setup() and loop() so it's easier to share code with people using the Arduino IDE.

Thanks :slight_smile:

I've noticed only a few hours ago that the playground was a wiki... So I added a link to the tutorial on the Eclipse page, and will completely move it to the playground when I'll have time for it (This page goes more in depth than my tut on tools configuration, I need to keep this)

I will add your suggestion of linking main.cxx, too. If the Arduino preprocessor is able to locate already declared functions and no re-declare them, then Eclipse source code can be 100% Arduino compatible... Nice ^^

update : all done ! :slight_smile:

Awesome.

It's here, btw, for anyone who finds this thread: Arduino Playground - Eclipse

Hi there,

I have configured eclipse to play nice with the arduino using the AVR-eclipse plugin per the instructions above, but rather than copying the "core.a" file into my project, I linked the arduino library source code into my project (per the method in the second post of this thread: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1235149012).

In this setup, however, I am forced to exclude main.cxx from my project build, and manually copying the contents of it into a sketch, which stinks.
What I would like to do is get Eclipse to compile main.cxx AFTER my sketches. Anyone know a way to swindle Eclipse into doing this?

Thanks!
colin

You could use a Makefile of your own. Uncheck "Generate Makefiles automatically" in the project options and use the hardware/cores/arduino/Makefile. It may take some tweaking to have it play nicely.

I personally prefer copying the core lib into the project, so that I can modify it. I get rid of main.cxx and use a more generic build process.

Hello, in my opinion the complete dependency tree should be available at eclipse workspace. This is also necessary for modeling some projects with uml tools (papyrus, uml2 for eclipse).

(A) My realization at first level (project names) looks like this ("Vorlage" means "template"):
000_ANSI_C
001_AVR_Vorlage
002_ArduinoCores
010_TwoWire
011_TwoWire_Philips
100_Blinker
110_TwoWire_DigitalPotentiometer
112_TwoWire_SFRRangeFinder

--> 00X means all templates (ANSI-C, AVR includes, arduino cores)
--> 0XX means all libraries (not executable)
--> XXX means all user programs

(B) At second level each project has following subdirs:
-include (for header files)
-src (for *.c and *.cpp files)
-mdl (for models, uml files etc.)
-codegen (for generated code from uml model)
-Release (auto generated after compiling)

(C) Description of projects:
-The project "ANSI_C" provide the simple data types of c language as uml model
-The project "AVR_Vorlage" provide the headers from "/usr/avr/include" and a uml model of mostely used types
-The project "ArduinoCores" comes from arduino files, but is completely re-engineered for better modularity. For example main.h and main.c are included and provide the init(), setup() and loop(). uml model is present for some functions and types
-The project "TwoWire" is re-engineered from arduino "Wire" library
-The project "TwoWire_Philips" is created to make a library for PCA95xx etc. and use the "TwoWire" library, uml model is present
-The project "Blinker" is the first user program and use some object files from 002_ArduinoCores (properties/c++build/settings/avrc++linker/objects)
-The project "TwoWire_DigitalPotentiometer" is a copy from arduino example (user program), but using the object files from libraries (001_, 002_)

BTW: My onset was also the tutorial at playground, but the step: "Install the Part Description Files - Get the folder Partdescriptionfiles from Atmel AVR Studio" was to hard for me and I had searched for a cleaner solution.

Questions and suggestions are welcome, Thomas

I just made some updates to the wiki article on using Eclipse, mostly to do with using it on Windows and using the current tools (which apparently work much better than the tools that were available when this thread was posted). I also compiled a static library that will hopefully allow somebody to get going in Eclipse without having to use the Arduino IDE code:

(see next post for external link)

In response to the question regarding main.cxx, all I had to do to use it was rename it to main.c (otherwise Eclipse ignored it and the main() function was not defined). I linked against the static library, but I imagine that it would be the same for if you compile directly with the cpp files in Arduino. All main.cxx does is define the main function with the structure given in the wiki article:

int main(void)
{
      init();

      setup();
    
      for (;;)
            loop();
        
      return 0;
}

avr-gcc will give you warnings that the init, setup, and loop symbols are being implicitly defined, but the linker will still be able to find the functions so the warnings are ignorable. If you're like me and don't like leaving warnings sitting around, just define the functions as extern at the top of main.c:

extern void init();

extern void setup();

extern void loop();

SP

Here's the link to the precompiled library, if the board will let me post it!

http://nrqm.pbworks.com/Arduino-Core-Library

edit: hah! I'm not a spam-bot after all.

SP

I never used Eclipse and thought I'd try it out with the Arduino. I was reading the instructions and one step says to get core.a file and add it to eclipse. I don't know what it means to add it. What exactly should I be doing with this file?

--Scott

core.a needs to be included in the link pass. I have no idea how to make that happen but, hopefully, Eclipse has some sort of "include in link" option.

The linker on Eclipse can be easily configured, see image:
http://img683.imageshack.us/img683/6125/screenshot25b.png

Since I have more than one arduino and all of them have a different chip, I keep a separate static library for each one, with a more meaningful name than just 'core.a'.

Iv decided to take the route and compile my own static library following the instructions from Arduino Playground - Eclipse

When I tried to compile the first time I was given errors in Tone.cpp.
The includes in Tone.cpp are as follows:

#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <wiring.h>
#include <pins_arduino.h>

Its clear the library was not compiling because it was not looking for the header files in the current directory. Everything compiled fine after I changed the includes to:

#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include "wiring.h"
#include "pins_arduino.h"

Could someone explain to me why the includes are not defined this way in the first plae? Am I doing something wrong?

-Wil

Could someone explain to me why the includes are not defined this way in the first plae? Am I doing something wrong?

Because if in your Makefile/gcc invocation you include . (current directory) to be in your include path, then the compiler will find wiring.h when included as

#include <wiring.h>

This is done by passing -I. to the compiler. If you are not adding . in the include path, then you need to include your headers as

#include "wiring.h"

Hope this answers the question.

Hi,

I'm new to Eclipse. My projects were getting too big for the Arduino IDE and I thought this might be the way to go. I've followed all the instructions on the web and I finally got the simple Hello_Blink project to compile and link!

Now I'm trying to move one of my bigger projects into Eclipse. It will compile, but the linker fails and I'm at a loss as to what might be wrong. The errors all share a common theme, so it is probably my lack of understanding.

I've included the WString.h in my code:
#include <WString.h>

In my Eclipse Libraries folder I have both WString.h and WString.o (I took WString.cpp.o from an Arduino IDE project and renamed it - perhaps this is wrong?).

In the AVR Linker Libraries I have -L to the folder holding the WString.o file.

The linker complains:
undefined reference to `String::String(int)'

I believe it is complaining about this in my code:
#define maxStringLength 1000
String inString = String(maxStringLength);

I should note that in the Elipse editor window it has a question mark for syntax error on any line that starts:
class foo
This is true in both my main.c file and in the WString.h file. The compiler command is avr-g++ and it compiles without error. So maybe Eclipse thinks this is wrong but the compiler handles it fine?

I added -verbose to the XLinker options but that didn't give me any more info.

I'm sure this is some problem in my understanding of the environment, but I am lost on it. Any advice would be appreciated.

Cheers,
Jim

@OrlandoArias

Thank you for your help.

For anyone else trying to build the Arduino library from the source in Eclipse you will need to set the -I flag for the compiler for files main.cpp and tone.cpp which have headers that are not found in the current directory. I tried setting the -I flag for the compiler to the current directory for the global project but the build would still fail.

To do this:
1)Right click on the file.
2)Choose properties
3)Select C/C++ Build -> Settings
4)Under the compiler select Directories
5)Set the directory to where the header can be found, in our case the current directory (Click workspace, select Eclipse project, etc)

@JimSchrempp

Are you not including <WProgram.h>?

To link objects you will just need to add them under AVR Linker -> Objects from the C/C++ Build->Settings menu.
You will then set -I to the folder containing that object.

You should be using the -L flag to set the folder containing the Arduino library you are using and the -l flag to set the name of the actual library (ie for a libary libcore.a you would set the -l flag to core)

This is true in both my main.c file and in the WString.h file.

If your using c++ youll need to change main.c to main.cpp. I was having issues in eclipse with not linking properly because the cpp linker was not finding my main class.

Hope this helps some

@Wil,

Thanks for the tips! Building on your suggestions I renamed my file from WStrings.o to LibWStrings.so. I then included it specifically on the Linker Libraries config. The error went away. I did the same for other libraries and my link now gets much further along!

I'm now getting a single link error in the servo library that says
undefined reference to `map(long, long, long, long, long)'
I'll try to figure that out now.

As to your suggestion on main.c to main.cpp - I see. I have to name the file .cpp for Eclipse to know that the class syntax is legal. I tried that quickly, but Eclipse then says there is no build rule for main.o. I'll look into the Eclipse documentation to find out how to alter build rules.

Thanks again for your help.
Jim

I'm trying to use the Wire library in an eclipse project. So I copied that Wire folder and the utilities subfolder into my eclipse project. Now the funny thing is that twi is built (from the subfolder), but not Wire, I did just #include <Wire.h>. I just get a linker error now, because the object is not built...

why does eclipse not build that library?

Furthermore why are you suggesting to just copy precompiled objects from arduino? Thats just a hack and not a solution... I mean eclipse/gcc should be able to compile everything just from the c/h files.