如何在我自己的项目CMake中链接到本地安装的librdkafka?

bis0qfac  于 2023-02-11  发布在  Kafka
关注(0)|答案(1)|浏览(417)

我已经通过apt包管理器在我的机器上安装了rdkafka,现在我想用CMake链接到那个库,这样我就可以使用它了。
我目前正在尝试这样做:

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

它找到了rdkafka库,但是当试图构建目标时,它仍然有未定义的引用。

/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

我设法通过手动创建包含以下内容的Makefile来编译它:

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

我希望有人可以帮助我配置,如果有一个错误的方式,我试图链接到库或指出我在正确的方向,如果它是正确的,问题是不同的东西。

qoefvg9y

qoefvg9y1#

librdkafka提供了一个CMake配置文件和pkg-config文件,因此您有两种解决方案:

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

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

如果您要链接rdkafka++:

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

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

相关问题