10

After updating to Ubuntu 18.04 I can't compile my Qt application.

The following error occurs:

undefined reference to `i2c_smbus_read_word_data(int, unsigned char)

As I understood, i2c_smbus_read_word_data is now defined not in linux/i2c-dev.h, but in dynamic library /usr/lib/x86_64-linux-gnu/libi2c.so.

I tryed to link dynamically:

-li2c

and statically:

/usr/lib/x86_64-linux-gnu/libi2c.a

But I still have compillation error

UPD: libi2c-dev, libi2c0 and i2c-tools packages are installed.

vitperov
  • 1,347
  • 17
  • 20
  • 2
    Possible duplicate of [Why are i2c\_smbus function not available? (I2C – Embedded Linux)](https://stackoverflow.com/questions/25159064/why-are-i2c-smbus-function-not-available-i2c-embedded-linux) – 273K May 03 '18 at 11:47
  • 1
    No this is a new thing, they have reworked how I2C tools are created to avoid that issue mention in that question, that there are two identically named kernel space and user space includes, unfortunately not all guides are up to date. – r_ahlskog Jul 12 '18 at 06:11
  • But what exact problem do you have? Looks like you are considering only one possible solution, but possibly the are other solutions. – user1742529 Apr 11 '21 at 16:11

1 Answers1

23

The smbus include is not C++ "ready" as most C headers for general use are, so it does not have an extern "C" declaration which means the C++ compiler mangles the names and the linking fails.

I beat my head against this for a few hours before I had an accidental insight. I fixed it by wrapping the includes in an extern "C" block and now my program links again.

extern "C" {
    #include <linux/i2c-dev.h>
    #include <i2c/smbus.h>
}
r_ahlskog
  • 1,916
  • 1
  • 17
  • 26