How do I link to the locally installed librdkafka in my own projects CMake?

I have installed rdkafka locally on my machine via the apt package manager as instructed in their documentation. Now I want to link to that library with CMake so I can use it.

I am currently trying it like this:

find_package(PkgConfig REQUIRED)
pkg_check_modules(rdkafka REQUIRED rdkafka)
target_include_directories(myTarget PUBLIC ${RDKAFKA_INCLUDE_DIRS})
target_link_libraries(myTarget PUBLIC ${RDKAFKA_LDFLAGS})

It finds the rdkafka library but when trying to build the target it still has undefined references. It does work with the glib library though.

EDIT: This is the output when configuring CMake:

-- Checking for module 'rdkafka'
-- Found rdkafka, version 1.8.0
-- Configuring done
-- Generating done
-- Build files have been written to:

Starting the build leads to this:

/usr/bin/ld: CMakeFiles/ in function `g_autoptr_cleanup_generic_gfree':
opc_kafka_bridge.c:(.text+0x6c6): undefined reference to `g_free'
/usr/bin/ld: CMakeFiles/ in function `glib_autoptr_clear_GError':
opc_kafka_bridge.c:(.text+0x6e8): undefined reference to `g_error_free'
/usr/bin/ld: CMakeFiles/ in function `glib_autoptr_clear_GKeyFile':
opc_kafka_bridge.c:(.text+0x726): undefined reference to `g_key_file_unref'
/usr/bin/ld: CMakeFiles/ in function `load_config_group':
opc_kafka_bridge.c:(.text+0x7a8): undefined reference to `g_key_file_get_keys'
/usr/bin/ld: opc_kafka_bridge.c:(.text+0x7eb): undefined reference to `g_log'
/usr/bin/ld: opc_kafka_bridge.c:(.text+0x822): undefined reference to `g_key_file_get_string'
/usr/bin/ld: opc_kafka_bridge.c:(.text+0x861): undefined reference to `g_log'
/usr/bin/ld: opc_kafka_bridge.c:(.text+0x8b9): undefined reference to `g_log'
/usr/bin/ld: CMakeFiles/ in function `dr_msg_cb':
opc_kafka_bridge.c:(.text+0x95b): undefined reference to `g_log'
/usr/bin/ld: CMakeFiles/ in function `sendKafkaMessage':
opc_kafka_bridge.c:(.text+0x9a4): undefined reference to `g_key_file_new'
/usr/bin/ld: opc_kafka_bridge.c:(.text+0x9d0): undefined reference to `g_key_file_load_from_file'
/usr/bin/ld: opc_kafka_bridge.c:(.text+0xa00): undefined reference to `g_log'
/usr/bin/ld: opc_kafka_bridge.c:(.text+0xaa0): undefined reference to `g_log'
/usr/bin/ld: opc_kafka_bridge.c:(.text+0xbc5): undefined reference to `g_log'
/usr/bin/ld: opc_kafka_bridge.c:(.text+0xc03): undefined reference to `g_log'
/usr/bin/ld: opc_kafka_bridge.c:(.text+0xc4e): undefined reference to `g_log'
/usr/bin/ld: CMakeFiles/(.text+0xca4): more undefined references to `g_log' follow
collect2: error: ld returned 1 exit status
gmake[2]: *** [CMakeFiles/ OPCKafkaBridge] Error 1
gmake[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/ Error 2
gmake: *** [Makefile:91: all] Error 2

I managed to get it to compile by creating a Makefile manually with the following contents:

CFLAGS=-Wall $(shell pkg-config --cflags glib-2.0 rdkafka)
LDLIBS=$(shell pkg-config --libs glib-2.0 rdkafka)

I hope someone can help me configure should there be a mistake with the way I am trying to link to the library or point me in the right direction if it's correct and the problem is something different.

4

1 Answer

librdkafka provides a CMake config file and pkg-config files, so you have 2 solutions:

find_package(RdKafka REQUIRED)
target_link_libraries(myTarget PUBLIC RdKafka::rdkafka)

or

find_package(PkgConfig REQUIRED)
pkg_check_modules(rdkafka REQUIRED IMPORTED_TARGET rdkafka)
target_link_libraries(myTarget PUBLIC PkgConfig::rdkafka)

if you want to link rdkafka++:

find_package(RdKafka REQUIRED)
target_link_libraries(myTarget PUBLIC RdKafka::rdkafka++)

or

find_package(PkgConfig REQUIRED)
pkg_check_modules(rdkafka++ REQUIRED IMPORTED_TARGET rdkafka++)
target_link_libraries(myTarget PUBLIC PkgConfig::rdkafka++)
1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like