【问题标题】:CMake - How to add lpthread to command line [duplicate]CMake - 如何将 lpthread 添加到命令行 [重复]
【发布时间】:2020-09-10 12:11:28
【问题描述】:

这是我的 CMakeLists.txt:

if(WIN32)
    if(MSVC)
        list(APPEND PRIVATE_SOURCES AuthServer.rc)
    endif()
endif()

add_executable(AuthServer
        AuthServer.cpp
        ${PRIVATE_SOURCES}
        ../Database/Implementation/LoginDatabase.cpp
        ../Database/Implementation/LoginDatabase.h
        ../Database/Implementation/LoginDatabase/Accounts.h
        ../Database/Implementation/LoginDatabase/AccountRoles.h
        ../Database/Implementation/LoginDatabase/Accounts.cpp ../Database/Implementation/LoginDatabase/AccountRoles.cpp ../Memory/LoginMemory.h ../Memory/LoginMemory.cpp)
target_include_directories(AuthServer PUBLIC ../Database ../Database/Implementation/LoginDatabase/)
target_link_libraries(AuthServer LINK_PUBLIC Common)

if(NOT EXISTS ${CMAKE_BINARY_DIR}/bin/configs/)
    message(STATUS "Configs folder will be created: ${CMAKE_BINARY_DIR}/bin/configs")
    file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/bin/configs/)
endif()

if(NOT EXISTS ${CMAKE_BINARY_DIR}/bin/configs/AuthServer.conf)
message(STATUS "New AuthServer Config file in will be installed in: ${CMAKE_BINARY_DIR}/bin/configs")
add_custom_command(
        TARGET AuthServer POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy
        ${CMAKE_CURRENT_SOURCE_DIR}/AuthServer.conf.dist
        ${CMAKE_BINARY_DIR}/bin/configs/AuthServer.conf)
endif()

当我尝试构建时,我在链接阶段收到错误:

[ 75%] Linking CXX executable ../../../bin/AuthServer
/usr/bin/ld: CMakeFiles/AuthServer.dir/AuthServer.cpp.o: undefined reference to symbol 'pthread_condattr_setclock@@GLIBC_2.3.3'
//lib/x86_64-linux-gnu/libpthread.so.0: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
Source/Core/AuthServer/CMakeFiles/AuthServer.dir/build.make:165: recipe for target 'bin/AuthServer' failed
make[3]: *** [bin/AuthServer] Error 1
CMakeFiles/Makefile2:288: recipe for target 'Source/Core/AuthServer/CMakeFiles/AuthServer.dir/all' failed
make[2]: *** [Source/Core/AuthServer/CMakeFiles/AuthServer.dir/all] Error 2
CMakeFiles/Makefile2:295: recipe for target 'Source/Core/AuthServer/CMakeFiles/AuthServer.dir/rule' failed
make[1]: *** [Source/Core/AuthServer/CMakeFiles/AuthServer.dir/rule] Error 2
Makefile:210: recipe for target 'AuthServer' failed
make: *** [AuthServer] Error 2

我做了一个研究,看来我必须将lpthread这个添加到gcc的命令行中。如何在 CMakeLists 中实现这一点?

【问题讨论】:

  • LINK_PUBLIC 是旧接口,只需使用PUBLIC

标签: c++ cmake


【解决方案1】:

也许是这样的:

target_link_libraries(
    AuthServer
    pthread
)

?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-31
    • 2011-08-25
    • 1970-01-01
    • 2016-06-08
    相关资源
    最近更新 更多