0

I am writing a project in C using the ZeroMQ library. I need to use a static library. CMakeLists.txt:

cmake_minimum_required(VERSION 3.17)

project(client C CXX)

set(CMAKE_C_STANDARD 99)

find_library(czmq_location NAMES libczmq.a)
message(STATUS ${czmq_location})

add_library(czmq STATIC IMPORTED)
set_target_properties(czmq PROPERTIES IMPORTED_LOCATION ${czmq_location})

add_executable(client client.c ft_lib_c/clib.c ft_lib_c/clib.h ft_lib_c/itoa.c ft_lib_c/itoa.h)

target_link_libraries(client czmq  )

However, when building, I get a lot of errors -_-:

CMakeFiles/client.dir/client.c.o: In function `main':
/home/neleps/MMS_radar_projects/master/client/client.c:18: undefined reference to `zmq_ctx_new'
/home/neleps/MMS_radar_projects/master/client/client.c:20: undefined reference to `zmq_socket'
/home/neleps/MMS_radar_projects/master/client/client.c:22: undefined reference to `zmq_connect'
....

That is, the functions from the library are not visible . What am I doing wrong?

Thank you in advance for your help :)

neleps
  • 1
  • 1
    Are you sure CMake found the library, i.e. does `message(STATUS ${czmq_location})` print the location where the .a file is located? – Corristo Feb 27 '21 at 01:37
  • Functions `zmq_ctx_new` and other a part of `zmq` library, so you need to link with it. Since you use **static** libraries, linking with `czmq` doesn't automatically links with `zmq`. – Tsyvarev Feb 27 '21 at 13:16
  • @Corristo: In case `find_library` fails to find a library, the variable `czmq_location` won't be empty but contain value `czmq_location-NOTFOUND`. With such value CMake will emit an error during the configuration. Since the project has been configured successfully, not finding the library is not a case. – Tsyvarev Feb 27 '21 at 13:19
  • @Tsyvarev IIRC, only the most recent versions of CMake warn when IMPORTED_LOCATION or the config-specific equivalent point to files that don't exist. Before that CMake would just pass `czqm_location-NOTFOUND` to the linker, which will obviously fail at build time. Depending on how many 'undefined reference' warnings OP got it might be easy to miss the linker warning about not finding the library. – Corristo Feb 27 '21 at 16:10
  • Yes, I am aware that not all CMake versions emit an error on `czqm_location-NOTFOUND` used in `target_link_libraries` call. But would CMake passes `czqm_location-NOTFOUND` to the linker, the error will be about not finding the library, not "undefined reference". – Tsyvarev Feb 27 '21 at 16:51
  • @Tsyvarev In my experience you get both errors. First the linker complains about not finding that library, but then tries to link anyway. Linking then fails due to undefined references and you get these message as well. I myself have missed the first warning about trying to link a non-existent library in the sea of undefined reference warnings before, which is why I wouldn't rule out that this is what happened here, too. – Corristo Feb 27 '21 at 20:03
  • @Tsyvarev, thank you! But I have a new problem - the functions in the libzmq.a library are written in C++ (although the zmq is C/C++ library). In this regard, the code does not compile, giving me a lot of errors: "undefined reference to..." What should I do? (i need only c-project, c++ is not my way) – neleps Feb 28 '21 at 10:10
  • If your code uses functions from `zmq`, then you need to link with `zmq`. If your code uses functions from `czmq`, then you need to link with **both libraries**, `czmq` and `zmq`. It seems your case is the latter one. What **exact** errors do you got when follow it? – Tsyvarev Feb 28 '21 at 10:32
  • @Tsyvarev udefender refrence to symbol «pthread_setname_np@@GLIBC_2.12» //lib/x86_64-linux-gnu/libpthread.so.0: error adding symbols: DSO missing from command line – neleps Feb 28 '21 at 11:15
  • @Tsyvarev but if i add "set(CMAKE_C_FLAGS "-pthread")" i have more errors: /usr/lib/x86_64-linux-gnu/libzmq.a(src_libzmq_la-ctx.o): In function `void std::__cxx11::basic_string, std::allocator >::_M_construct(char const*, char const*, std::forward_iterator_tag) [clone .isra.108]': (.text+0x89): undefined reference to `std::__cxx11::basic_string....... – neleps Feb 28 '21 at 11:20
  • It seems your `libzmq` library is not compatible with the compiler you use. See e.g. [that question](https://stackoverflow.com/questions/33394934/converting-std-cxx11string-to-stdstring) about `__cxx11` in the "undefined reference" errors. – Tsyvarev Feb 28 '21 at 11:28

1 Answers1

0

This worked well for me:

target_link_libraries(server PRIVATE czmq zmq)

The complete CMakeLists.txt:

cmake_minimum_required(VERSION 3.15)
set(CMAKE_C_STANDARD 11)
project(client C)

find_library(ZeroMQ czmq REQUIRED)

add_executable(client)
target_link_libraries(client PRIVATE czmq zmq)

target_include_directories(client PRIVATE include)
target_sources(client PRIVATE 
    src/client.c)
James Risner
  • 5,451
  • 11
  • 25
  • 47
goush
  • 13
  • 1
  • 3