如何解决libuv使用Cmake时链接报错undefined reference to `pthread_sigmask'的问题?

?当我们在Cmake中使用:

实例CmakeList在文章最后

target_link_libraries(smpHttp /home/ele/libuv/out/Debug/libuv.a)

这样链接libuv时,有时会报出这样的错误:

[build] /home/ele/libuv/out/Debug/libuv.a(process.o): In function `uv__process_child_init':
[build] /home/ele/libuv/out/../src/unix/process.c:402: undefined reference to `pthread_sigmask'
[build] /home/ele/libuv/out/Debug/libuv.a(signal.o): In function `uv__signal_global_init':
[build] /home/ele/libuv/out/../src/unix/signal.c:73: undefined reference to `pthread_atfork'
[build] /home/ele/libuv/out/Debug/libuv.a(signal.o): In function `uv__signal_block_and_lock':
[build] /home/ele/libuv/out/../src/unix/signal.c:146: undefined reference to `pthread_sigmask'
[build] /home/ele/libuv/out/Debug/libuv.a(signal.o): In function `uv__signal_unlock_and_unblock':
[build] /home/ele/libuv/out/../src/unix/signal.c:158: undefined reference to `pthread_sigmask'
[build] /home/ele/libuv/out/Debug/libuv.a(thread.o): In function `uv_barrier_init':
[build] /home/ele/libuv/out/../src/unix/thread.c:141: undefined reference to `pthread_barrier_init'
[build] /home/ele/libuv/out/Debug/libuv.a(thread.o): In function `uv_barrier_wait':
[build] /home/ele/libuv/out/../src/unix/thread.c:148: undefined reference to `pthread_barrier_wait'
[build] /home/ele/libuv/out/Debug/libuv.a(thread.o): In function `uv_barrier_destroy':
[build] /home/ele/libuv/out/../src/unix/thread.c:158: undefined reference to `pthread_barrier_destroy'
[build] /home/ele/libuv/out/Debug/libuv.a(thread.o): In function `uv_thread_create_ex':
[build] /home/ele/libuv/out/../src/unix/thread.c:253: undefined reference to `pthread_attr_setstacksize'
[build] /home/ele/libuv/out/../src/unix/thread.c:258: undefined reference to `pthread_create'
[build] /home/ele/libuv/out/Debug/libuv.a(thread.o): In function `uv_thread_join':
[build] /home/ele/libuv/out/../src/unix/thread.c:272: undefined reference to `pthread_join'
[build] /home/ele/libuv/out/Debug/libuv.a(thread.o): In function `uv_mutex_init_recursive':
[build] /home/ele/libuv/out/../src/unix/thread.c:308: undefined reference to `pthread_mutexattr_init'
[build] /home/ele/libuv/out/../src/unix/thread.c:311: undefined reference to `pthread_mutexattr_settype'
[build] /home/ele/libuv/out/../src/unix/thread.c:316: undefined reference to `pthread_mutexattr_destroy'
[build] /home/ele/libuv/out/Debug/libuv.a(thread.o): In function `uv_mutex_trylock':
[build] /home/ele/libuv/out/../src/unix/thread.c:338: undefined reference to `pthread_mutex_trylock'
[build] /home/ele/libuv/out/Debug/libuv.a(thread.o): In function `uv_rwlock_init':
[build] /home/ele/libuv/out/../src/unix/thread.c:356: undefined reference to `pthread_rwlock_init'
[build] /home/ele/libuv/out/Debug/libuv.a(thread.o): In function `uv_rwlock_destroy':
[build] /home/ele/libuv/out/../src/unix/thread.c:361: undefined reference to `pthread_rwlock_destroy'
[build] /home/ele/libuv/out/Debug/libuv.a(thread.o): In function `uv_rwlock_rdlock':
[build] /home/ele/libuv/out/../src/unix/thread.c:367: undefined reference to `pthread_rwlock_rdlock'
[build] /home/ele/libuv/out/Debug/libuv.a(thread.o): In function `uv_rwlock_tryrdlock':
[build] /home/ele/libuv/out/../src/unix/thread.c:375: undefined reference to `pthread_rwlock_tryrdlock'
[build] /home/ele/libuv/out/Debug/libuv.a(thread.o): In function `uv_rwlock_rdunlock':
[build] /home/ele/libuv/out/../src/unix/thread.c:387: undefined reference to `pthread_rwlock_unlock'
[build] /home/ele/libuv/out/Debug/libuv.a(thread.o): In function `uv_rwlock_wrlock':
[build] /home/ele/libuv/out/../src/unix/thread.c:393: undefined reference to `pthread_rwlock_wrlock'
[build] /home/ele/libuv/out/Debug/libuv.a(thread.o): In function `uv_rwlock_trywrlock':
[build] /home/ele/libuv/out/../src/unix/thread.c:401: undefined reference to `pthread_rwlock_trywrlock'

可以看出来问题在于没有链接pthread


?那么第一反应我添加这这样一行:

target_link_libraries(/home/ele/libuv/out/Debug/libuv.a pthread)

这样是不行的,因为:libuv.a不是本项目中生成的。

[cmake]   Cannot specify link libraries for target
[cmake]   "/home/ele/libuv/out/Debug/libuv.a" which is not built by this project.

?那么在最后链接上pthread呢?

target_LINK_LIbraries(smpHttp ${deps} pthread)

结果仍然是不可以的。


是libuv的问题吗? 到这里我是这样怀疑的。我重新编译了libuv仍然如此,我也去issue里面查找,没有类似问题。

?终于我发现了问题所在,我在最开始有这样一行代码:

target_link_libraries(Net /home/ele/libuv/out/Debug/libuv.a radix http_request_handles)

我在第一次链接libuv时没有链接pthread,所以在后面的smphttp添加pthread并不起效果。
改成这样就可以了:

target_link_libraries(Net /home/ele/libuv/out/Debug/libuv.a radix http_request_handles pthread)
?结论: 一定要在第一次链接libuv.a时就要同时链接pthread。

?出问题的CMakeLists.txt(现在是正确的):

cmake_minimum_required(VERSION 3.0.0)
project(smpHttp VERSION 0.1.0)

include(CTest)
enable_testing()

include_directories(/home/ele/libuv/include)
include_directories(./src)
include_directories(./src/radix_tree)
include_directories(./src/http_parser)
add_definitions("-Wall -g -fPIC")
add_library(radix src/radix_tree/radix_tree.c)
add_library(http_parser src/http_parser/http_parser.c)
add_library(http_request_handles src/http_request_handles.cpp)
add_library(Net src/Net.cpp)
target_link_libraries(Net /home/ele/libuv/out/Debug/libuv.a radix http_request_handles pthread)
add_library(RequestContext src/RequestContext.cpp)
add_library(uv_handles src/uv_handles.cpp)
target_link_libraries(uv_handles RequestContext http_parser radix) 

list(APPEND deps /home/ele/libuv/out/Debug/libuv.a radix http_parser http_request_handles Net RequestContext uv_handles)

add_executable(smpHttp test/main.cpp)
target_LINK_LIbraries(smpHttp ${deps})

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)

你可能感兴趣的:(遇到的问题s)