Using libraries with Eclipse

Hi all,

I have been working and scouring the internet for the past couple of days trying to get my Eclipse project working with the Arduino libraries. I got my Eclipse environment set up using the playground/Code/Eclipse instructions and got the Blink program to run like many other people have. Now I'm trying to make a servo test program using the Servo library.

None of the posts I've found really cut straight to how to use the Arduino libraries in new projects. I've tried adding the Servo.cpp and Servo.h files to the project, adding the Servo library path to the AVR C++ Linker, and even tried creating a new static library for the Servo library. None of these things has worked. So I really have no clue what to do at this point.

Back to the point - how do I use the Arduino libraries with a new Eclipse project?

I appreciate the help greatly.

Thanks,
k

Set up your project and add main.cpp and other source files.

Then add the Servo library by linking the Servo.cpp file into your project:
(right click) -> New -> File -> Advanced -> Link to file in the file system -> Browse -> ".../libraries/Servo.cpp"

Or, you can import Servo.cpp into your project and keep a local, duplicate copy of Servo.cpp in your Eclipse workspace.

Now tell Eclipse where to find Servo.h:
(right click) - > Properties -> C/C++ Build -> Settings -> AVR C++ Compiler -> Directories -> add Include path ".../libraries/Servo"

You can also import Servo.h into your project and keep a local, duplicate copy, just as can for Servo.cpp. If you do this, then add an Include path to your project workspace instead of the arduino libraries folder.

Jim

I did like you said, linking the Servo.cpp and adding the directory for the Servo.h files. I'm getting an error on the build though.

./Servo.o: In function `Servo::read()':
Servo.cpp:(.text+0x238): undefined reference to `map(long, long, long, long, long)'
./Servo.o: In function `Servo::write(int)':
Servo.cpp:(.text+0x2fc): undefined reference to `map(long, long, long, long, long)'

I was getting this error before when I would include the Servo.cpp and Servo.h files in my project. Google searching the error hasn't really turned up anything useful.

k

Can you build the following?:

#include <WProgram.h>
#include <Servo.h>

Servo servo;
int pin1 = 1;

void setup() {
      servo.attach( pin1 );
      servo.read();
}

void loop(){
}

int main(void)
{
      init();
      setup();  
      for (;;)
            loop();
      return 0;
}

This builds without error on my Eclipse/AVR installation.

FWIW, I am using a core.a library that is built in Eclipse, but that shouldn't make a difference.

Jim

Here's the console output from the build:

**** Build of configuration Release for project servo-test ****

make all 
Building file: C:/Program Files/arduino-0018/libraries/Servo/Servo.cpp
Invoking: AVR C++ Compiler
avr-g++ -I"C:\Program Files\arduino-0018\hardware\arduino\cores\arduino" -I"C:\Program Files\arduino-0018\libraries\Servo" -Wall -Os -fpack-struct -fshort-enums -ffunction-sections -fdata-sections -funsigned-char -funsigned-bitfields -fno-exceptions -mmcu=atmega328p -DF_CPU=16000000UL -MMD -MP -MF"Servo.d" -MT"Servo.d" -c -o"Servo.o" "C:/Program Files/arduino-0018/libraries/Servo/Servo.cpp"
Finished building: C:/Program Files/arduino-0018/libraries/Servo/Servo.cpp
 
Building file: ../main.cpp
Invoking: AVR C++ Compiler
avr-g++ -I"C:\Program Files\arduino-0018\hardware\arduino\cores\arduino" -I"C:\Program Files\arduino-0018\libraries\Servo" -Wall -Os -fpack-struct -fshort-enums -ffunction-sections -fdata-sections -funsigned-char -funsigned-bitfields -fno-exceptions -mmcu=atmega328p -DF_CPU=16000000UL -MMD -MP -MF"main.d" -MT"main.d" -c -o"main.o" "../main.cpp"
Finished building: ../main.cpp
 
Building target: servo-test.elf
Invoking: AVR C++ Linker
avr-g++ --cref -s -Os -o"servo-test.elf"  ./Servo.o ./main.o   -lcore328P -lm -Wl,-Map,servo-test.map,--cref -L"C:\Documents and Settings\jim-g\eclipse\workspace\core328P\Release" -mmcu=atmega328p
Finished building target: servo-test.elf
 
Invoking: AVR Create Extended Listing
avr-objdump -h -S servo-test.elf  >"servo-test.lss"
Finished building: servo-test.lss
 
Create Flash image (ihex format)
avr-objcopy -R .eeprom -O ihex servo-test.elf  "servo-test.hex"
Finished building: servo-test.hex
 
Invoking: Print Size
avr-size --format=avr --mcu=atmega328p servo-test.elf
AVR Memory Usage
----------------
Device: atmega328p

Program:    3550 bytes (10.8% Full)
(.text + .data + .bootloader)

Data:         56 bytes (2.7% Full)
(.data + .bss + .noinit)


Finished building: sizedummy

