I'm trying to build project which uses standalone asio as dependency. Problem is that with exceptions turned off (-fno-exceptions
), I have to define throw_exception
function (link to boost documentation), but no matter how I define that function, linker throws undefined reference
.
This is how I've tried to define throw_exception
:
#define ASIO_NO_EXCEPTIONS
#include <asio/detail/throw_exception.hpp>
template <class E>
void asio::detail::throw_exception(E const& e) {
// do something
}
I receive these linking errors:
/usr/bin/ld: libsioclient.a(sio_socket.cpp.o): in function `asio::detail::do_throw_error(std::error_code const&, char const*) [clone .isra.0]':
sio_socket.cpp:(.text 0x333): undefined reference to `void asio::detail::throw_exception<std::system_error>(std::system_error const&)'
/usr/bin/ld: _libsioclient.a(sio_socket.cpp.o): in function `void asio::execution::detail::any_executor_base::query_fn<void, asio::execution::prefer_only<asio::execution::detail::outstanding_work::tracked_t<0> > >(void*, void const*, void const*)':
sio_socket.cpp:(.text._ZN4asio9execution6detail17any_executor_base8query_fnIvNS0_11prefer_onlyINS1_16outstanding_work9tracked_tILi0EEEEEEEvPvPKvSB_[_ZN4asio9execution6detail17any_executor_base8query_fnIvNS0_11prefer_onlyINS1_16outstanding_work9tracked_tILi0EEEEEEEvPvPKvSB_] 0x28): undefined reference to `void asio::detail::throw_exception<asio::execution::bad_executor>(asio::execution::bad_executor const&)'
/usr/bin/ld: libsioclient.a(sio_socket.cpp.o): in function `void asio::execution::detail::any_executor_base::query_fn<void, asio::execution::prefer_only<asio::execution::detail::outstanding_work::untracked_t<0> > >(void*, void const*, void const*)':
sio_socket.cpp:(.text._ZN4asio9execution6detail17any_executor_base8query_fnIvNS0_11prefer_onlyINS1_16outstanding_work11untracked_tILi0EEEEEEEvPvPKvSB_[_ZN4asio9execution6detail17any_executor_base8query_fnIvNS0_11prefer_onlyINS1_16outstanding_work11untracked_tILi0EEEEEEEvPvPKvSB_] 0x28): undefined reference to `void asio::detail::throw_exception<asio::execution::bad_executor>(asio::execution::bad_executor const&)'
/usr/bin/ld: libsioclient.a(sio_socket.cpp.o): in function `void asio::execution::detail::any_executor_base::query_fn<void, asio::execution::prefer_only<asio::execution::detail::relationship::fork_t<0> > >(void*, void const*, void const*)':
sio_socket.cpp:(.text._ZN4asio9execution6detail17any_executor_base8query_fnIvNS0_11prefer_onlyINS1_12relationship6fork_tILi0EEEEEEEvPvPKvSB_[_ZN4asio9execution6detail17any_executor_base8query_fnIvNS0_11prefer_onlyINS1_12relationship6fork_tILi0EEEEEEEvPvPKvSB_] 0x28): undefined reference to `void asio::detail::throw_exception<asio::execution::bad_executor>(asio::execution::bad_executor const&)'
/usr/bin/ld: libsioclient.a(sio_socket.cpp.o): in function `void asio::execution::detail::any_executor_base::query_fn<void, asio::execution::prefer_only<asio::execution::detail::relationship::continuation_t<0> > >(void*, void const*, void const*)':
sio_socket.cpp:(.text._ZN4asio9execution6detail17any_executor_base8query_fnIvNS0_11prefer_onlyINS1_12relationship14continuation_tILi0EEEEEEEvPvPKvSB_[_ZN4asio9execution6detail17any_executor_base8query_fnIvNS0_11prefer_onlyINS1_12relationship14continuation_tILi0EEEEEEEvPvPKvSB_] 0x28): undefined reference to `void asio::detail::throw_exception<asio::execution::bad_executor>(asio::execution::bad_executor const&)'
/usr/bin/ld: libsioclient.a(sio_socket.cpp.o): in function `void asio::execution::detail::any_executor_base::query_fn<void, asio::execution::context_as_t<asio::execution_context&> >(void*, void const*, void const*)':
sio_socket.cpp:(.text._ZN4asio9execution6detail17any_executor_base8query_fnIvNS0_12context_as_tIRNS_17execution_contextEEEEEvPvPKvSA_[_ZN4asio9execution6detail17any_executor_base8query_fnIvNS0_12context_as_tIRNS_17execution_contextEEEEEvPvPKvSA_] 0x28): undefined reference to `void asio::detail::throw_exception<asio::execution::bad_executor>(asio::execution::bad_executor const&)'
/usr/bin/ld: libsioclient.a(sio_socket.cpp.o):sio_socket.cpp:(.text._ZN4asio9execution6detail17any_executor_base10require_fnINS0_12any_executorIJNS0_12context_as_tIRNS_17execution_contextEEENS1_8blocking7never_tILi0EEENS0_11prefer_onlyINS9_10possibly_tILi0EEEEENSC_INS1_16outstanding_work9tracked_tILi0EEEEENSC_INSG_11untracked_tILi0EEEEENSC_INS1_12relationship6fork_tILi0EEEEENSC_INSN_14continuation_tILi0EEEEEEEEvS8_EET_PKvSX_[_ZN4asio9execution6detail17any_executor_base10require_fnINS0_12any_executorIJNS0_12context_as_tIRNS_17execution_contextEEENS1_8blocking7never_tILi0EEENS0_11prefer_onlyINS9_10possibly_tILi0EEEEENSC_INS1_16outstanding_work9tracked_tILi0EEEEENSC_INSG_11untracked_tILi0EEEEENSC_INS1_12relationship6fork_tILi0EEEEENSC_INSN_14continuation_tILi0EEEEEEEEvS8_EET_PKvSX_] 0x2d): more undefined references to `void asio::detail::throw_exception<asio::execution::bad_executor>(asio::execution::bad_executor const&)' follow
/usr/bin/ld: libsioclient.a(sio_socket.cpp.o): in function `asio::detail::scheduler::scheduler(asio::execution_context&, int, bool, asio::detail::scheduler_task* (*)(asio::execution_context&)) [clone .part.0]':
sio_socket.cpp:(.text._ZN4asio6detail9schedulerC2ERNS_17execution_contextEibPFPNS0_14scheduler_taskES3_E.part.0[_ZN4asio6detail9schedulerC5ERNS_17execution_contextEibPFPNS0_14scheduler_taskES3_E] 0x228): undefined reference to `void asio::detail::throw_exception<std::system_error>(std::system_error const&)'
/usr/bin/ld: libsioclient.a(sio_socket.cpp.o): in function `asio::detail::epoll_reactor::notify_fork(asio::execution_context::fork_event)':
sio_socket.cpp:(.text._ZN4asio6detail13epoll_reactor11notify_forkENS_17execution_context10fork_eventE[_ZN4asio6detail13epoll_reactor11notify_forkENS_17execution_context10fork_eventE] 0x375): undefined reference to `void asio::detail::throw_exception<std::system_error>(std::system_error const&)'
/usr/bin/ld: libsioclient.a(sio_socket.cpp.o): in function `asio::detail::scheduler::scheduler(asio::execution_context&, int, bool, asio::detail::scheduler_task* (*)(asio::execution_context&))':
sio_socket.cpp:(.text._ZN4asio6detail9schedulerC2ERNS_17execution_contextEibPFPNS0_14scheduler_taskES3_E[_ZN4asio6detail9schedulerC5ERNS_17execution_contextEibPFPNS0_14scheduler_taskES3_E] 0x24a): undefined reference to `void asio::detail::throw_exception<std::system_error>(std::system_error const&)'
/usr/bin/ld: sio_socket.cpp:(.text._ZN4asio6detail9schedulerC2ERNS_17execution_contextEibPFPNS0_14scheduler_taskES3_E[_ZN4asio6detail9schedulerC5ERNS_17execution_contextEibPFPNS0_14scheduler_taskES3_E] 0x4f7): undefined reference to `void asio::detail::throw_exception<std::system_error>(std::system_error const&)'
/usr/bin/ld: libsioclient.a(sio_socket.cpp.o): in function `asio::execution_context::service* asio::detail::service_registry::create<asio::detail::epoll_reactor, asio::execution_context>(void*)':
sio_socket.cpp:(.text._ZN4asio6detail16service_registry6createINS0_13epoll_reactorENS_17execution_contextEEEPNS4_7serviceEPv[_ZN4asio6detail16service_registry6createINS0_13epoll_reactorENS_17execution_contextEEEPNS4_7serviceEPv] 0x5d6): undefined reference to `void asio::detail::throw_exception<std::system_error>(std::system_error const&)'
/usr/bin/ld: libsioclient.a(sio_client_impl.cpp.o): in function `std::_Function_handler<void (bool, std::shared_ptr<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const> const&), sio::client_impl::on_ping()::{lambda(bool, std::shared_ptr<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const>)#1}>::_M_invoke(std::_Any_data const&, bool&&, std::shared_ptr<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const> const&)':
sio_client_impl.cpp:(.text 0x3c40): undefined reference to `websocketpp::endpoint<websocketpp::connection<websocketpp::config::asio_client>, websocketpp::config::asio_client>::send(std::weak_ptr<void>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, websocketpp::frame::opcode::value)'
/usr/bin/ld: libsioclient.a(sio_client_impl.cpp.o): in function `sio::client_impl::connect_impl(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
sio_client_impl.cpp:(.text 0x988c): undefined reference to `websocketpp::connection<websocketpp::config::asio_client>::replace_header(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
How to properly define throw_exception
function or if I'm missing something, how can these errors be solved?
CodePudding user response:
You can try this, log() is the imaginary function you already use for logging ...
// somechere in a cpp file, preferably.
#include <boost/asio.hpp>
// ...
namespace boost::asio::detail {
template<>
void throw_exception(const asio::execution::bad_executor& e)
{
// throw e; // this is what the original function does
LOG("asio::execution::bad_executor thrown\n"); // etc...
exit(3); // That's all you can do at this point.
}
template<>
void throw_exception(const std::system_error& e)
{
LOG("std::system_error thrown\n"); // etc...
exit(3); // That's all you can do at this point.
}
}
You may need to declare more specializations as you go along.
CodePudding user response:
I find solution. Problem was that compiler threw implementation of throw_exception
from code while compiling, and when linker tried to find that function, there simply was no throw_exception
. I made it like this:
namespace asio::detail {
template <typename Exception> void throw_exception(const Exception& e) {
std::cerr << "Something went wrong..." << std::endl;
std::terminate();
}
}; // namespace asio::detail
If I call this function somewhere, I don't receive linking errors. Probably, I have to implement some kind of error handling instead of that and find a solution to make this function properly compiled and linked, but, at least, I've found a reason of that strange behaviour.