RobinW
RobinW

Reputation: 322

CMake project linking a static library

I'm currently trying to include a static library into my cmake project. 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):

##################################################################
#                          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. But I have no idea how to resolve this in my cmake configuration.

Upvotes: 0

Views: 975

Answers (1)

krisz
krisz

Reputation: 2695

You have to link pthread:

target_link_libraries(MainApp PRIVATE pthread RPlidar)

Upvotes: 3

Related Questions