Error with the SD library using an Adafruit SD shield w/ Arduino Mega

Noob here, struggling to write data onto an SD card using an Arduino Mega 2560 and an Adafruit dataloging shield. I am 100% certain that the hardware is correctly wired, but whenever compile and upload my code the serial monitor tells me that the SD card failed or is not present. I believe that the error is within the SD library, specifically, in the file SD2Card.cpp. The header file, SD2Card.h says in the coments that when using an Arduino Mega the value of MEGA_SOFT_SPI must be changed from 0 to 1. When I make no change, the code compiles and uploads without error, but the SD card fails. When I change MEGA_SOFT_SPI to 1, the code fails to compile

* Define MEGA_SOFT_SPI non-zero to use software SPI on Mega Arduinos.
 * Pins used are SS 10, MOSI 11, MISO 12, and SCK 13.
 *
 * MEGA_SOFT_SPI allows an unmodified Adafruit GPS Shield to be used
 * on Mega Arduinos.  Software SPI works well with GPS Shield V1.1
 * but many SD cards will fail with GPS Shield V1.0.
 */
#define MEGA_SOFT_SPI 1
//------------------------------------------------------------------------------
#if MEGA_SOFT_SPI && (defined(__AVR_ATmega1280__)||defined(__AVR_ATmega2560__))
#define SOFTWARE_SPI
#endif  // MEGA_SOFT_SPI
//------------------------------------------------------------------------------
// SPI pin definitions
//

The error message is as follows:

Arduino: 1.6.6 Hourly Build 2015/07/01 04:14 (Mac OS X), Board: "Arduino Mega or Mega 2560, ATmega2560 (Mega 2560)"

/Users/user/Documents/Arduino/libraries/SD/src/utility/Sd2Card.cpp: In member function 'void Sd2Card::chipSelectHigh()':
/Users/user/Documents/Arduino/libraries/SD/src/utility/Sd2Card.cpp:167:5: error: 'SPI' was not declared in this scope
SPI.endTransaction();
^
/Users/user/Documents/Arduino/libraries/SD/src/utility/Sd2Card.cpp: In member function 'void Sd2Card::chipSelectLow()':
/Users/user/Documents/Arduino/libraries/SD/src/utility/Sd2Card.cpp:176:5: error: 'SPI' was not declared in this scope
SPI.beginTransaction(settings);
^
/Users/user/Documents/Arduino/libraries/SD/src/utility/Sd2Card.cpp:176:26: error: 'settings' was not declared in this scope
SPI.beginTransaction(settings);
^
/Users/user/Documents/Arduino/libraries/SD/src/utility/Sd2Card.cpp: In member function 'uint8_t Sd2Card::init(uint8_t, uint8_t)':
/Users/user/Documents/Arduino/libraries/SD/src/utility/Sd2Card.cpp:275:3: error: 'SPI' was not declared in this scope
SPI.beginTransaction(settings);
^
/Users/user/Documents/Arduino/libraries/SD/src/utility/Sd2Card.cpp:275:24: error: 'settings' was not declared in this scope
SPI.beginTransaction(settings);
^
/Users/user/Documents/Arduino/libraries/SD/src/utility/Sd2Card.cpp: In member function 'uint8_t Sd2Card::setSckRate(uint8_t)':
/Users/user/Documents/Arduino/libraries/SD/src/utility/Sd2Card.cpp:518:14: error: 'settings' was not declared in this scope
case 0: settings = SPISettings(25000000, MSBFIRST, SPI_MODE0); break;
^
/Users/user/Documents/Arduino/libraries/SD/src/utility/Sd2Card.cpp:518:57: error: 'SPI_MODE0' was not declared in this scope
case 0: settings = SPISettings(25000000, MSBFIRST, SPI_MODE0); break;
^
/Users/user/Documents/Arduino/libraries/SD/src/utility/Sd2Card.cpp:518:66: error: 'SPISettings' was not declared in this scope
case 0: settings = SPISettings(25000000, MSBFIRST, SPI_MODE0); break;
^
Multiple libraries were found for "SD.h"
Used: /Users/user/Documents/Arduino/libraries/SD
Not used: /Applications/Arduino.app/Contents/Java/libraries/SD
Error compiling.

This report would have more information with
"Show verbose output during compilation"
enabled in File > Preferences.

what am i missing? what am i forgetting?

This answer looks promising.

https://groups.google.com/forum/#!topic/shapercube/TszKUtyt_40

It's down to changes in the Arduino environment V1.0. I found an answer which worked for me on the reprap forums:

From http://forums.reprap.org/read.php?146,111931,112925

Thanks, but no luck, It looks like that would have worked for an older version, but my code has no references to Wprogram ect.

Try including the SPI library in your sketch.

#include <SPI.h>

Is the "datalogging shield" the same thing as the "GPS shield" ?

Here’s a novel idea. Look at the actual error messages you got, starting with the first one.

Your error message:

/Users/user/Documents/Arduino/libraries/SD/src/utility/Sd2Card.cpp: In member function ‘void Sd2Card::chipSelectHigh()’:
/Users/user/Documents/Arduino/libraries/SD/src/utility/Sd2Card.cpp:167:5: error: ‘SPI’ was not declared in this scope

The code near line 167 of Sd2Card.cpp

static uint8_t chip_select_asserted = 0;

void Sd2Card::chipSelectHigh(void) {
  digitalWrite(chipSelectPin_, HIGH);
#ifdef USE_SPI_LIB
  if (chip_select_asserted) {
    chip_select_asserted = 0;
    SPI.endTransaction();
  }
#endif
}

The start of Sd2card.cpp :

/* Arduino Sd2Card Library
 * Copyright (C) 2009 by William Greiman
 *
 * This file is part of the Arduino Sd2Card Library
 *
 * This Library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This Library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the Arduino Sd2Card Library.  If not, see
 * <http://www.gnu.org/licenses/>.
 */
#define USE_SPI_LIB
#include <Arduino.h>
#include "Sd2Card.h"
//------------------------------------------------------------------------------
#ifndef SOFTWARE_SPI
#ifdef USE_SPI_LIB
#include <SPI.h>
static SPISettings settings;
#endif

USE_SPI_LIB is defined, and should cause SPI.h to be included, and then you would not have the problem at line 167.

The reason SPI.h is missing, is that you have told it somehow that you want to use software SPI, which I think you probably don’t actually need to do.

Actually, you probably do want software spi, therefore USE_SPI_LIB should not be defined, therefore all the rest of those nasty nested #if's in sd2card.cpp come into play. It's a mess.

If you have requested the software spi correctly, you should not be getting those error messages. Are you sure you selected the right board in the IDE ?

#if MEGA_SOFT_SPI && (defined(AVR_ATmega1280)||defined(AVR_ATmega2560))
#define SOFTWARE_SPI
#endif // MEGA_SOFT_SPI

If this bit of code is working correctly, then SOFTWARE_SPI should be defined, and therefore USE_SPI_LIB should NOT be defined.

Solution here:

https://github.com/arduino/Arduino/issues/3556