librdkafka icon indicating copy to clipboard operation
librdkafka copied to clipboard

Compilation shows `undefined reference`

Open EAHITechnology opened this issue 3 years ago • 0 comments

clang++-8 ubuntu 20.02

description

I introduced librdkafka into my project, and an error occurred when I compiled the project

/usr/bin/ld: CMakeFiles/kafka_test.dir/main.cc.o: in function `main':
main.cc:(.text+0x2f1): undefined reference to `RdKafka::Consumer::create(RdKafka::Conf const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&)'
/usr/bin/ld: main.cc:(.text+0x4ad): undefined reference to `RdKafka::Topic::create(RdKafka::Handle*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, RdKafka::Conf const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&)'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [CMakeFiles/kafka_test.dir/build.make:97: kafka_test] Error 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/kafka_test.dir/all] Error 2
make: *** [Makefile:91: all] Error 2

my .cc file :

#include <iostream>
#include <string>
#include <list>
#include <stdint.h>
#include "librdkafka/rdkafka.h"
#include "librdkafka/rdkafkacpp.h"
#include <list>
using namespace std;
using namespace RdKafka;

void msg_consume(RdKafka::Message* msg) {
    if (msg->err() == RdKafka::ERR_NO_ERROR) {
        std::cout << "Read msg at offset " << msg->offset() << std::endl;
        if (msg->key()) {
            std::cout << "Key: " << *msg->key() << std::endl;
        }
        printf("%.*s\n", static_cast<int>(msg->len()), static_cast<const char *>(msg->payload()));
    } else if (msg->err() == RdKafka::ERR__TIMED_OUT) {
        printf("error[%s]\n", "ERR__TIMED_OUT");
    } else {
        printf("error[%s]\n", "other");
    }
}

int main(int argc, char **argv) {
    string err_string;
    int32_t partition = RdKafka::Topic::PARTITION_UA;
    partition = 0;

    std::string broker_list = "10.67.76.9:9092";

    RdKafka::Conf* global_conf = RdKafka::Conf::create(RdKafka::Conf::CONF_GLOBAL);
    RdKafka::Conf* topic_conf = RdKafka::Conf::create(RdKafka::Conf::CONF_TOPIC);

    int64_t start_offset = RdKafka::Topic::OFFSET_BEGINNING;
    global_conf->set("metadata.broker.list", broker_list, err_string);

    // create consumer
    RdKafka::Consumer* consumer = RdKafka::Consumer::create(global_conf, err_string);
    if (!consumer) {
        printf("failed to create consumer, %s\n", err_string.c_str());
        return -1;
    }
    printf("created consumer %s\n", consumer->name().c_str());

    // create topic
    std::string topic_name = "test";
    RdKafka::Topic* topic = RdKafka::Topic::create(consumer, topic_name, topic_conf, err_string);
    if (!topic) {
        printf("try create topic[%s] failed, %s\n", topic_name.c_str(), err_string.c_str());
        return -1;
    }

    // Start consumer for topic+partition at start offset
    RdKafka::ErrorCode resp = consumer->start(topic, partition, start_offset);
    if (resp != RdKafka::ERR_NO_ERROR) {
        printf("Failed to start consumer: %s\n",RdKafka::err2str(resp).c_str());
        return -1;
    }

    while (true) {
        RdKafka::Message *msg = consumer->consume(topic, partition, 2000);
        msg_consume(msg);
        delete msg;
    }

    // stop consumer
    consumer->stop(topic, partition);
    consumer->poll(1000);

    delete topic;
    delete consumer;

    system("pause");
    return 0;
}

reproduce

First I executed install librdkafka

git clone https://github.com/edenhill/librdkafka.git ./librdkafka
cd ./librdkafka
./configure
make -j20
sudo make install
$ ls /usr/local/include/librdkafka/
rdkafka.h  rdkafka_mock.h  rdkafkacpp.h

$ ls -all /usr/local/lib/librdkafka*
-rwxr-xr-x 1 root root  3756462 Sep 29 16:14 /usr/local/lib/librdkafka++.a
lrwxrwxrwx 1 root root       17 Sep 29 16:14 /usr/local/lib/librdkafka++.so -> librdkafka++.so.1
-rwxr-xr-x 1 root root  1415208 Sep 29 16:14 /usr/local/lib/librdkafka++.so.1
-rwxr-xr-x 1 root root  4177416 Sep 29 16:14 /usr/local/lib/librdkafka-static.a
-rwxr-xr-x 1 root root 13101330 Sep 29 16:14 /usr/local/lib/librdkafka.a
lrwxrwxrwx 1 root root       15 Sep 29 16:14 /usr/local/lib/librdkafka.so -> librdkafka.so.1
-rwxr-xr-x 1 root root  8415776 Sep 29 16:14 /usr/local/lib/librdkafka.so.1

step 2: create cmake file:

cmake_minimum_required(VERSION 3.14.0)
project(kafka_test)

SET(CMAKE_C_COMPILER clang)
SET(CMAKE_CXX_COMPILER clang++)

set(CMAKE_CXX_STANDARD 17)

find_path(KAFKA_INCLUDE_DIR librdkafka/rdkafka.h PATHS)
include_directories(${KAFKA_INCLUDE_DIR})

find_path(KAFKA_CXX_INCLUDE_DIR librdkafka/rdkafkacpp.h PATHS)
include_directories(${KAFKA_CXX_INCLUDE_DIR})

add_executable(kafka_test main.cc)
target_link_libraries(kafka_test rdkafka-static rdkafka++ rdkafka -lz -lpthread -lrt)

step 3: mkdir build && cd build

step 4:

cmake .. && make -j20

IMPORTANT: Always try to reproduce the issue on the latest released version (see https://github.com/edenhill/librdkafka/releases), if it can't be reproduced on the latest version the issue has been fixed.

Checklist

IMPORTANT: We will close issues where the checklist has not been completed.

Please provide the following information:

  • [x] librdkafka version (release number or git tag): <REPLACE with e.g., v0.10.5 or a git sha. NOT "latest" or "current">
  • [ ] Apache Kafka version: <REPLACE with e.g., 0.10.2.3>
  • [ ] librdkafka client configuration: <REPLACE with e.g., message.timeout.ms=123, auto.reset.offset=earliest, ..>
  • [ ] Operating system: <REPLACE with e.g., Centos 5 (x64)>
  • [ ] Provide logs (with debug=.. as necessary) from librdkafka
  • [ ] Provide broker log excerpts
  • [ ] Critical issue

EAHITechnology avatar Sep 29 '22 08:09 EAHITechnology