尝试使用标头构建时出现“未定义的引用”链接错误QPID/Proton C++

问题描述

我是 C++ 新手,我完全不知所措,但我还是在努力!

编辑:这里是一个简单的代码存储库复制器,它会生成提到的错误https://github.com/thoraxe/qpid-proton-test

http://qpid.apache.org/releases/qpid-proton-0.33.0/proton/cpp/examples/simple_recv.cpp.html

我正在尝试将上述内容合并到 existing project 中并且挣扎得很厉害。为了能够使用 QPID Proton 发送器/接收器的各种实例,我需要包含它们的类文件。该示例是一个纯 cpp,不包含头文件

为了包含接收者(消费者)类,我需要编写一个文件。而且在编译时一切都崩溃了。

这是一个非常简单的例子。我正在尝试开始,只是或多或少地按照接收器示例逐行进行。所以,这是我的 cpp:

#include "ProtonConsumer.h"

这是标题

class ProtonConsumer : public proton::messaging_handler {
  private:
    std::string url;
    proton::receiver receiver;

  public:
    // constructor
    ProtonConsumer(const std::string &s) : url(s) {};

};

在我的主要内容中,我试图简单地使用 ProtonConsumer一个实例:

#include "ProtonConsumer.h"
#include <iostream>

int main(int,char**) {
    std::string     strProtonURI = "127.0.0.1/COMMAND.IN";
    std::cout << "Hello,world!\n";
    ProtonConsumer pProtonConsumer(strProtonURI);
}

当我尝试构建 (cmake) 时,出现各种“未定义引用”和其他错误

