0

I'm trying to try out the cpprestsdk library and am finding that when compiling using clang++ src/handler.cpp main.cpp, I get this error:

/usr/bin/ld: /tmp/handler-8f246d.o: in function `boost::asio::error::detail::ssl_category::message[abi:cxx11](int) const':
handler.cpp:(.text._ZNK5boost4asio5error6detail12ssl_category7messageB5cxx11Ei[_ZNK5boost4asio5error6detail12ssl_category7messageB5cxx11Ei]+0x29): undefined reference to `ERR_reason_error_string'
/usr/bin/ld: /tmp/handler-8f246d.o: in function `boost::asio::detail::posix_thread::join()':
handler.cpp:(.text._ZN5boost4asio6detail12posix_thread4joinEv[_ZN5boost4asio6detail12posix_thread4joinEv]+0x2a): undefined reference to `pthread_join'
/usr/bin/ld: /tmp/handler-8f246d.o: in function `boost::asio::detail::posix_thread::~posix_thread()':
handler.cpp:(.text._ZN5boost4asio6detail12posix_threadD2Ev[_ZN5boost4asio6detail12posix_threadD2Ev]+0x26): undefined reference to `pthread_detach'
/usr/bin/ld: /tmp/handler-8f246d.o: in function `boost::asio::ssl::detail::openssl_init_base::do_init::~do_init()':
handler.cpp:(.text._ZN5boost4asio3ssl6detail17openssl_init_base7do_initD2Ev[_ZN5boost4asio3ssl6detail17openssl_init_base7do_initD2Ev]+0x12): undefined reference to `CONF_modules_unload'
/usr/bin/ld: /tmp/Main-9a7bc9.o: in function `main':
Main.cpp:(.text+0x30): undefined reference to `web::uri::uri(char const*)'
/usr/bin/ld: Main.cpp:(.text+0x6c): undefined reference to `web::http::methods::GET[abi:cxx11]'
/usr/bin/ld: /tmp/Main-9a7bc9.o: in function `web::http::experimental::listener::http_listener::open()':
Main.cpp:(.text._ZN3web4http12experimental8listener13http_listener4openEv[_ZN3web4http12experimental8listener13http_listener4openEv]+0x32): undefined reference to `web::http::experimental::listener::details::http_listener_impl::open()'
/usr/bin/ld: /tmp/Main-9a7bc9.o: in function `pplx::task_options::task_options()':
Main.cpp:(.text._ZN4pplx12task_optionsC2Ev[_ZN4pplx12task_optionsC2Ev]+0x27): undefined reference to `pplx::get_ambient_scheduler()'
/usr/bin/ld: /tmp/Main-9a7bc9.o: in function `web::http::experimental::listener::http_listener::~http_listener()':
Main.cpp:(.text._ZN3web4http12experimental8listener13http_listenerD2Ev[_ZN3web4http12experimental8listener13http_listenerD2Ev]+0x3a): undefined reference to `web::http::experimental::listener::details::http_listener_impl::close()'
/usr/bin/ld: /tmp/Main-9a7bc9.o: in function `std::unique_ptr<web::http::experimental::listener::details::http_listener_impl, std::default_delete<web::http::experimental::listener::details::http_listener_impl> > utility::details::make_unique<web::http::experimental::listener::details::http_listener_impl, web::uri>(web::uri&&)':
Main.cpp:(.text._ZN7utility7details11make_uniqueIN3web4http12experimental8listener7details18http_listener_implENS2_3uriEEESt10unique_ptrIT_St14default_deleteISA_EEOT0_[_ZN7utility7details11make_uniqueIN3web4http12experimental8listener7details18http_listener_implENS2_3uriEEESt10unique_ptrIT_St14default_deleteISA_EEOT0_]+0x84): undefined reference to `web::http::experimental::listener::details::http_listener_impl::http_listener_impl(web::uri)'
/usr/bin/ld: /tmp/Main-9a7bc9.o: in function `pplx::details::_CancellationTokenState::_DeregisterCallback(pplx::details::_CancellationTokenRegistration*)':
Main.cpp:(.text._ZN4pplx7details23_CancellationTokenState19_DeregisterCallbackEPNS0_30_CancellationTokenRegistrationE[_ZN4pplx7details23_CancellationTokenState19_DeregisterCallbackEPNS0_30_CancellationTokenRegistrationE]+0x188): undefined reference to `pplx::details::platform::GetCurrentThreadId()'
/usr/bin/ld: /tmp/Main-9a7bc9.o: in function `pplx::details::_TaskCollectionImpl::_RunTask(void (*)(void*), void*, pplx::details::_TaskInliningMode)':
Main.cpp:(.text._ZN4pplx7details19_TaskCollectionImpl8_RunTaskEPFvPvES2_NS0_17_TaskInliningModeE[_ZN4pplx7details19_TaskCollectionImpl8_RunTaskEPFvPvES2_NS0_17_TaskInliningModeE]+0x38): undefined reference to `pplx::get_ambient_scheduler()'
/usr/bin/ld: /tmp/Main-9a7bc9.o: in function `pplx::details::_CancellationTokenRegistration::_Invoke()':
Main.cpp:(.text._ZN4pplx7details30_CancellationTokenRegistration7_InvokeEv[_ZN4pplx7details30_CancellationTokenRegistration7_InvokeEv]+0x15): undefined reference to `pplx::details::platform::GetCurrentThreadId()'
clang: error: linker command failed with exit code 1 (use -v to see invocation)

the files are as follows: include/handler.h:

#ifndef HANDLER_H
#define HANDLER_H
#include <cpprest/http_listener.h>
using namespace web;
using namespace web::http;

void handle_get(http_request request);

#endif

src/handler.cpp:

#include <iostream>
#include "../include/handler.h"

using namespace std;

void handle_get(http_request request)
{
    cout << "in handler!!!" << endl;
}

main.cpp

#include <iostream>
#include <cpprest/http_listener.h>
#include "include/handler.h"

using namespace web;
using namespace web::http;
using namespace web::http::experimental::listener;
using namespace std;

int main(const int, const char **)
{
    http_listener listener("http://*:8080");
    listener.support(methods::GET, handle_get);
    try
    {
        listener
            .open()
            .then([&listener]() { cout << "starting to listen..." << endl; })
            .wait();
        while (true)
            ;
    }
    catch (exception const &e)
    {
        cout << e.what() << endl;
    }
    return 0;
}

Any help on resolving this would be much appreciated.

reactor
  • 1,722
  • 1
  • 14
  • 34
  • https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix – rustyx Dec 25 '20 at 19:15

1 Answers1

1
handler.cpp: [...] undefined reference to `pthread_join'
Main.cpp: [...] undefined reference to `web::uri::uri(char const*)'

It looks like you aren't linking to the libraries that contain those functions, which is why the linker can't find them.

To link to the pthread_*() functions, you can add -pthread to your compile-line arguments.

For the web::uri::uri(char * const) function, the argument will probably be something like -lcpprestsdk, but I'm not sure of the exact library name. Look for a file named libcpprestsdk.so or libcpprestsdk.a or similar, and derive the -l argument from that (e.g. to link to libxyz.so or libxyz.a, you would specify -lxyz)

Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234