0

I need to use libuv with my library. Since I cannot link it two static libraries I decided to include the source for libuv along with my code. I have a .cmake file that downloads libuv, checks out the right tag and adds the source files to a variable:

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
    )

etc.

I then add add the list of libuv source files to the list of source files for my project and link the library with its dependencies:

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)

But when I run make I get the following error:

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 is my unit tests executable.

I think there's something I'm not linking against, just don't know what. Any clues?

ruipacheco
  • 15,025
  • 19
  • 82
  • 138
  • at a guess - the pthread library. Note that order matters, and you should link to pthread after the libraries that use it – UKMonkey Oct 27 '16 at 14:53
  • Order matters, you say? – ruipacheco Oct 27 '16 at 14:55
  • I would use, instead of `include(FindThreads)`, `find(Threads REQUIRED)` and put it before the `SET` commands – Amadeus Oct 27 '16 at 14:56
  • @ruipacheco Of course linking order matters. http://stackoverflow.com/questions/45135/why-does-the-order-in-which-libraries-are-linked-sometimes-cause-errors-in-gcc – Antonio Oct 27 '16 at 14:57

1 Answers1

3

Hi I just ran into this same problem, it seems pthread_barrier primitives are not implemented on MacOS despite it's claims of POSIX compliance. LibUV implements it's own versions using other threading primitives. If you add ${LIBUVDIR}/src/unix/pthread-barrier.c to your list of libuv c files it should link correctly.

Here is what I have in CMakeLists.txt for 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)