c++qmakelibrdkafka

How to build an example cppkafka using qmake & mingw?


I need to get away from cmake, so I want to build a qmake project on mingw. Please help me understand what is missing. Build CPPKafka from https://github.com/mfontanini/cppkafka

main.cpp

#include <cppkafka/cppkafka.h>
using namespace std;
using namespace cppkafka;
int main() {
    Configuration config = {    // Create the config
        { "metadata.broker.list", "127.0.0.1:9092" }
    };
    Producer producer(config);    // Create the producer
    string message = "hey there!";    // Produce a message!
    producer.produce(MessageBuilder("my_topic").partition(0).payload(message));
    producer.flush();
    return 0;
}

.pro file

TEMPLATE = app
CONFIG += console c++14
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += main.cpp
INCLUDEPATH += 'C:\Program Files\CppKafka\include' \
               'C:\Program Files\RdKafka\include' \
               'C:\boost_1_72_0' \
               'C:\Program Files\CppKafka\include\cppkafka'

LIBS +=  'C:\Program Files\RdKafka\bin\librdkafka.dll' \
         'C:\Program Files\RdKafka\bin\librdkafka++.dll' \
         -lpthread \
         -lz \
         -lstdc++ \
         'C:\Program Files\CppKafka\bin\libcppkafka.dll'

command

g++ -Wl,-subsystem,console -mthreads -o debug\cpp_kafka_ex.exe debug/main.o "C:\Program Files\RdKafka\bin\librdkafka.dll" "C:\Program Files\RdKafka\bin\librdkafka++.dll" -lpthread -lz -lstdc++ "C:\Program Files\CppKafka\bin\libcppkafka.dll"

link error

debug/main.o: In function std::unique_ptr<rd_kafka_s, cppkafka::KafkaHandleBase::HandleDeleter>::~unique_ptr()': C:/Qt/Tools/mingw730_64/lib/gcc/x86_64-w64-mingw32/7.3.0/include/c++/bits/unique_ptr.h:268: undefined reference to cppkafka::KafkaHandleBase::HandleDeleter::operator()(rd_kafka_s*)' collect2.exe: error: ld returned 1 exit status

cmake

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../include)
include_directories('C:\Program Files\RdKafka\include')
include_directories(C:\boost_1_72_0)

add_custom_target(examples)
macro(create_example example_name)
    string(REPLACE "_" "-" sanitized_name ${example_name})
    add_executable(${sanitized_name} EXCLUDE_FROM_ALL "${example_name}_example.cpp")
    target_link_libraries(${sanitized_name} cppkafka RdKafka::rdkafka Boost::boost Boost::program_options)
    add_dependencies(examples ${sanitized_name})
endmacro()

create_example(producer)
create_example(buffered_producer)
create_example(consumer)
create_example(consumer_dispatcher)
create_example(metadata)
create_example(consumers_information)

Solution

  • You need to add dllexport to the struct HandleDeleter and rebuild.

    before include\cppkafka\kafka_handle_base.h:

    struct HandleDeleter {
            explicit HandleDeleter(const KafkaHandleBase* handle_base_ptr) : handle_base_ptr_{handle_base_ptr} {}
            void operator()(rd_kafka_t* handle);
        private:
            const KafkaHandleBase * handle_base_ptr_;
        };
    

    after:

    struct CPPKAFKA_API HandleDeleter {
            explicit HandleDeleter(const KafkaHandleBase* handle_base_ptr) : handle_base_ptr_{handle_base_ptr} {}
            void operator()(rd_kafka_t* handle);
        private:
            const KafkaHandleBase * handle_base_ptr_;
        };