Still get the undefined reference to map error and am using the compiled static library from Arduino Playground - Eclipse. In this case, mine is called libArduinoCore.a.

**** Build of configuration Release for project ServoTest ****

make all 
Building file: ../main.cpp
Invoking: AVR C++ Compiler
avr-g++ -I/home/badger/arduino-0018/hardware/arduino/cores/arduino -I/home/badger/arduino-0018/libraries/Servo -Wall -Os -fpack-struct -fshort-enums -funsigned-char -funsigned-bitfields -fno-exceptions -mmcu=atmega168 -DF_CPU=16000000UL -MMD -MP -MF"main.d" -MT"main.d" -c -o"main.o" "../main.cpp"
./Servo.o: In function `Servo::read()':
Servo.cpp:(.text+0x238): undefined reference to `map(long, long, long, long, long)'
./Servo.o: In function `Servo::write(int)':
Servo.cpp:(.text+0x2fc): undefined reference to `map(long, long, long, long, long)'
Finished building: ../main.cpp
 
Building target: ServoTest.elf
Invoking: AVR C++ Linker
avr-gcc --cref -s -Os -o"ServoTest.elf"  ./Servo.o ./main.o   -lArduinoCore -lm -Wl,-Map,ServoTest.map,--cref -L/home/badger/workspace/ArduinoCore/Release -mmcu=atmega168
make: *** [ServoTest.elf] Error 1

I don't see the command line for compiling Servo.cpp in your output.

Jim

Hmm... neither do I now that you mention it. I'm pretty new so I didn't realize I needed to compile Servo.cpp as well. Do I need to compile Servo.cpp and then add that to my project?

If you add Servo.cpp to your Eclipse project, either by linking it or by importing it, then it should get compiled automagically.

The small test project I made has two files: main.cpp and Servo.cpp

Both get compiled when doing a build.

Jim

Ok, yeah, now I see they are both being compiled in yours. There must be something wrong with my configuration then if my Servo.cpp isn't compiling.

Ok, not sure what I did, but there is some progress, although with the same error. Servo.cpp is building now, though.

**** Build of configuration Release for project ServoTest ****

make all 
Building file: /home/badger/arduino-0018/libraries/Servo/Servo.cpp
Invoking: AVR C++ Compiler
avr-g++ -I/home/badger/arduino-0018/hardware/arduino/cores/arduino -I/home/badger/arduino-0018/libraries/Servo -Wall -Os -fpack-struct -fshort-enums -funsigned-char -funsigned-bitfields -fno-exceptions -mmcu=atmega168 -DF_CPU=16000000UL -MMD -MP -MF"Servo.d" -MT"Servo.d" -c -o"Servo.o" "/home/badger/arduino-0018/libraries/Servo/Servo.cpp"
Finished building: /home/badger/arduino-0018/libraries/Servo/Servo.cpp
./Servo.o: In function `Servo::read()':
Servo.cpp:(.text+0x238): undefined reference to `map(long, long, long, long, long)'
./Servo.o: In function `Servo::write(int)':
Servo.cpp:(.text+0x2fc): undefined reference to `map(long, long, long, long, long)'
 
Building file: ../main.cpp
Invoking: AVR C++ Compiler
avr-g++ -I/home/badger/arduino-0018/hardware/arduino/cores/arduino -I/home/badger/arduino-0018/libraries/Servo -Wall -Os -fpack-struct -fshort-enums -funsigned-char -funsigned-bitfields -fno-exceptions -mmcu=atmega168 -DF_CPU=16000000UL -MMD -MP -MF"main.d" -MT"main.d" -c -o"main.o" "../main.cpp"
make: *** [ServoTest.elf] Error 1
Finished building: ../main.cpp
 
Building target: ServoTest.elf
Invoking: AVR C++ Linker
avr-gcc --cref -s -Os -o"ServoTest.elf"  ./Servo.o ./main.o   -lArduinoCore -lm -Wl,-Map,ServoTest.map,--cref -L/home/badger/workspace/ArduinoCore/Release -mmcu=atmega168

It seems that I can compile both files individually, but can't successfully compile the project.

I'm sorry that I am not helping very much. I can't see the cause of the problem.

I noticed that my example, which does build, has a ATmega328P as a target, while yours is set for a different processor. Shouldn't matter, but ?

I also notice that the map() function it is complaining about is declared in WProgram.h. It looks like you have its directory in your Includes, so I don't know why it is not finding it.

Jim

Oh, you have been very helpful. I don't think I would have noticed Servo.cpp wasn't compiling. I think I'll try seeing if I can create a project with just Servo.cpp and see what happens.

Well, it looks like I finally figured it out. I had to add the WMath.cpp file to my project. I probably could have linked to it, though.

Thanks for all the help, JimG.

k

Good news. Glad to hear it is solved.

I suspect the problem may go back to the core library. WMath.cpp should already be in that library.

Jim

I thought it should have been too. Perhaps I'll go back tomorrow and investigate that further.