构建libuv时未定义的符号

时间:2016-10-27 14:50:03

标签: c++ macos cmake pthreads libuv

我需要在我的库中使用libuv。由于我无法将它链接到两个静态库,所以我决定将libuv的源代码与我的代码一起包含在内。我有一个.cmake文件,可以下载libuv,检出正确的标签并将源文件添加到变量中:

include(DownloadProject.cmake)

download_project(PROJ               libuv
                 GIT_REPOSITORY     https://github.com/libuv/libuv.git
                 GIT_TAG            v1.10.0
                 ${UPDATE_DISCONNECTED_IF_AVAILABLE}
)
exec_program(COMMAND "./autogen.sh" WORKING_DIRECTORY ${libuv_SOURCE_DIR})
exec_program(COMMAND "./configure" WORKING_DIRECTORY ${libuv_SOURCE_DIR})

include_directories(${LIBUVDIR}/include ${LIBUVDIR}/src)
set(LIBUV_SOURCES
    ${LIBUVDIR}/include/uv.h
    ${LIBUVDIR}/include/tree.h
    ${LIBUVDIR}/include/uv-errno.h
    ${LIBUVDIR}/include/uv-threadpool.h
    ${LIBUVDIR}/include/uv-version.h
    ${LIBUVDIR}/src/fs-poll.c
    ${LIBUVDIR}/src/heap-inl.h
    ${LIBUVDIR}/src/inet.c
    ${LIBUVDIR}/src/queue.h
    ${LIBUVDIR}/src/threadpool.c
    ${LIBUVDIR}/src/uv-common.c
    ${LIBUVDIR}/src/uv-common.h
    ${LIBUVDIR}/src/version.c
    )

然后我添加了libuv源文件列表到我的项目的源文件列表,并链接库及其依赖项:

include(libuv.cmake)

# Build library
set(SOURCE_FILES
    <my sources>
  ${LIBUV_SOURCES})
add_library(databaseclient STATIC ${SOURCE_FILES})
set(CMAKE_THREAD_PREFER_PTHREAD 1)
set(THREADS_PREFER_PTHREAD_FLAG 1)
include(FindThreads)
target_link_libraries(databaseclient PUBLIC Threads::Threads)

但是当我运行make时,我收到以下错误:

Undefined symbols for architecture x86_64:
  "_pthread_barrier_destroy", referenced from:
      _uv_barrier_destroy in libdatabaseclient.a(thread.c.o)
  "_pthread_barrier_init", referenced from:
      _uv_barrier_init in libdatabaseclient.a(thread.c.o)
  "_pthread_barrier_wait", referenced from:
      _uv_barrier_wait in libdatabaseclient.a(thread.c.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [tests/unit_tests] Error 1
make[1]: *** [tests/CMakeFiles/unit_tests.dir/all] Error 2
make: *** [all] Error 2

unit_tests是我的单元测试可执行文件。

我认为我没有与之相关的东西,只是不知道是什么。有线索吗?

1 个答案:

答案 0 :(得分:3)

您好我刚遇到同样的问题,似乎pthread_barrier原语并未在MacOS上实现,尽管它声称符合POSIX标准。 LibUV使用其他线程原语实现它自己的版本。如果您将${LIBUVDIR}/src/unix/pthread-barrier.c添加到libuv c文件列表中,它应该正确链接。

以下是我在CMakeLists.txt中的libuv

set(LIBUVDIR libuv)

include_directories(${LIBUVDIR}/include ${LIBUVDIR}/src)
set(SOURCES
  ${LIBUVDIR}/src/fs-poll.c
  ${LIBUVDIR}/src/inet.c
  ${LIBUVDIR}/src/threadpool.c
  ${LIBUVDIR}/src/uv-common.c
  ${LIBUVDIR}/src/version.c)

if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
  add_definitions(-D_GNU_SOURCE)
  set(SOURCES ${SOURCES}
    ${LIBUVDIR}/src/unix/linux-syscalls.c
    ${LIBUVDIR}/src/unix/linux-core.c
    ${LIBUVDIR}/src/unix/linux-inotify.c)
elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Darwin")
  add_definitions(-D_DARWIN_USE_64_BIT_INODE=1 -D_DARWIN_UNLIMITED_SELECT=1)
  set(SOURCES ${SOURCES}
    ${LIBUVDIR}/src/unix/darwin.c
    ${LIBUVDIR}/src/unix/darwin-proctitle.c
    ${LIBUVDIR}/src/unix/fsevents.c
    ${LIBUVDIR}/src/unix/kqueue.c
    ${LIBUVDIR}/src/unix/pthread-barrier.c
    ${LIBUVDIR}/src/unix/proctitle.c)
endif()

include_directories(${LIBUVDIR}/src/unix)
set(SOURCES ${SOURCES}
  ${LIBUVDIR}/src/unix/async.c
  ${LIBUVDIR}/src/unix/core.c
  ${LIBUVDIR}/src/unix/dl.c
  ${LIBUVDIR}/src/unix/fs.c
  ${LIBUVDIR}/src/unix/getaddrinfo.c
  ${LIBUVDIR}/src/unix/getnameinfo.c
  ${LIBUVDIR}/src/unix/loop-watcher.c
  ${LIBUVDIR}/src/unix/loop.c
  ${LIBUVDIR}/src/unix/pipe.c
  ${LIBUVDIR}/src/unix/poll.c
  ${LIBUVDIR}/src/unix/process.c
  ${LIBUVDIR}/src/unix/signal.c
  ${LIBUVDIR}/src/unix/stream.c
  ${LIBUVDIR}/src/unix/tcp.c
  ${LIBUVDIR}/src/unix/thread.c
  ${LIBUVDIR}/src/unix/timer.c
  ${LIBUVDIR}/src/unix/tty.c
  ${LIBUVDIR}/src/unix/udp.c)

add_library(uv STATIC ${SOURCES})

target_link_libraries(uv pthread)
相关问题