linking everything together problem with undefined reference

Hi

I'm new to Arduino and programming in general and I had this code running but now I seem to have messed up and I get error messages when "Linking everything together"... which basically says:-

In function 'setup':
Undefined reference to 'Adafruit_LSM303::begin()'
In function 'setup':
Undefined reference to 'Adafruit_LSM303::begin()'

If anybody is able to spot my error that would be much appreciated !

The code is intended to operate a magnetic sensor with an encoder so that magnetic readings can be plotted against distance. The code is: -

// add adafruit sketch
#include <Wire.h>
#include <Adafruit_LSM303.h>
Adafruit_LSM303 lsm;
int val;
int totmag;

// Digital pin definitions
enum enDigitalPins
{
  // Rotary encoder input lines
  dpInEncoderA=2,
  dpInEncoderB=4,
  dpInEncoderPress=6,
};

static void _ResetPins()
{
  // Rotary encoder input lines
  // Configure as input, turn on pullup resistors
  pinMode(dpInEncoderA, INPUT);
  digitalWrite(dpInEncoderA, HIGH);
  pinMode(dpInEncoderB, INPUT);
  digitalWrite(dpInEncoderB, HIGH);
  pinMode(dpInEncoderPress, INPUT);
  digitalWrite(dpInEncoderPress, HIGH);
}

void setup()
{
  //configure the pins
  _ResetPins();

  // init serial communication
  Serial.begin(9600);
  config_MP();
  Serial.println("Ready to begin");

  // start adafruit
  if (!lsm.begin())   // Try to initialise and warn if we couldn't detect
  {
    Serial.println("Oops...unable to initialise the LSM303. Check wiring");
    while (1);
   }
   //end adafruit
  }

  
void _lowlevel_ReadEncoder(int &rotate,int& press)
 {
    rotate = (digitalRead(dpInEncoderB) * 2) +digitalRead(dpInEncoderA);
    press =digitalRead(dpInEncoderPress);
  }
  
  void loop()
  {
   ReadEncoder(); 
  }



  void ReadEncoder()
  {
    int Position, Press;
    int isFwd = 0;
    int depth = 0;

    _ResetPins();
    Serial.println("Reading the encoder... rotate the knob by one click");
   _lowlevel_ReadEncoder(Position, Press);
   while (!Serial.available())
  {
    int Position2, Press2;
    do
    {
      _lowlevel_ReadEncoder(Position2, Press2);
    }while ((Position2 == Position) && (Press2 == Press));
    if (Position2 != Position)
    {
      // "Forward" is shown by the position going from (0 to 1) or (1 to 3)
      // or (3 to 2) or (2 to 0). Anything else indicates that the user is 
      // turning the device the other way.

      int isFwd = ((Position == 0) && (Position2 == 1)) ||
      ((Position == 1) && (Position2 == 3)) ||
      ((Position == 3) && (Position2 == 2)) ||  
      ((Position == 2) && (Position2 == 0));
          depth = depth + (isFwd ? 10 : -10); //this yields four depth measurements

       lsm.read();   // reads the magnetometer

           val =sq(lsm.magData.x);
           val = val +sq(lsm.magData.y);
           val = val +sq(lsm.magData.z);
           val =sqrt(val);    // calculate total magnetic field

       float Pi = 3.14159;

    // Calculate the angle of the vector y,x
    float heading = (atan2(lsm.magData.z,lsm.magData.x) * 180) / Pi;
    // Normalise  to 0-360
    if (heading < 0)
    {
      heading = 360 + heading;
    }
           Serial.print(depth/4);
           Serial.print(",");
           //Serial.print((int)lsm.magData.x);
           //Serial.print(".");
           Serial.print((int)lsm.magData.y);
           Serial.print(".");
           //Serial.print((int)lsm.magData.z);
           //Serial.print(".");
           Serial.print(depth/4);
           Serial.print(".");
           Serial.print(val);
           Serial.print(".");
           Serial.print(depth/4);
           Serial.print(".");
           Serial.println((int)lsm.accelData.z);
           Serial.print("Compass Heading: ");
           Serial.println(heading);   
           
    }
 if (Press2 != Press)
 {
  Serial.println(Press ? "Press" :"Release");
 }
 Position = Position2;
 Press = Press2;
  }
  }

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read. If you're using the Arduino Web Editor you will not have access to this useful tool but it's still unacceptable to post poorly formatted code. I recommend you to use the standard IDE instead.

