3

I want to use eclipse for arduino programming, so I followed the guides to set up a static project for the arduino core library. Then I added the library file (.a) and the .h and .cpp files to my new project in the settings.
So far, so good.
But now I am trying to add the SPI library to my project without success (I only have the .h and .cpp files, no .a)!

My sample code

#include <Arduino.h>
#include <SPI.h>

int main(void) {
    init();
    pinMode(13, OUTPUT);
    for (;;) {
        digitalWrite(13,1);
        SPI.setBitOrder(0);
    }
}  

The pinMode and digitalWrite parts work, but with the SPI method I get an error:

05:33:26 **** Build of configuration Debug for project ArduinoTestProject ****
make all 
Building file: ../main.cpp
Invoking: AVR C++ Compiler
avr-g++ -I/home/fabio/Workspace/arduinoLIB/SPI -I/home/fabio/Workspace/arduinoCORE -Wall -g2 -gstabs -Os -ffunction-sections -fdata-sections -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: ArduinoTestProject.elf
Invoking: AVR C++ Linker
avr-gcc -s -Os -o"ArduinoTestProject.elf"  ./main.o   -l"arduinoUNO" -l"m" -lm -Wl,-Map,ArduinoTestProject.map,--cref -mrelax -Wl,--gc-sections -L/home/fabio/Workspace/arduinoCORE/lib -mmcu=atmega328p; avr-nm -C -n "ArduinoTestProject.elf" >ArduinoTestProject.symbol
./main.o: In function `main':
../main.cpp:38: undefined reference to `SPIClass::setBitOrder(unsigned char)'
collect2: error: ld returned 1 exit status
avr-nm: 'ArduinoTestProject.elf': No such file
make: *** [ArduinoTestProject.elf] Error 1

05:33:26 Build Finished (took 72ms)  

Does anyone see the problem?

  • Did you tell it to build and link against the .cpp file? – Ignacio Vazquez-Abrams Aug 17 '15 at 4:14
  • 1
    i did not intentionally... I just set configuration values. How do you tell it to? – Fabio Aug 17 '15 at 15:37
  • 1
    Upon makefile and console output analysis I have noticed, that I actually didn't include the library file to be compiled, so the linker doesn't find the object files and exits! – Fabio Aug 18 '15 at 1:05
  • Does that mean the issue is solved? – Nick Gammon Jan 30 '16 at 4:49
  • Not really... But I'm not working in it right now. Should I remove the question or leave it open if I find some time again to work on it? – Fabio Jan 30 '16 at 10:48
1

As far as I understand you are using eclipse CDT directly. (I mean you are not using an "arduino eclipse plugin" that does the stuff below for you)

The first thing to understand is that a arduino library is not treated as a library when compiling a arduino sketch.

In other words in the Arduino IDE, a arduino library is compiled like standard source code just like your .ino files.

As such there is no SPI.a file created during the compilation process. In CDT you can have both options (with or without .a file) but the Arduino IDE way is the easiest, by far.

So lets first assume you want to compile the "Arduino way" aka without SPI.a. What you need to do is simply drag and drop the files in your project. If you want to drop in a subfolder (like libraries/SPI) you will need to add those folders to your include path. That is all there is to it.

If you want to treat the library as a real library (saying you want a SPI.a) the best thing I can think of is to create a new library project for each sketch-project . Drag and drop the SPI code in that project (like above) and make your project dependent on the library project. There are probably some include path issues but the .a file should be found and included in the build

Note that the second approach makes that you will need a SPI project for each and every (arduino) board you have. If you want to use project defines it is even more difficult.

I would strongly advise to start your arduino eclipse development with my arduino eclipse plugin because it is simply easier. Later you can evolve to the full freedom of CDT.

If you want to drag and drop folders instead of the .h and .cpp files you will have to add exclusion rules to your include path. This because otherwise all the examples of the library will be in your project making you have multiple setup and loop functions.

  • You assume correctly: I was using only CDT with avr-eclipse. Weird that i have never heard of this eclipse plugin before...? Anyway you answered my question: it is not easy to create static libraries (.a) to link directly to the main code. This was my intention, as I didn't want to drag and drop files into each single project. – Fabio Feb 23 '16 at 22:00
  • Thanks for accepting my answer. I don't know why you haven't heard of my plugin. It has been around for quite some time. I guess I'm bad at marketing ;-) – jantje Feb 24 '16 at 20:32

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.