【问题标题】:Undefined symbols when building libuv构建libuv时未定义的符号
【发布时间】:2016-10-27 14:50:02
【问题描述】:

我需要在我的库中使用 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 是我的单元测试可执行文件。

我认为我没有链接到某些东西,只是不知道是什么。有什么线索吗?

【问题讨论】:

  • 猜测一下——pthread 库。请注意,顺序很重要,您应该在使用它的库之后链接到 pthread
  • 你说订单很重要?
  • 我会使用 include(FindThreads),而不是 find(Threads REQUIRED) 并将其放在 SET 命令之前
  • @ruipacheco 当然链接顺序很重要。 stackoverflow.com/questions/45135/…

标签: c++ macos cmake pthreads libuv


【解决方案1】:

您好,我刚刚遇到了同样的问题,似乎 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)

【讨论】:

    猜你喜欢
    • 2014-10-14
    • 2015-03-18
    • 1970-01-01
    • 2021-07-10
    • 2014-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多