"Undefined reference to" 尝试使用 headers (QPID/Proton C++) 构建时出现链接错误

"Undefined reference to" Linking errors when trying to build with headers (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 sender/receiver 的各种实例,我需要包含它们的 class 文件。该示例是纯 cpp,不包含 header 文件。

为了包含接收者(消费者)class,我需要编写一个 header 文件。这一切都在编译时分崩离析。

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

#include "ProtonConsumer.h"

这是 header:

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 中进行了深入研究,发现了其他各种使用 header 的例子,for example here。我在代码中遇到的许多未定义引用错误都是在本参考示例的 header 文件中声明的函数。我相信 ~ProtonConsumer 错误与具有析构函数或其他东西有关?

我完全迷路了,不太确定从这里到哪里去。看起来我可能必须在我的 header 和我的 class 中定义所有 _ZTV14ProtonConsumer 未定义的引用,但我不确定其他事情。我引用的代码示例没有声明所有函数。从 messaging handler documentation 中并不完全清楚哪些函数可能需要声明,哪些不需要。

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

您没有 link 使用 Proton 库。使用find_package to locate the library you're trying to use, and then target_link_libraries到link。

例如,find_package(ProtonCpp REQUIRED).