【发布时间】:2015-09-11 03:46:53
【问题描述】:
我正在尝试编写一个使用BNO055 Driver 的程序。我尝试了许多不同的方法将这个库链接到我的项目。
由于 BNO055 驱动程序不附带任何构建系统设置或构建的库,因此我必须以某种方式包含该驱动程序。
对于以下每次尝试,我都会收到构建错误:
[100%] Linking CXX executable imc-server
CMakeFiles/imc-server.dir/main.cpp.o: In function `bno055_read_data()':
/home/noah/Documents/Inertial-Motion-Capture/imc-server/main.cpp:83: undefined reference to `bno055_init(bno055_t*)'
/home/noah/Documents/Inertial-Motion-Capture/imc-server/main.cpp:84: undefined reference to `bno055_set_power_mode(unsigned char)'
/home/noah/Documents/Inertial-Motion-Capture/imc-server/main.cpp:86: undefined reference to `bno055_set_operation_mode(unsigned char)'
/home/noah/Documents/Inertial-Motion-Capture/imc-server/main.cpp:91: undefined reference to `bno055_read_quaternion_wxyz(bno055_quaternion_t*)'
/home/noah/Documents/Inertial-Motion-Capture/imc-server/main.cpp:97: undefined reference to `bno055_read_linear_accel_xyz(bno055_linear_accel_t*)'
/home/noah/Documents/Inertial-Motion-Capture/imc-server/main.cpp:98: undefined reference to `bno055_convert_double_linear_accel_xyz_msq(bno055_linear_accel_double_t*)'
/home/noah/Documents/Inertial-Motion-Capture/imc-server/main.cpp:101: undefined reference to `bno055_set_power_mode(unsigned char)'
collect2: error: ld returned 1 exit status
make[2]: *** [imc-server] Error 1
make[1]: *** [CMakeFiles/imc-server.dir/all] Error 2
make: *** [all] Error 2
这是我通常在库未正确链接时看到的那种错误。
尝试 1 - 包括 BNO055 驱动源
我首先尝试使用一种简单的方法,只在我的可执行文件中包含驱动程序源(bno055.h 和bno055.c):
#CMakeLists.txt
set(SOURCE_FILES ${SOURCE_FILES}
${CMAKE_SOURCE_DIR}/bno055/bno055.h
${CMAKE_SOURCE_DIR}/bno055/bno055.c)
[...]
set(SOURCE_FILES ${SOURCE_FILES}
main.cpp)
add_executable(imc-server ${SOURCE_FILES})
尝试 2 - 从驱动程序源和链接构建库文件
后来我尝试为 BNO055 驱动程序构建一个库,然后将其链接到我的可执行文件:
#In BNO055 sub-directory CMakeLists.txt
add_library(bno055 ${CMAKE_CURRENT_SOURCE_DIR}/bno055.c)
target_include_directories(bno055 PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
#In Main CMakeLists.txt
add_subdirectory(bno055)
[...]
add_executable(imc-server main.cpp)
target_link_libraries(imc-server bno055)
这两次尝试都失败了,据我所知,它们应该正确链接。这让我相信要么
a) 我错误地链接了 BNO055 库
或
b) 我必须做一些特别的事情才能让 BNO055 驱动程序与我的程序一起工作
我查看了许多其他与我遇到的构建错误相关的 SO 问题,到目前为止,我找到的所有解决方案都没有帮助。
链接
Project Github Repo(/imc-server下的代码)
- Commit 39a6196, Attempt 1
- Commit e64e7c8, Attempt 2
BNO055 Driver
【问题讨论】:
-
您正在将 C 标头包含在没有
extern "C" { }包装器的 C++ 源代码中。我想,这就是原因。 -
@arrowd 成功了,谢谢!如果你愿意,把它作为这个问题的答案
标签: c++ c linker cmake linker-errors