Error "undefined reference to..."

I am using code found on the internet. I have included all the .h files in the arduino library subfolder.
C:\Users\Robert\Documents\Arduino\libraries\myhfiles

Code in the main ino is

#include <english.h>
#include <sound.h>
#include <TTS.h>
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_LSM9DS1.h>
#include <Adafruit_Sensor.h>

// i2c, initialize the sensor breakout board
Adafruit_LSM9DS1 lsm = Adafruit_LSM9DS1();

#define LSM9DS1_SCK 21
#define LSM9DS1_MISO 12
#define LSM9DS1_MOSI 20
#define LSM9DS1_XGCS 6
#define LSM9DS1_MCS 5

void setupSensor()
{
  // Set the magnetometer sensitivity
  lsm.setupMag(lsm.LSM9DS1_MAGGAIN_4GAUSS);
}

// Dictionary of the 1000 most common English words.
char *words[] = {"the", "of", "to", "and", "a", "in", "is", "it", "you", "that", "he", "was", "for", "on", "are", "with", "as", "I", "his", "they", "be", "at", "one", "have", "this", "from", "or", "had", "by", "hot", "word", "but", "what", "some", "we", "can", "out", "other", "were", "all", "there", "when", "up", "use", "your", "how", "said", "an", "each", "she", "which", "do", "their", "time", "if", "will", "way", "about", "many", "then", "them", "write", "would", "like", "so", "these", "her", "long", "make", "thing", "see", "him", "two", "has", "look", "more", "day", "could", "go", "come", "did", "number", "sound", "no", "most", "people", "my", "over", "know", "water", "than", "call", "first", "who", "may", "down", "side", "been", "now", "find", "any", "new", "work", "part", "take", "get", "place", "made", "live", "where", "after", "back", "little", "only", "round", "man", "year", "came", "show", "every", "good", "me", "give", "our", "under", "name", "very", "through", "just", "form", "sentence", "great", "think", "say", "help", "low", "line", "differ", "turn", "cause", "much", "mean", "before", "move", "right", "boy", "old", "too", "same", "tell", "does", "set", "three", "want", "air", "well", "also", "play", "small", "end", "put", "home", "read", "hand", "port", "large", "spell", "add", "even", "land", "here", "must", "big", "high", "such", "follow", "act", "why", "ask", "men", "change", "went", "light", "kind", "off", "need", "house", "picture", "try", "us", "again", "animal", "point", "mother", "world", "near", "build", "self", "earth", "father", "head", "stand", "own", "page", "should", "country", "found", "answer", "school", "grow", "study", "still", "learn", "plant", "cover", "food", "sun", "four", "between", "state", "keep", "eye", "never", "last", "let", "thought", "city", "tree", "cross", "farm", "hard", "start", "might", "story", "saw", "far", "sea", "draw", "left", "late", "run", "don't", "while", "press", "close", "night", "real", "life", "few", "north", "open", "seem", "together", "next", "white", "children", "begin", "got", "walk", "example", "ease", "paper", "group", "always", "music", "those", "both", "mark", "often", "letter", "until", "mile", "river", "car", "feet", "care", "second", "book", "carry", "took", "science", "eat", "room", "friend", "began", "idea", "fish", "mountain", "stop", "once", "base", "hear", "horse", "cut", "sure", "watch", "color", "face", "wood", "main", "enough", "plain", "girl", "usual", "young", "ready", "above", "ever", "red", "list", "though", "feel", "talk", "bird", "soon", "body", "dog", "family", "direct", "pose", "leave", "song", "measure", "door", "product", "black", "short", "numeral", "class", "wind", "question", "happen", "complete", "ship", "area", "half", "rock", "order", "fire", "south", "problem", "piece", "told", "knew", "pass", "since", "top", "whole", "king", "space", "heard", "best", "hour", "better", "true", "during", "hundred", "five", "remember", "step", "early", "hold", "west", "ground", "interest", "reach", "fast", "verb", "sing", "listen", "six", "table", "travel", "less", "morning", "ten", "simple", "several", "vowel", "toward", "war", "lay", "against", "pattern", "slow", "center", "love", "person", "money", "serve", "appear", "road", "map", "rain", "rule", "govern", "pull", "cold", "notice", "voice", "unit", "power", "town", "fine", "certain", "fly", "fall", "lead", "cry", "dark", "machine", "note", "wait", "plan", "figure", "star", "box", "noun", "field", "rest", "correct", "able", "pound", "done", "beauty", "drive", "stood", "contain", "front", "teach", "week", "final", "gave", "green", "oh", "quick", "develop", "ocean", "warm", "free", "minute", "strong", "special", "mind", "behind", "clear", "tail", "produce", "fact", "street", "inch", "multiply", "nothing", "course", "stay", "wheel", "full", "force", "blue", "object", "decide", "surface", "deep", "moon", "island", "foot", "system", "busy", "test", "record", "boat", "common", "gold", "possible", "plane", "stead", "dry", "wonder", "laugh", "thousand", "ago", "ran", "check", "game", "shape", "equate", "hot", "miss", "brought", "heat", "snow", "tire", "bring", "yes", "distant", "fill", "east", "paint", "language", "among", "grand", "ball", "yet", "wave", "drop", "heart", "am", "present", "heavy", "dance", "engine", "position", "arm", "wide", "sail", "material", "size", "vary", "settle", "speak", "weight", "general", "ice", "matter", "circle", "pair", "include", "divide", "syllable", "felt", "perhaps", "pick", "sudden", "count", "square", "reason", "length", "represent", "art", "subject", "region", "energy", "hunt", "probable", "bed", "brother", "egg", "ride", "cell", "believe", "fraction", "forest", "sit", "race", "window", "store", "summer", "train", "sleep", "prove", "lone", "leg", "exercise", "wall", "catch", "mount", "wish", "sky", "board", "joy", "winter", "sat", "written", "wild", "instrument", "kept", "glass", "grass", "cow", "job", "edge", "sign", "visit", "past", "soft", "fun", "bright", "gas", "weather", "month", "million", "bear", "finish", "happy", "hope", "flower", "clothe", "strange", "gone", "jump", "baby", "eight", "village", "meet", "root", "buy", "raise", "solve", "metal", "whether", "push", "seven", "paragraph", "third", "shall", "held", "hair", "describe", "cook", "floor", "either", "result", "burn", "hill", "safe", "cat", "century", "consider", "type", "law", "bit", "coast", "copy", "phrase", "silent", "tall", "sand", "soil", "roll", "temperature", "finger", "industry", "value", "fight", "lie", "beat", "excite", "natural", "view", "sense", "ear", "else", "quite", "broke", "case", "middle", "kill", "son", "lake", "moment", "scale", "loud", "spring", "observe", "child", "straight", "consonant", "nation", "dictionary", "milk", "speed", "method", "organ", "pay", "age", "section", "dress", "cloud", "surprise", "quiet", "stone", "tiny", "climb", "cool", "design", "poor", "lot", "experiment", "bottom", "key", "iron", "single", "stick", "flat", "twenty", "skin", "smile", "crease", "hole", "trade", "melody", "trip", "office", "receive", "row", "mouth", "exact", "symbol", "die", "least", "trouble", "shout", "except", "wrote", "seed", "tone", "join", "suggest", "clean", "break", "lady", "yard", "rise", "bad", "blow", "oil", "blood", "touch", "grew", "cent", "mix", "team", "wire", "cost", "lost", "brown", "wear", "garden", "equal", "sent", "choose", "fell", "fit", "flow", "fair", "bank", "collect", "save", "control", "decimal", "gentle", "woman", "captain", "practice", "separate", "difficult", "doctor", "please", "protect", "noon", "whose", "locate", "ring", "character", "insect", "caught", "period", "indicate", "radio", "spoke", "atom", "human", "history", "effect", "electric", "expect", "crop", "modern", "element", "hit", "student", "corner", "party", "supply", "bone", "rail", "imagine", "provide", "agree", "thus", "capital", "won't", "chair", "danger", "fruit", "rich", "thick", "soldier", "process", "operate", "guess", "necessary", "sharp", "wing", "create", "neighbor", "wash", "bat", "rather", "crowd", "corn", "compare", "poem", "string", "bell", "depend", "meat", "rub", "tube", "famous", "dollar", "stream", "fear", "sight", "thin", "triangle", "planet", "hurry", "chief", "colony", "clock", "mine", "tie", "enter", "major", "fresh", "search", "send", "yellow", "gun", "allow", "print", "dead", "spot", "desert", "suit", "current", "lift", "rose", "continue", "block", "chart", "hat", "sell", "success", "company", "subtract", "event", "particular", "deal", "swim", "term", "opposite", "wife", "shoe", "shoulder", "spread", "arrange", "camp", "invent", "cotton", "born", "determine", "quart", "nine", "truck", "noise", "level", "chance", "gather", "shop", "stretch", "throw", "shine", "property", "column", "molecule", "select", "wrong", "gray", "repeat", "require", "broad", "prepare", "salt", "nose", "plural", "anger", "claim", "continent", "oxygen", "sugar", "death", "pretty", "skill", "women", "season", "solution", "magnet", "silver", "thank", "branch", "match", "suffix", "especially", "fig", "afraid", "huge", "sister", "steel", "discuss", "forward", "similar", "guide", "experience", "score", "apple", "bought", "led", "pitch", "coat", "mass", "card", "band", "rope", "slip", "win", "dream", "evening", "condition", "feed", "tool", "total", "basic", "smell", "valley", "nor", "double", "seat", "arrive", "master", "track", "parent", "shore", "division", "sheet", "substance", "favor", "connect", "post", "spend", "chord", "fat", "glad", "original", "share", "station", "dad", "bread", "charge", "proper", "bar", "offer", "segment", "slave", "duck", "instant", "market", "degree", "populate", "chick", "dear", "enemy", "reply", "drink", "occur", "support", "speech", "nature", "range", "steam", "motion", "path", "liquid", "log", "meant", "quotient", "teeth", "shell", "neck"};

