undefined reference to `i2c_smbus_read_word_data(int, unsigned char)

不打扰是莪最后的温柔 提交于 2021-01-02 05:56:06

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/50154296/undefined-reference-to-i2c-smbus-read-word-dataint-unsigned-char

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!