Linker error when trying to use functions in separate header and source files It is my first day of working with Arduino (it is a Due). I want to read from TMP102 through I2C. I wrote some code and it works just fine. But now I want to separate the TMP102 code from the main source code. But I get linker error:


  sketch_jun15a.ino.cpp.o: In function setup':
  C:\Users\Saeid\Desktop\sketch_jun15a/sketch_jun15a.ino:62: undefined
  reference totmp102_allocate(int)' collect2.exe: error: ld returned 1
  exit status


My code is at the moment consist of 3 files:

tmp102.h

#ifndef TMP102_H
#define TMP102_H

//TP102 sensor struct
typedef struct TMP102 { 
  int   address;
  float celsius;
  float fahrenheit;    
} tmp102_t;

tmp102_t* tmp102_allocate(int address);

void tmp102_init_struct(tmp102_t * t, int address);

#endif


tmp102.c

#include "tmp102.h"

tmp102_t* tmp102_allocate(int address) {
  tmp102_t* t = (tmp102_t*) malloc(sizeof(tmp102_t));
  if(t) { //allocation success
    tmp102_init_struct(t, address);
    return t;
  } else {
    return 0; //null pointer
  }
}

void tmp102_init_struct(tmp102_t* t, int address) {
  t-&gt;address = address;
  t-&gt;celsius = 0.0;
  t-&gt;fahrenheit = 0.0;  
}


and here is the setup function of the sketch:

#include "tmp102.h"
void setup(){

  //setup TP102 sensors
  tp_1 = tmp102_allocate(72); //error is here
  tempSensors[0] = tp_1;

  //rest of stuff...

}


I think there is no mistake in the code. There is also no option in the Arduino IDE so that I can meddle with the compiler/linker options.

All the files are inside the sketch folder. I created these files using the drop down button in the IDE...
This is talked about here.  Evidently, the C++ compiler changes names of *.c files when compiling.  Try changing the names of your files from *.c to *.cpp so that the C++ compiler does not change the names. Then recompile.