spoulter:
which basically says:

Don't tell us what it "basically says". Tell us what it says exactly. When you encounter an error you'll see a button on the right side of the orange bar "Copy error messages". Click that button. Paste the error in a message here USING CODE TAGS (</> button on the toolbar).

Thanks and sorry! Here is the full error code message .....

Arduino: 1.8.7 (Windows 8.1), Board: "Arduino/Genuino Uno"

C:\Program Files (x86)\Arduino\arduino-builder -dump-prefs -logger=machine -hardware C:\Program Files (x86)\Arduino\hardware -tools C:\Program Files (x86)\Arduino\tools-builder -tools C:\Program Files (x86)\Arduino\hardware\tools\avr -built-in-libraries C:\Program Files (x86)\Arduino\libraries -libraries C:\Users\poulters\Documents\Arduino\libraries -fqbn=arduino:avr:uno -vid-pid=0X2341_0X0043 -ide-version=10807 -build-path C:\Users\poulters\AppData\Local\Temp\arduino_build_129944 -warnings=none -build-cache C:\Users\poulters\AppData\Local\Temp\arduino_cache_319530 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.arduinoOTA.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.arduinoOTA-1.2.1.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.avrdude.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.avrdude-6.3.0-arduino14.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.avr-gcc.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.avr-gcc-5.4.0-atmel3.6.1-arduino2.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -verbose C:\Users\poulters\Documents\Arduino\MiniMagcone_oct26b\MiniMagcone_oct26b.ino
C:\Program Files (x86)\Arduino\arduino-builder -compile -logger=machine -hardware C:\Program Files (x86)\Arduino\hardware -tools C:\Program Files (x86)\Arduino\tools-builder -tools C:\Program Files (x86)\Arduino\hardware\tools\avr -built-in-libraries C:\Program Files (x86)\Arduino\libraries -libraries C:\Users\poulters\Documents\Arduino\libraries -fqbn=arduino:avr:uno -vid-pid=0X2341_0X0043 -ide-version=10807 -build-path C:\Users\poulters\AppData\Local\Temp\arduino_build_129944 -warnings=none -build-cache C:\Users\poulters\AppData\Local\Temp\arduino_cache_319530 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.arduinoOTA.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.arduinoOTA-1.2.1.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.avrdude.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.avrdude-6.3.0-arduino14.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.avr-gcc.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.avr-gcc-5.4.0-atmel3.6.1-arduino2.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -verbose C:\Users\poulters\Documents\Arduino\MiniMagcone_oct26b\MiniMagcone_oct26b.ino
Using board 'uno' from platform in folder: C:\Program Files (x86)\Arduino\hardware\arduino\avr
Using core 'arduino' from platform in folder: C:\Program Files (x86)\Arduino\hardware\arduino\avr
Detecting libraries used...
"C:\\Program Files (x86)\\Arduino\\hardware\\tools\\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10807 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\cores\\arduino" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\variants\\standard" "C:\\Users\\poulters\\AppData\\Local\\Temp\\arduino_build_129944\\sketch\\MiniMagcone_oct26b.ino.cpp" -o nul
"C:\\Program Files (x86)\\Arduino\\hardware\\tools\\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10807 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\cores\\arduino" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\variants\\standard" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\libraries\\Wire\\src" "C:\\Users\\poulters\\AppData\\Local\\Temp\\arduino_build_129944\\sketch\\MiniMagcone_oct26b.ino.cpp" -o nul
Using cached library dependencies for file: C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\Wire\src\Wire.cpp
Using cached library dependencies for file: C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\Wire\src\utility\twi.c
Generating function prototypes...
"C:\\Program Files (x86)\\Arduino\\hardware\\tools\\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10807 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\cores\\arduino" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\variants\\standard" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\libraries\\Wire\\src" "C:\\Users\\poulters\\AppData\\Local\\Temp\\arduino_build_129944\\sketch\\MiniMagcone_oct26b.ino.cpp" -o "C:\\Users\\poulters\\AppData\\Local\\Temp\\arduino_build_129944\\preproc\\ctags_target_for_gcc_minus_e.cpp"
"C:\\Program Files (x86)\\Arduino\\tools-builder\\ctags\\5.8-arduino11/ctags" -u --language-force=c++ -f - --c++-kinds=svpf --fields=KSTtzns --line-directives "C:\\Users\\poulters\\AppData\\Local\\Temp\\arduino_build_129944\\preproc\\ctags_target_for_gcc_minus_e.cpp"
Compiling sketch...
"C:\\Program Files (x86)\\Arduino\\hardware\\tools\\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10807 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\cores\\arduino" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\variants\\standard" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\libraries\\Wire\\src" "C:\\Users\\poulters\\AppData\\Local\\Temp\\arduino_build_129944\\sketch\\MiniMagcone_oct26b.ino.cpp" -o "C:\\Users\\poulters\\AppData\\Local\\Temp\\arduino_build_129944\\sketch\\MiniMagcone_oct26b.ino.cpp.o"
Compiling libraries...
Compiling library "Wire"
Using previously compiled file: C:\Users\poulters\AppData\Local\Temp\arduino_build_129944\libraries\Wire\Wire.cpp.o
Using previously compiled file: C:\Users\poulters\AppData\Local\Temp\arduino_build_129944\libraries\Wire\utility\twi.c.o
Compiling core...
Using precompiled core: C:\Users\poulters\AppData\Local\Temp\arduino_cache_319530\core\core_arduino_avr_uno_0c812875ac70eb4a9b385d8fb077f54c.a
Linking everything together...
"C:\\Program Files (x86)\\Arduino\\hardware\\tools\\avr/bin/avr-gcc" -w -Os -g -flto -fuse-linker-plugin -Wl,--gc-sections -mmcu=atmega328p -o "C:\\Users\\poulters\\AppData\\Local\\Temp\\arduino_build_129944/MiniMagcone_oct26b.ino.elf" "C:\\Users\\poulters\\AppData\\Local\\Temp\\arduino_build_129944\\sketch\\MiniMagcone_oct26b.ino.cpp.o" "C:\\Users\\poulters\\AppData\\Local\\Temp\\arduino_build_129944\\libraries\\Wire\\Wire.cpp.o" "C:\\Users\\poulters\\AppData\\Local\\Temp\\arduino_build_129944\\libraries\\Wire\\utility\\twi.c.o" "C:\\Users\\poulters\\AppData\\Local\\Temp\\arduino_build_129944/..\\arduino_cache_319530\\core\\core_arduino_avr_uno_0c812875ac70eb4a9b385d8fb077f54c.a" "-LC:\\Users\\poulters\\AppData\\Local\\Temp\\arduino_build_129944" -lm
C:\Users\poulters\AppData\Local\Temp\ccQVLzYb.ltrans0.ltrans.o: In function `setup':

C:\Users\poulters\Documents\Arduino\MiniMagcone_oct26b/MiniMagcone_oct26b.ino:85: undefined reference to `Adafruit_LSM303::begin()'

