简体   繁体   English

CMake项目链接静态库

[英]CMake project linking a static library

I'm currently trying to include a static library into my cmake project. 我目前正在尝试在我的cmake项目中包括一个静态库。 When I build the project I get errors like this: 当我构建项目时,出现如下错误:

[ 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'

The CMakeLists.txt looks like this (RPlidar beeing the static library): 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)

I found out that those kind of errors appear when the linker option (-l) is placed in FRONT of the name of the file to be compiled and not AFTER it. 我发现,当将链接器选项(-l)放在要编译的文件名的前面而不是之后时,会出现这类错误。 But I have no idea how to resolve this in my cmake configuration. 但是我不知道如何在我的cmake配置中解决这个问题。

您必须链接pthread

target_link_libraries(MainApp PRIVATE pthread RPlidar)

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

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