[SOLVED] I2CMaster Lib undefined reference

Hi,

I am using an Arduino Nano.

I have just installed I2CMaster from Peter Fleury : http://homepage.hispeed.ch/peterfleury/i2cmaster.zip

I managed to install it properly (https://forum.arduino.cc/index.php?topic=571741.0) but when I use a function from this lib I have the “undefined reference” to every functions I use.

ex:

#include <i2cmaster.h>

void setup() {
  Serial.begin(115200);
  i2c_init();
  while (!Serial);
  //PORTC = (1 << PORTC4) | (1 << PORTC5); //enable pullups
  Serial.print("BattMon 1.0 ");
  //i2c_stop();
}

void loop() {
 
}

Error :

C:\Users\tagadac\AppData\Local\Temp\ccOy0OiH.ltrans0.ltrans.o: In function `setup':

c:\Users\tagadac\Documents\Arduino\BQ2060Av1/BQ2060Av1.ino:5: undefined reference to `i2c_init()'

collect2.exe: error: ld returned 1 exit status

exit status 1
[Error] Exit with code=1

It’s pretty confusing… I tried to rename “twimaster.c” to “i2cmaster.c” but (obviously) same error.

Thank you for your help.

I believe the correct form is using capital letters.

I2C_Init();

Here you can find some information about your library. Good luck!

Due to the file extension of twimaster.c, this code is compiled as C. But the Arduino IDE’s .ino sketch files are actually compiled as C++, after some minor preprocessing (mostly just generating function prototypes). so you must wrap the #include directive in extern “C” {}:

extern "C" {
#include <i2cmaster.h>
}

void setup() {
  Serial.begin(115200);
  i2c_init();
  while (!Serial);
  //PORTC = (1 << PORTC4) | (1 << PORTC5); //enable pullups
  Serial.print("BattMon 1.0 ");
  //i2c_stop();
}

void loop() {
 
}

Thank you for your help.

pati130013:
I believe the correct form is using capital letters.

No it’s not in capital letters, see doc here.

pert:
Due to the file extension of twimaster.c, this code is compiled as C. But the Arduino IDE’s .ino sketch files are actually compiled as C++, after some minor preprocessing (mostly just generating function prototypes). so you must wrap the #include directive in extern “C” {}:

extern "C" {

#include <i2cmaster.h>
}

void setup() {
  Serial.begin(115200);
  i2c_init();
  while (!Serial);
  //PORTC = (1 << PORTC4) | (1 << PORTC5); //enable pullups
  Serial.print("BattMon 1.0 ");
  //i2c_stop();
}

void loop() {

}

Puuuuurrrrrrfect ! This works like a charm ! I did not know this was possible with includes.

Thank you !

I'm glad to hear it's working now.

Perhaps the best practice would be for the library's header file to be wrapped in:

#ifdef __cplusplus
extern "C" {
#endif

// C code here

#ifdef __cplusplus
}
#endif

but wrapping the #include directive accomplishes the same thing, without needing to modify the library files at all.

Enjoy!
Per