C:\Users\poulters\AppData\Local\Temp\ccQVLzYb.ltrans0.ltrans.o: In function `ReadEncoder':

C:\Users\poulters\Documents\Arduino\MiniMagcone_oct26b/MiniMagcone_oct26b.ino:135: undefined reference to `Adafruit_LSM303::read()'

collect2.exe: error: ld returned 1 exit status

Using library Wire at version 1.0 in folder: C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\Wire 
exit status 1
Error compiling for board Arduino/Genuino Uno.

........ and the full auto-formatted code is.......

//  Operation:
// Connect to COM4 (right hand side on Dell FES0896) using USB cable
// to arduino. Open Makerplot
// in XY plot mode, select Port4, click on button at bottom left. The
// scales and titles should now be set automatically by the arduino.
// Operation: The knob is turned as the magnetometer descends. One click
// generates four readings and moves the x axis four tick-marks
// so 1 click = 0.1m is the recommended ratio for a reading every
// 0.025m. 10 clicks = 1m. Each data point on graph is 0.025m or 2.5cm.

// An adafruit magnetic sensor LSM303 is housed in a cylindrical case
// and connected by 20m of 5mm ethernet cable to an arduino Uno in
// another case (Farnell code 236-0474) with the four connectors
// as follows
// Blue = SCL (end pin on Uno) near USB
// Orange = SDM (next to end pin on Uno)
// Brown = 5v
// Green = Ground (shared with middle pin of encoder).

