C++ Get Started Consumer/Producer Tutorial Does Not Work

System Ubuntu 20.04

At the “Build Producer” step

When running make producer this error occurs

producer.c:67:37: warning: implicit declaration of function ‘arc4random’; did you mean ‘srandom’? [-Wimplicit-function-declaration]
   67 |         const char *key =  user_ids[arc4random() % ARR_SIZE(user_ids)];
      |                                     ^~~~~~~~~~
      |                                     srandom

The steps were followed exactly, this tutorial does not work.

Also, when I changed the use of arc4random() to random() I then get a new error that seems to have to do with linking libraries

It is like the Makefile is not correct?

$ make producer
cc -Wall -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -lrdkafka -lglib-2.0    producer.c   -o producer
/usr/bin/ld: /tmp/cczmdeDb.o: in function `g_autoptr_cleanup_generic_gfree':
producer.c:(.text+0x1f): undefined reference to `g_free'
/usr/bin/ld: /tmp/cczmdeDb.o: in function `glib_autoptr_clear_GError':
producer.c:(.text+0x41): undefined reference to `g_error_free'
/usr/bin/ld: /tmp/cczmdeDb.o: in function `glib_autoptr_clear_GKeyFile':
producer.c:(.text+0x81): undefined reference to `g_key_file_unref'
/usr/bin/ld: /tmp/cczmdeDb.o: in function `load_config_group':
producer.c:(.text+0x105): undefined reference to `g_key_file_get_keys'
/usr/bin/ld: producer.c:(.text+0x145): undefined reference to `g_log'
/usr/bin/ld: producer.c:(.text+0x17c): undefined reference to `g_key_file_get_string'
/usr/bin/ld: producer.c:(.text+0x1b8): undefined reference to `g_log'
/usr/bin/ld: producer.c:(.text+0x1e4): undefined reference to `rd_kafka_conf_set'
/usr/bin/ld: producer.c:(.text+0x20d): undefined reference to `g_log'
/usr/bin/ld: /tmp/cczmdeDb.o: in function `dr_msg_cb':
producer.c:(.text+0x28e): undefined reference to `rd_kafka_err2str'
/usr/bin/ld: producer.c:(.text+0x2ac): undefined reference to `g_log'
/usr/bin/ld: /tmp/cczmdeDb.o: in function `main':
producer.c:(.text+0x30e): undefined reference to `g_log'
/usr/bin/ld: producer.c:(.text+0x332): undefined reference to `g_key_file_new'
/usr/bin/ld: producer.c:(.text+0x35e): undefined reference to `g_key_file_load_from_file'
/usr/bin/ld: producer.c:(.text+0x38b): undefined reference to `g_log'
/usr/bin/ld: producer.c:(.text+0x392): undefined reference to `rd_kafka_conf_new'
/usr/bin/ld: producer.c:(.text+0x3cf): undefined reference to `rd_kafka_conf_set_dr_msg_cb'
/usr/bin/ld: producer.c:(.text+0x3ef): undefined reference to `rd_kafka_new'
/usr/bin/ld: producer.c:(.text+0x425): undefined reference to `g_log'
/usr/bin/ld: producer.c:(.text+0x5f5): undefined reference to `rd_kafka_producev'
/usr/bin/ld: producer.c:(.text+0x615): undefined reference to `rd_kafka_err2str'
/usr/bin/ld: producer.c:(.text+0x640): undefined reference to `g_log'
/usr/bin/ld: producer.c:(.text+0x67b): undefined reference to `g_log'
/usr/bin/ld: producer.c:(.text+0x68f): undefined reference to `rd_kafka_poll'
/usr/bin/ld: producer.c:(.text+0x6c3): undefined reference to `g_log'
/usr/bin/ld: producer.c:(.text+0x6d7): undefined reference to `rd_kafka_flush'
/usr/bin/ld: producer.c:(.text+0x6e6): undefined reference to `rd_kafka_outq_len'
/usr/bin/ld: producer.c:(.text+0x6f9): undefined reference to `rd_kafka_outq_len'
/usr/bin/ld: producer.c:(.text+0x716): undefined reference to `g_log'
/usr/bin/ld: producer.c:(.text+0x745): undefined reference to `g_log'
/usr/bin/ld: producer.c:(.text+0x754): undefined reference to `rd_kafka_destroy'
collect2: error: ld returned 1 exit status
make: *** [<builtin>: producer] Error 1

Thank you for reporting this. I was able to repro both issues (arc4random, and linkage errors).

For the arc4random one, I like changing to random since arc4random looks to be BSD only. This is better than having to install a BSD compat package.

For the linker errors, yeah I think that the tutorial should have explicit CFLAGS and LDLIBS, and that glib-2.0 should come first. Could you try the Makefile below and report back if it goes smoothly? I’ll update the tutorial if you can confirm.

ALL: producer consumer

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

That fixed it thank you.

I am now able to complete the tutorial.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.