1

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

4
  • are you including tmp102.h in the sketch, before the setup?
    – Talk2
    Jun 23, 2016 at 11:34
  • @Talk2 Yes of course!
    – Dumbo
    Jun 23, 2016 at 11:35
  • Why are you not declaring the tmp102_t* tmp102_allocate(int address) in the tmp102.h?
    – Talk2
    Jun 23, 2016 at 11:40
  • @Talk2 Oh sorry I have it in my code, I accidentaly removed it when pasitng the code in the question. That is not the problem :(
    – Dumbo
    Jun 23, 2016 at 11:42

2 Answers 2

1

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.

1

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
4
  • Thanks a lot. I was thinking that it will be compiled by a C compiler not a C++. I just enabled the compile output and I see that it is indeed using arm-none-eabi-g++ to compile my code!
    – Dumbo
    Jun 23, 2016 at 14:30
  • 1
    Actually you are almost there. The c file is compiled as c but the header is being included in a c++ file. It is the reference to the external c function that is being mangled not the named in the c file, which is where the mismatch occurs. The solution given here is still the correct one though. This method tells the compiler that the functions referenced are in a c file not a c++ file and disables the name mangling for them.
    – Majenko
    Jun 23, 2016 at 14:52
  • I gave extern "C" to the header and it does not help. it says multiple definitions found. even it does not matter if I name the file c or cpp
    – Dumbo
    Jun 23, 2016 at 16:01
  • It compiles fine for me. Suggest you copy past the header file. Maybe your #defines arent right. And your souurce file is still .c, correct? Jun 23, 2016 at 22:52

Your Answer

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

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