1

I want to link librdkafka with my project using CMakeList.txt

CMakeList.txt contains

cmake_minimum_required(VERSION 3.10)

project(client2 VERSION 1.0.0)
set(EXPORT_TARGETS_NAME ${PROJECT_NAME}Targets)

set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -pthread -Wl,--no-undefined")
file(GLOB_RECURSE SOURCES src/*.cpp)

find_package(librdkafka)
add_executable(${PROJECT_NAME} ${SOURCES})

set_target_properties(${PROJECT_NAME} PROPERTIES DEBUG_POSTFIX "${CMAKE_DEBUG_POSTFIX}")

target_link_libraries(${PROJECT_NAME} librdkafka)

install(TARGETS ${PROJECT_NAME}
    RUNTIME DESTINATION tests/${PROJECT_NAME} COMPONENT tests

But i get next error:

[build]   Could not find a package configuration file provided by "librdkafka" with any of the following names:
[build] 
[build]     librdkafkaConfig.cmake
[build]     librdkafka-config.cmake
[build] 
[build]   Add the installation prefix of "librdkafka" to CMAKE_PREFIX_PATH or set
[build]   "librdkafka_DIR" to a directory containing one of the above files.  If
[build]   "librdkafka" provides a separate development package or SDK, be sure it has
[build]   been installed.

And I also get "can't find -llibrdkafka"

Please tell me the right way to do what I want

Enkidu
  • 43
  • 6
  • Try with `find_package(RdKafka)` and link with `RdKafka::rdkafka++` – Guillaume Racicot Mar 14 '22 at 15:01
  • @Guillaume Racicot Now i get `CMake Error at CMakeLists.txt:23 (add_executable):Target "client2" links to target "RdKafka::rdkafka++" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing?` – Enkidu Mar 14 '22 at 15:11
  • Hmm, I would have to study the CMake script to search how it works – Guillaume Racicot Mar 14 '22 at 15:17

1 Answers1

0

Worked for me:

target_link_libraries(${PROJECT_NAME} PUBLIC rdkafka++)
Enkidu
  • 43
  • 6
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 14 '22 at 16:49