user23292: 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->address = address;
  t->celsius = 0.0;
  t->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...

user20245: 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.

user19911: The reason you are getting the undefined reference to  `tmp102_allocate(int)' is because your c source file is being converted to a cpp file and the function name is being mangled to support overloading etc etc. You need to flag that the functions are c code with  extern "C" blocks. If you change your header file to  the following it should compile. Alternatively, if you change the extension of your source file to .cpp you won't have any issues with name mangling but your code will probably not work (malloc). Note that by checking if __cplusplus is defined you can use this header in both cpp and c projects.
tmp102.h
#ifndef TMP102_H
#define TMP102_H

#ifdef __cplusplus
  extern "C" {
#endif

//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);

#ifdef __cplusplus
  }
#endif

#endif


23292: <p>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:</p>

<blockquote>
  <p>sketch_jun15a.ino.cpp.o: In function <code>setup':
  C:\Users\Saeid\Desktop\sketch_jun15a/sketch_jun15a.ino:62: undefined
  reference to</code>tmp102_allocate(int)' collect2.exe: error: ld returned 1
  exit status</p>
</blockquote>

<p>My code is at the moment consist of 3 files:</p>

<p><strong>tmp102.h</strong></p>

<pre><code>#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
</code></pre>

<p><strong>tmp102.c</strong></p>

<pre><code>#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;  
}
</code></pre>

<p>and here is the setup function of the sketch:</p>

<pre><code>#include "tmp102.h"
void setup(){

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

  //rest of stuff...

}
</code></pre>

<p>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.</p>

<p>All the files are inside the sketch folder. I created these files using the drop down button in the IDE...</p>
 20245: <p>This is talked about <a href="http://forum.arduino.cc/index.php?topic=57691.0" rel="nofollow">here</a>.  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.</p>
 19911: <p>The reason you are getting the undefined reference to  `tmp102_allocate(int)' is because your c source file is being converted to a cpp file and the function name is being mangled to support overloading etc etc. You need to flag that the functions are c code with  extern "C" blocks. If you change your header file to  the following it should compile. Alternatively, if you change the extension of your source file to .cpp you won't have any issues with name mangling but your code will probably not work (malloc). Note that by checking if __cplusplus is defined you can use this header in both cpp and c projects.</p>

<p><strong>tmp102.h</strong></p>

<pre><code>#ifndef TMP102_H
#define TMP102_H

#ifdef __cplusplus
  extern "C" {
#endif

//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);

#ifdef __cplusplus
  }
#endif

#endif
</code></pre>
