1

I am having trouble finding a way to linking the czmq library when using CMake. I can compile code using the czmq library with gcc myprog.c -lczmq just fine.

My project structure looks like this:

src/...
include/...
build/...
CMakeLists.txt

The src/ folder contains the .c files and corresponding .h files. The include/ folder contains the library header.

My current CmakeLists.txt looks like this:

cmake_minimum_required(VERSION 3.10)

project(mylib)
set(CMAKE_BUILD_TYPE Release)

#Header
include_directories(include)

#Src files, GLOB allows for wildcard additions
file(GLOB SOURCES "src/*c")

#Generate the shared library from the sources
add_library(mylib SHARED ${SOURCES})

#Set location for the library installation
install(TARGETS mylib DESTINATION .)
ZeppRock
  • 988
  • 1
  • 10
  • 22
  • That duplicate didn't answer my question as I still have to add the `-lczmq` flag when I compile code that uses the library created. – ZeppRock Aug 29 '19 at 06:11

1 Answers1

0

Set the CMAKE_EXE_LINKER_FLAGS to whatever options you want the linker to use:

SET(GCC_LINK_FLAGS "-lczmq")

then append the flags to the CMake variables:

SET(CMAKE_EXE_LINKER_FLAGS  "${CMAKE_EXE_LINKER_FLAGS} ${GCC_COMPILE_FLAGS}")
fugiefire
  • 318
  • 1
  • 8
  • Variable `CMAKE_EXE_LINKER_FLAGS` is a **wrong place** for `-l` flags: the content of this variable appears **before** the object file(s) in the linker's command line, while correct order of `-l` flags is **after** the object file(s). Command `target_link_libraries` should be used instead of `CMAKE_EXE_LINKER_FLAGS`. – Tsyvarev Aug 28 '19 at 16:10