繁体   English   中英

CMake库无法正确链接

[英]CMake Library not linking correctly

我试图编写一个使用BNO055驱动程序的程序 我尝试了许多不同的方法将此库链接到我的项目。

由于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.hbno055.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问题,到目前为止,我发现的所有解决方案都无济于事。

链接
Github回购专案/imc-server下的/imc-server码)
- 提交39a6196 ,尝试1
- 提交e64e7c8 ,尝试2
BNO055驱动程序

您将C标头包含在C ++源代码中,而没有将其包装到extern "C" { }块中。 驱动程序代码编译为C,因为CMake通过文件扩展名选择编译模式。

生成不带extern "C" {} .c和.cpp

将main.cpp更改为main.c,即可摆脱此问题

set(SOURCE_FILES ${SOURCE_FILES}
    main.c)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM