如何使用 qmake 和 mingw 构建示例 cppkafka?
How to build an example cppkafka using qmake & mingw?
我需要远离cmake,所以我想在mingw[=上构建一个qmake项目37=]。请帮助我了解缺少的内容。从 https://github.com/mfontanini/cppkafka
构建 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 文件
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'
命令
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错误
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)
您需要将 dllexport
添加到 struct HandleDeleter
并重建。
在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_;
};
之后:
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_;
};
我需要远离cmake,所以我想在mingw[=上构建一个qmake项目37=]。请帮助我了解缺少的内容。从 https://github.com/mfontanini/cppkafka
构建 CPPKafkamain.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 文件
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'
命令
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错误
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)
您需要将 dllexport
添加到 struct HandleDeleter
并重建。
在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_;
};
之后:
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_;
};