// Media pins
#define PWM A0
#define buttonPin 13

TTS text2speech(PWM);  // default is digital pin 10
int buttonState = 0;
float lastMag = 0;
float lastTemp = 0;

void setup() {
  pinMode(buttonPin, INPUT);
  Serial.begin(115200);
  // Try to initialise and warn if we couldn't detect the chip
  if (!lsm.begin())
  {
    Serial.println("Oops ... unable to initialize the LSM9DS1. Check your wiring!");
    while (1);
  }
  setupSensor();
}

void loop() {
  buttonState = digitalRead(buttonPin);
  // Check if the switch has been thrown. If so, start measuring and speaking.
  if (buttonState == HIGH) {
    lsm.read();  /* ask it to read in the data */

    /* Get a new sensor event */
    sensors_event_t a, m, g, temp;

    lsm.getEvent(&a, &m, &g, &temp);
    // Get the latest magnetometer and temperature reading
    float newMag = sqrt(sq(m.magnetic.x) + sq(m.magnetic.y) + sq(m.magnetic.z));
    float newTemp = temp.temperature;
    // If this isn't our first reading...
    if (lastMag != 0) {
      // Calculate the change in magnetic field/temperature
      float deltaMag = abs(newMag - lastMag);
      float deltaTemp = abs(newTemp - lastTemp);
      // Get the biggest change of the two
      float index = max(deltaMag, deltaTemp);
      // Convert to an index of our dictionary array
      float wordindfloat = index * 1000.0;
      int wordindint = round(wordindfloat);
      // Speak the change on teh speaker
      text2speech.setPitch(6);
      text2speech.sayText(words[wordindint]);
      Serial.println(words[wordindint]);
      // Store the readings for the next loop
      lastMag = newMag;
      lastTemp = newTemp;
    }
    // Store our readings as a baseline
    else {
      lastMag = newMag;
      lastTemp = newTemp;
    }
    delay(1000);
  }
}

