Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
123 views
in Technique[技术] by (71.8m points)

c++ - how to link .so files with CMake


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The error message means that you have to add pthread to the list of linked libraries. In target_link_libraries you only list the library names without path, lib prefix and file extension:

cmake_minimum_required(VERSION 3.5)    
set(CMAKE_CXX_STANDARD 11)      
project(test C CXX)

find_package(Threads REQUIRED)

add_executable(${PROJECT_NAME} main.cpp)

target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_SOURCE_DIR}/lib)     
target_link_libraries(test A B Threads::Threads)

You can add paths with target_link_directories:

cmake_minimum_required(VERSION 3.5)    
set(CMAKE_CXX_STANDARD 11)      
project(test C CXX)

find_package(ThreadsREQUIRED)

add_executable(${PROJECT_NAME} main.cpp)

target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_SOURCE_DIR}/lib)     
target_link_directories(test PRIVATE ${CMAKE_SOURCE_DIR})
target_link_libraries(test PRIVATE A B Threads::Threads)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...