我目前正在尝试将静态库包含到我的cmake项目中。当我构建项目时,我得到这样的错误:
[ 50%] Building CXX object src/CMakeFiles/MainApp.dir/main.cpp.o
[100%] Linking CXX executable MainApp
../../lib/librplidar_sdk.a(thread.o): In function `rp::hal::Thread::create(unsigned long (*)(void*), void*)':
thread.cpp:(.text+0x20): undefined reference to `pthread_create'
/../lib/librplidar_sdk.a(thread.o): In function `rp::hal::Thread::terminate()':
thread.cpp:(.text+0x48): undefined reference to `pthread_cancel'
CMakeLists.txt看起来像这样(RPlidar是静态库):
##################################################################
# RPLidar SDK #
##################################################################
add_library(RPlidar STATIC IMPORTED)
set_property(TARGET RPlidar PROPERTY IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/lib/librplidar_sdk.a)
set_property(TARGET RPlidar APPEND PROPERTY INTERFACE_INCLUDE_DIRECTORIES
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include/rplidar>
)
##################################################################
# Main Executable #
##################################################################
add_executable(MainApp main.cpp)
target_link_libraries(MainApp PRIVATE RPlidar)
我发现当链接器选项(-l)放在要编译的文件名的FRONT而不是它后面时会出现这种错误。但我不知道如何在我的cmake配置中解决这个问题。
答案 0 :(得分:2)
您必须关联pthread
:
target_link_libraries(MainApp PRIVATE pthread RPlidar)