I am trying to compile my project, but unfortunately get errors.

I created class and separated implementation and declaration (cpp file and h file)

Here they are


#ifndef SENSOR_H
#define SENSOR_H
#include <stdint.h>
#include "Arduino.h"
// Sensor types
enum SensorType {
class Sensor {
  // Variables declaration
    uint32_t sensorId;
    SensorType sensorType;
    float currentValue;
    bool isEnabled;
    float criticalLowerLevel;
    float criticalUpperLevel;
   // Methods declaration
    Sensor(uint32_t id,SensorType type,long currentValue,bool isEnabled,long lower,long upper);
    String toString();
    bool isCriticalLevelRised() {
      return (this->currentValue >= this->criticalUpperLevel || this->currentValue <= this->criticalLowerLevel);
    void enable(bool enable);
    void toggle();
    static String convertSensorTypeToString(SensorType sensorType);


And cpp file

#include "sensor.h"
  this->sensorId = 0;
  this->sensorType = UNKNOWN_SENSOR;
  this->currentValue = 0;
  this->isEnabled = false;
  this->criticalLowerLevel = 0;
  this->criticalUpperLevel = 0;
Sensor::Sensor(uint32_t id,SensorType type,long currentValue,bool isEnabled,long lower,long upper) {
  this->sensorId = id;
  this->sensorType = type;
  this->currentValue = currentValue;
  this->isEnabled = isEnabled;
  this->criticalLowerLevel = lower;
  this->criticalUpperLevel = upper;
void Sensor::enable(bool enable) { 
   this->isEnabled = enable;
void Sensor::toggle() {
String Sensor::toString()
  String response = "";
  response += F("Sensor id is ");
  response += this->sensorId;
  response += '\n';
  response += F("Sensor type is ");
  response += convertSensorTypeToString(this->sensorType);
  response += '\n';
  response += F("Sensor current value is ");
  response += this->currentValue;
  response += '\n';
  response += F("Sensor isEnabled is ");
  response += this->isEnabled;
  response += '\n';
  response += F("Sensor criticalLowerLevel is ");
  response += this->criticalLowerLevel;
  response += '\n';
  response += F("Sensor criticalLowerLevel is ");
  response += this->criticalUpperLevel;
  response += '\n';
  return response;
String Sensor::convertSensorTypeToString(SensorType sensorType) {
  switch (sensorType) {
      return F("UNKNOWN SENSOR");
    case ANY_SENSOR:
      return F("ANY_SENSOR");
    case WATER_SENSOR:
      return F("WATER_SENSOR");
      return F("TEMPERATURE_SENSOR");
      return F("VIBRATION_SENSOR");
      return F("HUMIDITY_SENSOR");
    case PIR_SENSOR:
      return F("PIR_SENSOR");
    case SOUND_SENSOR:
      return F("SOUND_SENSOR");
    case LIGHT_SENSOR:
      return F("LIGHT_SENSOR");
      return F("UNKNOWN TYPE");

And I got following errors

ketch/NodeIntercom.ino.cpp.o: In function `sendSensorMessageToMaster(RF24Mesh*, SensorDataMessage*, char, unsigned int, bool)':
/home/iron/Arduino/What'sHappening/NetworkMaster/additional_functions.h:48: undefined reference to `Sensor::toString()'
sketch/NodeIntercom.ino.cpp.o: In function `handleSetSensorDataRequest(RF24NetworkHeader*)':
/home/iron/Arduino/What'sHappening/NodeIntercom/NodeIntercom.ino:97: undefined reference to `Sensor::toggle()'
/home/iron/Arduino/What'sHappening/NodeIntercom/NodeIntercom.ino:89: undefined reference to `Sensor::enable(bool)'
sketch/NodeIntercom.ino.cpp.o: In function `SensorDataMessage':
/home/iron/Arduino/What'sHappening/NetworkMaster/common.h:40: undefined reference to `Sensor::Sensor()'
sketch/NodeIntercom.ino.cpp.o: In function `__static_initialization_and_destruction_0':
/home/iron/Arduino/What'sHappening/NodeIntercom/NodeIntercom.ino:35: undefined reference to `Sensor::Sensor(unsigned long, SensorType, long, bool, long, long)'
collect2: error: ld returned 1 exit status

What I am doing wrong ? Please help.


Yes main sketch compiles perfectly where this files are located.

But I don't want to duplicate common files in my all my arduino sketches withing the project, they are the same for whole project. Here my files location


So this files are only located in MasterNode directory

And I tried to include them like this

#include "/home/iron/Arduino/What'sHappening/NetworkMaster/common.h"
#include "/home/iron/Arduino/What'sHappening/NetworkMaster/additional_functions.h"

And in common.h I include sensor.h Also tried to include directly, but still the same

But headers files included as above works perfectly.

What is the right way to solve this problem, I don't want to duplicate this class in each folder whenever I change it.

If there is no way, maybe create symbolic link (I am using linux) ?


Just use symbolic links in the sketch directory and this works perfectly

  • 1
    where are these files located? in the sketch folder? in a library? Jan 25, 2016 at 16:48

2 Answers 2


The Arduino build preprocessing cannot locate the sensor source and header files if they are located in another sketch directory. The files needs to be moved to separate directory if shared (as a library).


The Sensor header is included with

#include <Sensor.h>

in the sketches that needs to use it.


There is nothing wrong with that class and its code (as far as I can tell) - it compiles perfectly fine.

What is probably wrong is that the IDE isn't picking up the code and compiling it in to your project.

Is this class part of the sketch, or is it a separate library that you are writing? If the latter you have to ensure that the IDE knows about it, and it can only look for it when you load the IDE, so if you haven't restarted the IDE since creating the library you must do that so it can find the new library.

  • Thank you for answer. I just created manually this class and want just to separate logic, I am not writing a library, just simple class. I think the problem is that this files are located inside another sketch directory MasterNode this is main node sketch. I have updated my question please see it Jan 25, 2016 at 17:55
  • 2
    You're trying to use files from one sketch in another sketch? That's not going to happen. It can't be done. If you want to share files between sketches then they must be as a library. There's no two ways about it. Create yourself a library and put your shared files into it.
    – Majenko
    Jan 25, 2016 at 18:18
  • Thank you for explanation, I have just used symbolic links and this works perfectly ! Jan 25, 2016 at 18:43
  • 2
    See my post about How the IDE organizes things.
    – Nick Gammon
    Jan 25, 2016 at 20:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.