Search code examples
c++amqpqpid

"Undefined reference to" Linking errors when trying to build with headers (QPID/Proton C++)


I'm a C++ neophyte and I am totally in over my head, but I'm trying anyway!

EDIT: here is a simple code repo reproducer that generates the error mentioned: https://github.com/thoraxe/qpid-proton-test

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

I am trying to incorporate the above into an existing project and struggling badly. In order to be able to use various instances of the QPID Proton sender/receiver, I need to include their class files. The example is a pure cpp and includes no header file.

In order to include a receiver (consumer) class, I need to write a header file. And it's all falling apart on compile.

Here's a really simple example. I'm trying to start out and just go more or less line by line with the receiver example. So, here's my cpp:

#include "ProtonConsumer.h"

Here's the header:

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

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

};

In my main, I'm trying to simply use an instance of the 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);
}

When I try to build (cmake), I get all kinds of "undefined reference to" and other errors:

[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 

I have dug around in GitHub and found various other examples where headers are used, for example here. Many of the undefined reference errors I'm getting in my code are functions declared in the header file of this reference example. I believe that the ~ProtonConsumer error is related to having a destructor, or something?

I'm super totally lost and not quite sure where to go from here. It looks like I might have to define all of the _ZTV14ProtonConsumer undefined references in my header and my class, but I'm not sure about the other things. The code example I referenced did not declare all of the functions. It's not entirely clear from the messaging handler documentation which functions might be required to be declared, or not.

After considerable digging, the only thing I found that might be related was this post, but it doesn't seem related after inspection.


Solution

  • You're not linking with the Proton library. Use find_package to locate the library you're trying to use, and then target_link_libraries to link with it.

    For example, find_package(ProtonCpp REQUIRED).