ERROR is:

C:\Users\Robert\AppData\Local\Temp\ccyUcG10.ltrans0.ltrans.o: In function global constructors keyed to 65535_0_Ovilus_I.ino.cpp.o.2731': <artificial>:(.text.startup+0x62): undefined reference to Adafruit_LSM9DS1::Adafruit_LSM9DS1(long)'
:(.text.startup+0x6e): undefined reference to TTS::TTS(int)' C:\Users\Robert\AppData\Local\Temp\ccyUcG10.ltrans0.ltrans.o: In function setup':
C:\Users\Robert\Documents\Arduino\Ovilus I\Ovilus_I/Ovilus_I.ino:40: undefined reference to Adafruit_LSM9DS1::begin()' C:\Users\Robert\AppData\Local\Temp\ccyUcG10.ltrans0.ltrans.o: In function setupSensor':
C:\Users\Robert\Documents\Arduino\Ovilus I\Ovilus_I/Ovilus_I.ino:21: undefined reference to Adafruit_LSM9DS1::setupMag(Adafruit_LSM9DS1::lsm9ds1MagGain_t)' C:\Users\Robert\AppData\Local\Temp\ccyUcG10.ltrans0.ltrans.o: In function loop':
C:\Users\Robert\Documents\Arduino\Ovilus I\Ovilus_I/Ovilus_I.ino:52: undefined reference to Adafruit_LSM9DS1::read()' C:\Users\Robert\Documents\Arduino\Ovilus I\Ovilus_I/Ovilus_I.ino:57: undefined reference to Adafruit_LSM9DS1::getEvent(sensors_event_t*, sensors_event_t*, sensors_event_t*, sensors_event_t*)'
C:\Users\Robert\Documents\Arduino\Ovilus I\Ovilus_I/Ovilus_I.ino:73: undefined reference to `TTS::sayText(char const*)'
collect2.exe: error: ld returned 1 exit status

Using library ovilus in folder: C:\Users\Robert\Documents\Arduino\libraries\ovilus (legacy)
Using library Wire at version 1.0 in folder: C:\Users\Robert\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\libraries\Wire
Using library SPI at version 1.0 in folder: C:\Users\Robert\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\libraries\SPI
Using library Adafruit BusIO at version 1.14.1 in folder: C:\Users\Robert\Documents\Arduino\libraries\Adafruit_BusIO
Using library Adafruit LIS3MDL at version 1.2.1 in folder: C:\Users\Robert\Documents\Arduino\libraries\Adafruit_LIS3MDL
exit status 1

Compilation error: exit status 1
I have never run into this problem before, so I am not sure how to solve it. Any help is appreciated.

What exactly do you mean by this ?

Are the libraries installed in their own folders in the Sketchbook libraries folder ?

libraries do not go inside a "myfiles" directory. When you install them with the IDE, they should all be in their own directory under the "libraries" folder...

C:\Users\Robert\Documents\Arduino\libraries\english
C:\Users\Robert\Documents\Arduino\libraries\sound
C:\Users\Robert\Documents\Arduino\libraries\TTS
C:\Users\Robert\Documents\Arduino\libraries\Wire
C:\Users\Robert\Documents\Arduino\libraries\Adafruit_LSM9DS1
C:\Users\Robert\Documents\Arduino\libraries\Adafruit_Sensor

No, it's not. The library directory name does not have to be the same as the .h file name.
You can easily verify this by opening any existed library containing more than one .h file. According to your logic, such libraries should not work at all, but it works.

In fact, the library directory name doesn't matter at all. The XXX.h file may be in the YYY-master directory and this will not prevent the IDE from finding it.

But, it's more complicated than just where to put a .h file; many libraries also include .cpp files as part of the installation, and these must also be located by the compile/link process.

The matching .cpp files must be in the same library directory as the .h file or they won't be included in the compile and you will get an 'undefined reference' error for any names defined in the .cpp.

For Adafruit libraries like "Adafruit_LSM9DS1" and "Adafruit_Sensor" the easy way is to use Library Manager (Tools -> Manage Libraries...) to install them. For "english", "sound", and "TTS" you may need to find the library and install it manually. Often you would download a .zip file containing the .h and .cpp files and use Sketch -> Include Library -> Add .ZIP Library to install it.

It looks like the library for "TTS.h", "english.h", and "sound.h" is:

Click on the "Code" button and select "Download ZIP".

True. I was saying what the IDE does when installing a new library, not what is or is not possible.

Sorry, but again wrong
As stated above, the library can contains a several .h files - and it all will be installed in one common directory.

When IDE installing a new library, it doesn't created a new folder itself - it only copied the ready-to-go folder structure from the library archive.

and the end result is the same... new library folder. Unless, of course, you have multiple instances of the IDE active and then you get a completely random folder name which does NOT match the name in the library archive.

What is the point of all your nit-picking?

Your statement in the post#3 misleads the OP. But since he left the branch a long time ago, I don't see the point in arguing further...

ploink

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.