[main] Building folder: qpid-proton-test 
[build] Starting build
[proc] Executing command: /usr/bin/cmake --build /home/thoraxe/Red_Hat/openshift/qpid-proton-test/build --config Debug --target all -- -j 10
[build] Scanning dependencies of target qpid-proton-test
[build] [ 50%] Building CXX object CMakeFiles/qpid-proton-test.dir/main.cpp.o
[build] [100%] Linking CXX executable qpid-proton-test
[build] /usr/bin/ld: CMakeFiles/qpid-proton-test.dir/main.cpp.o: in function `proton::endpoint::endpoint()':
[build] /usr/include/proton/endpoint.hpp:67: undefined reference to `vtable for proton::endpoint'
[build] /usr/bin/ld: CMakeFiles/qpid-proton-test.dir/main.cpp.o: in function `proton::link::link()':
[build] /usr/include/proton/link.hpp:49: undefined reference to `vtable for proton::link'
[build] /usr/bin/ld: CMakeFiles/qpid-proton-test.dir/main.cpp.o: in function `proton::receiver::receiver()':
[build] /usr/include/proton/receiver.hpp:48: undefined reference to `vtable for proton::receiver'
[build] /usr/bin/ld: CMakeFiles/qpid-proton-test.dir/main.cpp.o: in function `ProtonConsumer::ProtonConsumer(std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char> > const&)':
[build] /home/thoraxe/Red_Hat/openshift/qpid-proton-test/ProtonConsumer.h:26: undefined reference to `proton::messaging_handler::messaging_handler()'
[build] /usr/bin/ld: /home/thoraxe/Red_Hat/openshift/qpid-proton-test/ProtonConsumer.h:26: undefined reference to `proton::messaging_handler::~messaging_handler()'
[build] /usr/bin/ld: CMakeFiles/qpid-proton-test.dir/main.cpp.o: in function `proton::internal::pn_ptr<pn_link_t>::pn_ptr(pn_link_t*)':
[build] /usr/include/proton/internal/object.hpp:48: undefined reference to `proton::internal::pn_ptr_base::incref(void*)'
[build] /usr/bin/ld: CMakeFiles/qpid-proton-test.dir/main.cpp.o: in function `proton::internal::pn_ptr<pn_link_t>::~pn_ptr()':
[build] /usr/include/proton/internal/object.hpp:55: undefined reference to `proton::internal::pn_ptr_base::decref(void*)'
[build] /usr/bin/ld: CMakeFiles/qpid-proton-test.dir/main.cpp.o: in function `proton::internal::pn_ptr<pn_link_t>::pn_ptr(proton::internal::pn_ptr<pn_link_t> const&)':
[build] /usr/include/proton/internal/object.hpp:49: undefined reference to `proton::internal::pn_ptr_base::incref(void*)'
[build] /usr/bin/ld: CMakeFiles/qpid-proton-test.dir/main.cpp.o:(.rodata._ZTV14ProtonConsumer[_ZTV14ProtonConsumer]+0x20): undefined reference to `proton::messaging_handler::on_container_start(proton::container&)'
[build] /usr/bin/ld: CMakeFiles/qpid-proton-test.dir/main.cpp.o:(.rodata._ZTV14ProtonConsumer[_ZTV14ProtonConsumer]+0x28): undefined reference to `proton::messaging_handler::on_container_stop(proton::container&)'
[build] /usr/bin/ld: CMakeFiles/qpid-proton-test.dir/main.cpp.o:(.rodata._ZTV14ProtonConsumer[_ZTV14ProtonConsumer]+0x30): undefined reference to `proton::messaging_handler::on_message(proton::delivery&,proton::message&)'
[build] /usr/bin/ld: CMakeFiles/qpid-proton-test.dir/main.cpp.o:(.rodata._ZTV14ProtonConsumer[_ZTV14ProtonConsumer]+0x38): undefined reference to `proton::messaging_handler::on_sendable(proton::sender&)'
[build] /usr/bin/ld: CMakeFiles/qpid-proton-test.dir/main.cpp.o:(.rodata._ZTV14ProtonConsumer[_ZTV14ProtonConsumer]+0x40): undefined reference to `proton::messaging_handler::on_transport_open(proton::transport&)'
[build] /usr/bin/ld: CMakeFiles/qpid-proton-test.dir/main.cpp.o:(.rodata._ZTV14ProtonConsumer[_ZTV14ProtonConsumer]+0x48): undefined reference to `proton::messaging_handler::on_transport_close(proton::transport&)'
[build] /usr/bin/ld: CMakeFiles/qpid-proton-test.dir/main.cpp.o:(.rodata._ZTV14ProtonConsumer[_ZTV14ProtonConsumer]+0x50): undefined reference to `proton::messaging_handler::on_transport_error(proton::transport&)'
[build] /usr/bin/ld: CMakeFiles/qpid-proton-test.dir/main.cpp.o:(.rodata._ZTV14ProtonConsumer[_ZTV14ProtonConsumer]+0x58): undefined reference to `proton::messaging_handler::on_connection_open(proton::connection&)'
[build] /usr/bin/ld: CMakeFiles/qpid-proton-test.dir/main.cpp.o:(.rodata._ZTV14ProtonConsumer[_ZTV14ProtonConsumer]+0x60): undefined reference to `proton::messaging_handler::on_connection_close(proton::connection&)'
[build] /usr/bin/ld: CMakeFiles/qpid-proton-test.dir/main.cpp.o:(.rodata._ZTV14ProtonConsumer[_ZTV14ProtonConsumer]+0x68): undefined reference to `proton::messaging_handler::on_connection_error(proton::connection&)'
[build] /usr/bin/ld: CMakeFiles/qpid-proton-test.dir/main.cpp.o:(.rodata._ZTV14ProtonConsumer[_ZTV14ProtonConsumer]+0x70): undefined reference to `proton::messaging_handler::on_session_open(proton::session&)'
[build] /usr/bin/ld: CMakeFiles/qpid-proton-test.dir/main.cpp.o:(.rodata._ZTV14ProtonConsumer[_ZTV14ProtonConsumer]+0x78): undefined reference to `proton::messaging_handler::on_session_close(proton::session&)'
[build] /usr/bin/ld: CMakeFiles/qpid-proton-test.dir/main.cpp.o:(.rodata._ZTV14ProtonConsumer[_ZTV14ProtonConsumer]+0x80): undefined reference to `proton::messaging_handler::on_session_error(proton::session&)'
[build] /usr/bin/ld: CMakeFiles/qpid-proton-test.dir/main.cpp.o:(.rodata._ZTV14ProtonConsumer[_ZTV14ProtonConsumer]+0x88): undefined reference to `proton::messaging_handler::on_receiver_open(proton::receiver&)'
[build] /usr/bin/ld: CMakeFiles/qpid-proton-test.dir/main.cpp.o:(.rodata._ZTV14ProtonConsumer[_ZTV14ProtonConsumer]+0x90): undefined reference to `proton::messaging_handler::on_receiver_detach(proton::receiver&)'
[build] /usr/bin/ld: CMakeFiles/qpid-proton-test.dir/main.cpp.o:(.rodata._ZTV14ProtonConsumer[_ZTV14ProtonConsumer]+0x98): undefined reference to `proton::messaging_handler::on_receiver_close(proton::receiver&)'
[build] /usr/bin/ld: CMakeFiles/qpid-proton-test.dir/main.cpp.o:(.rodata._ZTV14ProtonConsumer[_ZTV14ProtonConsumer]+0xa0): undefined reference to `proton::messaging_handler::on_receiver_error(proton::receiver&)'
[build] /usr/bin/ld: CMakeFiles/qpid-proton-test.dir/main.cpp.o:(.rodata._ZTV14ProtonConsumer[_ZTV14ProtonConsumer]+0xa8): undefined reference to `proton::messaging_handler::on_sender_open(proton::sender&)'
[build] /usr/bin/ld: CMakeFiles/qpid-proton-test.dir/main.cpp.o:(.rodata._ZTV14ProtonConsumer[_ZTV14ProtonConsumer]+0xb0): undefined reference to `proton::messaging_handler::on_sender_detach(proton::sender&)'
[build] /usr/bin/ld: CMakeFiles/qpid-proton-test.dir/main.cpp.o:(.rodata._ZTV14ProtonConsumer[_ZTV14ProtonConsumer]+0xb8): undefined reference to `proton::messaging_handler::on_sender_close(proton::sender&)'
[build] /usr/bin/ld: CMakeFiles/qpid-proton-test.dir/main.cpp.o:(.rodata._ZTV14ProtonConsumer[_ZTV14ProtonConsumer]+0xc0): undefined reference to `proton::messaging_handler::on_sender_error(proton::sender&)'
[build] /usr/bin/ld: CMakeFiles/qpid-proton-test.dir/main.cpp.o:(.rodata._ZTV14ProtonConsumer[_ZTV14ProtonConsumer]+0xc8): undefined reference to `proton::messaging_handler::on_tracker_accept(proton::tracker&)'
[build] /usr/bin/ld: CMakeFiles/qpid-proton-test.dir/main.cpp.o:(.rodata._ZTV14ProtonConsumer[_ZTV14ProtonConsumer]+0xd0): undefined reference to `proton::messaging_handler::on_tracker_reject(proton::tracker&)'
[build] /usr/bin/ld: CMakeFiles/qpid-proton-test.dir/main.cpp.o:(.rodata._ZTV14ProtonConsumer[_ZTV14ProtonConsumer]+0xd8): undefined reference to `proton::messaging_handler::on_tracker_release(proton::tracker&)'
[build] /usr/bin/ld: CMakeFiles/qpid-proton-test.dir/main.cpp.o:(.rodata._ZTV14ProtonConsumer[_ZTV14ProtonConsumer]+0xe0): undefined reference to `proton::messaging_handler::on_tracker_settle(proton::tracker&)'
[build] /usr/bin/ld: CMakeFiles/qpid-proton-test.dir/main.cpp.o:(.rodata._ZTV14ProtonConsumer[_ZTV14ProtonConsumer]+0xe8): undefined reference to `proton::messaging_handler::on_delivery_settle(proton::delivery&)'
[build] /usr/bin/ld: CMakeFiles/qpid-proton-test.dir/main.cpp.o:(.rodata._ZTV14ProtonConsumer[_ZTV14ProtonConsumer]+0xf0): undefined reference to `proton::messaging_handler::on_sender_drain_start(proton::sender&)'
[build] /usr/bin/ld: CMakeFiles/qpid-proton-test.dir/main.cpp.o:(.rodata._ZTV14ProtonConsumer[_ZTV14ProtonConsumer]+0xf8): undefined reference to `proton::messaging_handler::on_receiver_drain_finish(proton::receiver&)'
[build] /usr/bin/ld: CMakeFiles/qpid-proton-test.dir/main.cpp.o:(.rodata._ZTV14ProtonConsumer[_ZTV14ProtonConsumer]+0x100): undefined reference to `proton::messaging_handler::on_connection_wake(proton::connection&)'
[build] /usr/bin/ld: CMakeFiles/qpid-proton-test.dir/main.cpp.o:(.rodata._ZTV14ProtonConsumer[_ZTV14ProtonConsumer]+0x108): undefined reference to `proton::messaging_handler::on_error(proton::error_condition const&)'
[build] /usr/bin/ld: CMakeFiles/qpid-proton-test.dir/main.cpp.o: in function `ProtonConsumer::~ProtonConsumer()':
[build] /home/thoraxe/Red_Hat/openshift/qpid-proton-test/ProtonConsumer.h:19: undefined reference to `proton::receiver::~receiver()'
[build] /usr/bin/ld: /home/thoraxe/Red_Hat/openshift/qpid-proton-test/ProtonConsumer.h:19: undefined reference to `proton::messaging_handler::~messaging_handler()'
[build] /usr/bin/ld: CMakeFiles/qpid-proton-test.dir/main.cpp.o:(.rodata._ZTI14ProtonConsumer[_ZTI14ProtonConsumer]+0x10): undefined reference to `typeinfo for proton::messaging_handler'
[build] collect2: error: ld returned 1 exit status
[build] gmake[2]: *** [CMakeFiles/qpid-proton-test.dir/build.make:103: qpid-proton-test] Error 1
[build] gmake[1]: *** [CMakeFiles/Makefile2:636: CMakeFiles/qpid-proton-test.dir/all] Error 2
[build] gmake: *** [Makefile:136: all] Error 2
[build] Build finished with exit code 

我在 GitHub 中挖掘并发现了其他各种使用标头的示例,for example here。我在代码中遇到的许多未定义引用错误都是在此参考示例的头文件中声明的函数。我认为 ~ProtonConsumer 错误与具有析构函数或其他内容有关?

我完全迷失了方向,不太确定从哪里开始。看起来我可能必须在我的标题和我的班级中定义所有 _ZTV14ProtonConsumer 未定义的引用,但我不确定其他事情。我引用的代码示例并未声明所有函数。从 messaging handler documentation 中并不完全清楚可能需要声明哪些函数

经过大量挖掘,我发现唯一可能相关的是this post,但经过检查似乎并不相关。

解决方法

您没有链接到 Proton 图书馆。使用 find_package 定位您要使用的库,然后使用 target_link_libraries 与之链接。

例如,find_package(ProtonCpp REQUIRED)