I2C – Cannot find smbus_* functions

Recently I have been writing some userspace applications on x86 to interact with some i2c devices when I noticed that gcc was having some difficulties finding the i2c_smbus_* symbols.

The error is something like this:

cookie.c:23:6: warning: implicit declaration of function 'i2c_smbus_write_byte';

This was curious to me since both packages libi2c-dev and i2c-tools were installed and my header contained.

#include "linux/i2c-dev.h"
#include "linux/i2c.h"

Digging a bit more it seems that these are linux kernel headers and do not actually contain the i2c_smbus_* function declarations even though in the past, they did. This makes sense since it seems dubious that userspace code should take a dependency on kernel headers.

So where are the smbus functions declared? Looking through the libi2c-dev package via dpkg -L libi2c-dev, I found the following header:

/usr/include/i2c/smbus.h which contains all the declarations.

Adding:

#include "i2c/smbus.h"

Resolved my issues, however it seems that if I remove the linux/i2c-dev.h header from the include list, I cannot resolve the symbol I2C_SLAVE.

So this brings about the weird conclusion that we still have a dependency on a kernel header and the final include list becomes:

#include "i2c/smbus.h"
#include "linux/i2c-dev.h"

Also make sure to link your application against libi2c.so via -li2c

Hope this helps!

Leave a Reply