queezythegreat / arduino-cmake

Arduino CMake Build system
644 stars 216 forks source link

Code using the Arduino SD library fails to link #22

Closed nilrog closed 12 years ago

nilrog commented 12 years ago

I just made a quick test to see if this tool was useful and not since I really want to get rid of the Arduino IDE but still be able to use all the libraries out there. So I simply created a target using the CardInfo example from the SD library included with Arduino 1.0 (and adding the Arduino.h include to the file). But the code won't link correctly, most likely because the SD library has code also in a subdirectory called "utility" which I guess your cmake tool cannot detect.

Here is the error:

Linking CXX executable sd_cardinfo.elf CMakeFiles/sd_cardinfo.dir/sd_cardinfo.cpp.obj: In function SdFile': /Applications/Arduino.app/Contents/Resources/Java/libraries/SD/utility/SdFat.h:138: undefined reference tovtable for SdFile' /Applications/Arduino.app/Contents/Resources/Java/libraries/SD/utility/SdFat.h:138: undefined reference to vtable for SdFile' CMakeFiles/sd_cardinfo.dir/sd_cardinfo.cpp.obj: In functionsetup': /Users/nilrog/Documents/Arduino/arduino-cmake/sd_cardinfo/sd_cardinfo.cpp:50: undefined reference to Sd2Card::init(unsigned char, unsigned char)' CMakeFiles/sd_cardinfo.dir/sd_cardinfo.cpp.obj: In functionSdVolume::init(Sd2Card)': /Applications/Arduino.app/Contents/Resources/Java/libraries/SD/utility/SdFat.h:450: undefined reference to `SdVolume::init(Sd2Card, unsigned char)' /Applications/Arduino.app/Contents/Resources/Java/libraries/SD/utility/SdFat.h:450: undefined reference to SdVolume::init(Sd2Card*, unsigned char)' CMakeFiles/sd_cardinfo.dir/sd_cardinfo.cpp.obj: In functionSdFile::openRoot(SdVolume&)': /Applications/Arduino.app/Contents/Resources/Java/libraries/SD/utility/SdFat.h:345: undefined reference to SdFile::openRoot(SdVolume*)' CMakeFiles/sd_cardinfo.dir/sd_cardinfo.cpp.obj: In functionsetup': /Users/nilrog/Documents/Arduino/arduino-cmake/sd_cardinfo/sd_cardinfo.cpp:106: undefined reference to `SdFile::ls(unsigned char, unsigned char)' make[2]: * [sd_cardinfo/sd_cardinfo.elf] Error 1 make[1]: * [sd_cardinfo/CMakeFiles/sd_cardinfo.dir/all] Error 2

If I try the same using one of the EEPROM examples from Arduino the code compiles and links correctly.

johnyb commented 12 years ago

can you try to add this to your CMakeLists.txt

set(SD_RECURSE True)

this might work, though I haven’t tried, yet.

nilrog commented 12 years ago

Yes, that worked. Just spotted it in the docs too :)

nilrog commented 12 years ago

After reading the cake sources I believe this is a bug...because for the Wire and Ethernet libraries the recurse option is set. So that should be the case for SD too.

queezythegreat commented 12 years ago

Recurse patch was merged in, closing issue.

KoVe commented 8 years ago

I get similar errors, but set(SD_RECURSE True) doesn't work. I made a small test.

CMakeLists.txt

cmake_minimum_required(VERSION 2.8.4) set(CMAKE_TOOLCHAIN_FILE ${CMAKE_SOURCE_DIR}/cmake/ArduinoToolchain.cmake) set(PROJECT_NAME SDtest) project(${PROJECT_NAME}) ` set(${CMAKE_PROJECT_NAME}_ARDLIBS SD) set(${CMAKE_PROJECT_NAME}_SKETCH SDtest.ino) generate_arduino_firmware(${CMAKE_PROJECT_NAME})`

SDtest.ino

#include <Arduino.h> #include <SD.h> void setup() { SD.begin(4); } void loop() {}

Messages:

[ 97%] Linking CXX executable SDtest.elf libuno_SD.a(Sd2Card.cpp.obj): In functionSPIClass::endTransaction()': /Applications/Arduino.app/Contents/Java/hardware/arduino/avr/libraries/SPI/src/SPI.h:275: undefined reference to SPIClass::interruptMode'`` /Applications/Arduino.app/Contents/Java/hardware/arduino/avr/libraries/SPI/src/SPI.h:275: undefined reference to SPIClass::interruptSave' libuno_SD.a(Sd2Card.cpp.obj): In functionSPIClass::beginTransaction(SPISettings)': /Applications/Arduino.app/Contents/Java/hardware/arduino/avr/libraries/SPI/src/SPI.h:179: undefined reference toSPIClass::interruptMode' /Applications/Arduino.app/Contents/Java/hardware/arduino/avr/libraries/SPI/src/SPI.h:184: undefined reference to SPIClass::interruptMode' /Applications/Arduino.app/Contents/Java/hardware/arduino/avr/libraries/SPI/src/SPI.h:185: undefined reference toSPIClass::interruptSave' /Applications/Arduino.app/Contents/Java/hardware/arduino/avr/libraries/SPI/src/SPI.h:186: undefined reference to SPIClass::interruptMask' /Applications/Arduino.app/Contents/Java/hardware/arduino/avr/libraries/SPI/src/SPI.h:191: undefined reference toSPIClass::interruptSave' libuno_SD.a(Sd2Card.cpp.obj): In function SPIClass::endTransaction()': /Applications/Arduino.app/Contents/Java/hardware/arduino/avr/libraries/SPI/src/SPI.h:269: undefined reference toSPIClass::interruptMode' libuno_SD.a(Sd2Card.cpp.obj): In function Sd2Card::init(unsigned char, unsigned char)': /Applications/Arduino.app/Contents/Java/libraries/SD/src/utility/Sd2Card.cpp:268: undefined reference toSPIClass::begin()' libuno_SD.a(Sd2Card.cpp.obj): In function SPIClass::endTransaction()': /Applications/Arduino.app/Contents/Java/hardware/arduino/avr/libraries/SPI/src/SPI.h:269: undefined reference toSPIClass::interruptMode' collect2: error: ld returned 1 exit status make[3]: * [SDtest.elf] Error 1 make[2]: * [CMakeFiles/SDtest.dir/all] Error 2 make[1]: * [CMakeFiles/SDtest.dir/rule] Error 2 make: * [SDtest] Error 2

With the arduino IDE it compiles and links correctly. Any idea what is happening?

KoVe commented 8 years ago

Anyhow, by adding #include <SPI.h> and SPI.begin(); (or some other SPI command) to the main program, clion links it correctly.