warning: implicit declaration of function ‘i2c_smbus_write_byte_data’

とあるi2cを使うcコードでコンパイル警告とリンクエラーが出た。

コンパイル警告
warning: implicit declaration of function ‘i2c_smbus_write_byte_data’
warning: implicit declaration of function ‘i2c_smbus_read_byte’

リンクエラー
undefined reference to `i2c_smbus_write_byte_data'
undefined reference to `i2c_smbus_read_byte'

Debian 8 jessieでは通るがDebian 10 busterではダメ。 どうやらi2c-devの仕様が変わった。

  • linux/i2c-dev.hlinux/i2c.h, i2c/smbus.h へ分割された
  • libi2c-dev が必要になった (-li2c)

fix

#code
+ #include <linux/i2c.h>
  #include <linux/i2c-dev.h>
+ #include <i2c/smbus.h>
#makefile
at24c.out: at24c.o
-    $(CC) $^ -lrt -o at24c.out
+    $(CC) $^ -lrt -li2c -o at24c.out

libi2c-dev in buster.. whats changed - Raspberry Pi Forums

linux - Getting linking error for i2c_smbus_read_byte() - Stack Overflow