// Encoder is part no 152-0813 EC12E2424407 24 DET 24 PPR from Farnell
// Ltd plus knob part  no 128-2595 connected as follows: -
// My setup: This encoder has pin C (middle pin of the three on one
// side) and one of the pair on the other side connected to ground.
// The other pins are connected as follows to arduino uno:
//  'A' the left of the 3 pins when pins are top and facing you:
// digital pin 2
//  'B' the right of the 3 pins when pins are top and facing you:
// digital pin 4
// 'press': digital pin 6 (not required)

// Operation: The knob is turned as the Mini-Magcone descends. One
// click generates four readings and moves the x axis four tick-marks
// so 1 click = 0.1m is the recommended ration for a reading every 0.025m.
// 10 clicks =1m. Each data point on graph is 0.025m or 2.5cm.

// The Arduino is connected by USB to the laptop. Makerplot.com graphing
// software is then used to plot and log the data. Makerplot is a Windows
// supervisory control and data acquisition system (SCADA) application.

// The Arduino is connected to the magnetic sensor by 5mm Ethernet cable
// CAT 5e via BULGIN-PXP6034/B-PLUG, ETHERNET, REWIREABLE, FLEX connector
// and BULGIN-PXP6033TP-SOCKET, CHASSIS, JACK TO JACK socket plus caps
// BULGIN-PXP6083-SEALING CAP, FOR CHASSIS CONNECTORS and
// BULGIN-PXP6081-SEALING CAP, FOR FLEX CONNECTORS.

// add adafruit sketch
#include <Wire.h>
#include <Adafruit_LSM303.h>
Adafruit_LSM303 lsm;
int val;
int totmag;

// Digital pin definitions
enum enDigitalPins
{
  // Rotary encoder input lines
  dpInEncoderA = 2,
  dpInEncoderB = 4,
  dpInEncoderPress = 6,
};

static void _ResetPins()
{
  // Rotary encoder input lines
  // Configure as input, turn on pullup resistors
  pinMode(dpInEncoderA, INPUT);
  digitalWrite(dpInEncoderA, HIGH);
  pinMode(dpInEncoderB, INPUT);
  digitalWrite(dpInEncoderB, HIGH);
  pinMode(dpInEncoderPress, INPUT);
  digitalWrite(dpInEncoderPress, HIGH);
}

void setup()
{
  //configure the pins
  _ResetPins();

  // init serial communication
  Serial.begin(9600);
  config_MP();
  Serial.println("Ready to begin");

  // start adafruit
  if (!lsm.begin())   // Try to initialise and warn if we couldn't detect
  {
    Serial.println("Oops...unable to initialise the LSM303. Check wiring");
    while (1);
  }
  //end adafruit
}


void _lowlevel_ReadEncoder(int &rotate, int& press)
{
  rotate = (digitalRead(dpInEncoderB) * 2) + digitalRead(dpInEncoderA);
  press = digitalRead(dpInEncoderPress);
}

void loop()
{
  ReadEncoder();
}



