【发布时间】:2021-01-01 12:59:13
【问题描述】:
由于我对 Cmake 的使用比较陌生,所以我真的不知道如何解决这个问题。在我的 C++ 代码中,我使用了来自 Linux i2c 库的函数,如下所示:
extern "C" {
#include <linux/i2c-dev.h>
#include <i2c/smbus.h>
}
#include <sys/ioctl.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdint.h>
bool cpi2c_writeRegister(uint8_t address, uint8_t subAddress, uint8_t data) {
return i2c_smbus_write_byte_data(address, subAddress, data) == 0;
}
我通常使用 CMakeLists 使用 Cmake 进行编译,但现在它不起作用,因为它声明:undefined reference to 'i2c_smbus_write_byte_data'。我尝试使用 G++ 进行动态链接:
g++ -std=c++11 -Wall -c main.cpp
g++ -std=c++11 -o Main main.o -li2c
这很好,没有问题。问题是我真的需要用 CMakeLists 编译,所以你知道如何实现这一点吗? Cmake默认找不到包(因为它没有.config-file),我不知道i2c/smbus.h的函数是在哪里定义的。
感谢您的帮助!
【问题讨论】:
-
请显示(添加到问题帖子)
CMakeLists.txt用于构建项目的脚本。 -
target_link_libraries https://cmake.org/cmake/help/v3.17/command/target_link_libraries.html
-
@drescherjm 谢谢,问题解决了
标签: c++ cmake linker shared-libraries i2c