0

I am trying to include NATS C client as a dependency to my project. I prefer to link statically as my project is quite small and works as a standalone service, so shipping one single executable is more convenient.

ExternalProject_Add seems the most promising for its compatibility with a variety of build systems. But I get link errors when building with this approach:

3rd_party/nats/build/src/libnats_static.a(mutex.c.o): In function `natsMutex_Create':
mutex.c:(.text+0x36): undefined reference to `pthread_mutexattr_init'    
mutex.c:(.text+0x4b): undefined reference to `pthread_mutexattr_settype'
mutex.c:(.text+0xc0): undefined reference to `pthread_mutexattr_destroy'
mutex.c:(.text+0x19d): undefined reference to `pthread_mutexattr_destroy'
3rd_party/nats/build/src/libnats_static.a(mutex.c.o): In function `natsMutex_TryLock':
mutex.c:(.text+0x1b5): undefined reference to `pthread_mutex_trylock'
...

The weird part is when I changed the link method from static to shared, the build succeeds and the program works as expected. The related codes are included in the CMakeLists.txt.

The source code that produces the error above can be fetched by git clone https://github.com/onichandame/nats-epics.git --branch question --depth 1

The development environment is CentOS 8. I made a docker image for this environment onichandame/docker-dev

Kevin
  • 16,549
  • 8
  • 60
  • 74
xiao
  • 133
  • 1
  • 1
  • 9
  • Welcome to SO. Please tate the [tour] and read [ask] and post an [mcve]. You cannot expect the community to pull your entire repo to help you with this problem. That said, based on your post, you may not have a static library to link to. – jwdonahue Jun 19 '20 at 04:45
  • Welcome to SO. Please tate the [tour] and read [ask] and post an [mcve]. You cannot expect the community to pull your entire repo to help you with this problem. That said, based on your post, you may not have a static library to link to. – jwdonahue Jun 19 '20 at 04:45

1 Answers1

1

When you are linking it with the static NATS library, you also need to link to the dependencies of the NATS library. In this case, it looks like it may just be the pthread library. CMake has a cool way of linking to the pthread library (for CMake 3.1.0 or newer):

set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)

add_executable(myexe ${SourceFiles})
target_link_libraries(myexe PRIVATE Threads::Threads)

For CMake versions 2.8.12 or newer, here is how you could do it:

find_package(Threads REQUIRED)

add_executable(myexe2 ${SourceFiles})
target_link_libraries(myexe2 PRIVATE ${CMAKE_THREAD_LIBS_INIT})
  • Thanks! Your comment really dragged me out from the mud! In addition to `Thread` lib, `OpenSSL` is also required. – xiao Jun 19 '20 at 04:50
  • 1
    If it solved your problem, can you mark it as the answer? – Anthony Smith Jun 19 '20 at 04:51
  • Sorry for the delay in accepting the answer. It is my first conversation on StackOverflow so I still have a lot to learn. :P – xiao Jun 19 '20 at 05:06