void ReadEncoder()
{
  int Position, Press;
  int isFwd = 0;
  int depth = 0;

  _ResetPins();
  Serial.println("Reading the encoder... rotate the knob by one click");
  _lowlevel_ReadEncoder(Position, Press);
  while (!Serial.available())
  {
    int Position2, Press2;
    do
    {
      _lowlevel_ReadEncoder(Position2, Press2);
    } while ((Position2 == Position) && (Press2 == Press));
    if (Position2 != Position)
    {
      // "Forward" is shown by the position going from (0 to 1) or (1 to 3)
      // or (3 to 2) or (2 to 0). Anything else indicates that the user is
      // turning the device the other way.

      int isFwd = ((Position == 0) && (Position2 == 1)) ||
                  ((Position == 1) && (Position2 == 3)) ||
                  ((Position == 3) && (Position2 == 2)) ||
                  ((Position == 2) && (Position2 == 0));
      depth = depth + (isFwd ? 10 : -10); //this yields four depth measurements

      lsm.read();   // reads the magnetometer

      val = sq(lsm.magData.x);
      val = val + sq(lsm.magData.y);
      val = val + sq(lsm.magData.z);
      val = sqrt(val);   // calculate total magnetic field

      float Pi = 3.14159;

      // Calculate the angle of the vector y,x
      float heading = (atan2(lsm.magData.z, lsm.magData.x) * 180) / Pi;
      // Normalise  to 0-360
      if (heading < 0)
      {
        heading = 360 + heading;
      }
      Serial.print(depth / 4);
      Serial.print(",");
      //Serial.print((int)lsm.magData.x);
      //Serial.print(".");
      Serial.print((int)lsm.magData.y);
      Serial.print(".");
      //Serial.print((int)lsm.magData.z);
      //Serial.print(".");
      Serial.print(depth / 4);
      Serial.print(".");
      Serial.print(val);
      Serial.print(".");
      Serial.print(depth / 4);
      Serial.print(".");
      Serial.println((int)lsm.accelData.z);
      Serial.print("Compass Heading: ");
      Serial.println(heading);

    }
    if (Press2 != Press)
    {
      Serial.println(Press ? "Press" : "Release");
    }
    Position = Position2;
    Press = Press2;
  }
}

void config_MP()
{
  delay(2000);      //allow connection to stabilise
  Serial.println();   // send in case rubbish in queue
  Serial.println("!ASCL ALL");     //sets span of Y-axis to fit all data
  Serial.println("!TMAX 10");     // set x axis to 10m .... 1 click = 10cm..
  Serial.println("!YLBL 1000 nT");    //label Y-axis Adafruit LSM303
  Serial.println("!XLBL Depth below surface (m)");    // label X-axis
  Serial.println("!AMTH 0,0.01,0");   // divides depth by 100
  Serial.println("!AMTH 2,0.01,0");   // divides depth by 100
  Serial.println("!AMTH 4,0.01,0");   // divides depth by 100
  Serial.println("!GRID 40,40");  // set grid to 40x40
  Serial.println("!PNTS 8000");   // set data points to 8000
  Serial.println("!SAVD ON");   // // sets logging to auto on
  Serial.println("!RSET(CR)! PLOT ON");  // reset plot, send CR as MP value
  Serial.println("!@TEXT 40A,110A,1A,(Blue),blue = y accelerometer");
  Serial.println("!@TEXT 40A,107A,1A,(Red),red = total magnetic field");
  Serial.println("!@TEXT 40A,104A,1A,(Green),green = z axis of magnetic field");
  Serial.println("!@TEXT 5A,107A,1.5A,(Black),MINI-MAGCONE");
  Serial.println("!@TEXT 5A,104A,1A,(Black),Configured by Steve Poulter");

It seems like you've done something strange with the installation of the Adafruit_LSM303 library. The error is what I would expect if you only had the .h file of the library installed and the .cpp file was missing. Even more odd, I don't see the path to the library in the compilation output. It's almost as though you just went and dumped the .h file in some random folder.

The sketch you posted is not complete. I wonder if maybe you missed copying some lines at the end?

Thanks a million, that sounds very likely, I'm very capable at making basic errors!

I installed the latest version of the library, added a closing brace to the end of your code, and then it compiled with no problems. When I deleted the file Adafruit_LSM303.cpp from the library, I got the same undefined reference errors as you, but my output showed the location of the Adafruit_LSM303 library installation.

The first thing you need to do is find where the file Adafruit_LSM303.h is located. Do a search of these folders:

  • C:\Program Files (x86)\Arduino
  • C:\Users\poulters\Documents\Arduino

You can click on the folder in Windows Explorer and then type the filename into the search box at the top right corner of the window to search that folder and all subfolders.

Once you find the file, delete it.

Now you can install the library correctly like this:

After that, the error should be fixed.

This is a great forum, really helpful, many thanks, you have solved it and it all runs fine now, I just need to get MakerPlot to co-operate again fully.

I'm glad to hear it's working now. Enjoy!
Per