0

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/OPCKafkaBridge.dir/opc_kafka_bridge.c.o: in function `g_autoptr_cleanup_generic_gfree':
opc_kafka_bridge.c:(.text+0x6c6): undefined reference to `g_free'
/usr/bin/ld: CMakeFiles/OPCKafkaBridge.dir/opc_kafka_bridge.c.o: in function `glib_autoptr_clear_GError':
opc_kafka_bridge.c:(.text+0x6e8): undefined reference to `g_error_free'
/usr/bin/ld: CMakeFiles/OPCKafkaBridge.dir/opc_kafka_bridge.c.o: in function `glib_autoptr_clear_GKeyFile':
opc_kafka_bridge.c:(.text+0x726): undefined reference to `g_key_file_unref'
/usr/bin/ld: CMakeFiles/OPCKafkaBridge.dir/opc_kafka_bridge.c.o: 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/OPCKafkaBridge.dir/opc_kafka_bridge.c.o: in function `dr_msg_cb':
opc_kafka_bridge.c:(.text+0x95b): undefined reference to `g_log'
/usr/bin/ld: CMakeFiles/OPCKafkaBridge.dir/opc_kafka_bridge.c.o: 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/OPCKafkaBridge.dir/opc_kafka_bridge.c.o:opc_kafka_bridge.c:(.text+0xca4): more undefined references to `g_log' follow
collect2: error: ld returned 1 exit status
gmake[2]: *** [CMakeFiles/OPCKafkaBridge.dir/build.make:98: OPCKafkaBridge] Error 1
gmake[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/OPCKafkaBridge.dir/all] 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.

Sebastian
  • 63
  • 4

1 Answers1

0

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++)
SpacePotatoes
  • 659
  • 5
  • 11
  • Sadly this leads to the same problem. When configuring CMake with the CMakeList.txt it reports it has found rdkafka and even lists its version but still fails the build. – Sebastian Feb 13 '23 at 16:24