c++ - 没有 RTTI 的 ASIO

标签 c++ g++ boost-asio c++20 rtti

我尝试在没有 rtti 的情况下使用 ASIO (1.24),但总是收到 undefined reference 错误。 所以我做了一个简单的测试程序来重现问题:

main.cpp :

#define ASIO_STANDALONE
#define ASIO_HEADER_ONLY
#define ASIO_NO_EXCEPTIONS
#define ASIO_NO_TYPEID
#include "asio.hpp"

int main()
{
    asio::io_context io;

    return 0;
}

当它被编译时

g++-12 -o Test -std=c++20 -fno-rtti -fno-exceptions -I../libs/Asio/1.24.0/include Main.cpp

我明白了

undefined reference to `void asio::detail::throw_exception<std::system_error>(std::system_error const&)'
undefined reference to `void asio::detail::throw_exception<asio::invalid_service_owner>(asio::invalid_service_owner const&)'
undefined reference to `void asio::detail::throw_exception<asio::service_already_exists>(asio::service_already_exists const&)'

是不是我漏掉了什么?我怎样才能编译它?

最佳答案

Boost documentation states如果您在没有异常支持的情况下进行编译,则需要提供 throw_exception 函数:

This macro disables exception handling in Boost, forwarding all exceptions to a user-defined non-template version of boost::throw_exception. However, unless BOOST_EXCEPTION_DISABLE is also defined, users can still examine the exception object for any data added at the point of the throw, or use boost::diagnostic_information (of course under BOOST_NO_EXCEPTIONS, the user-defined boost::throw_exception is not allowed to return to the caller.)

我认为这与独立的 Asio 密切相关

关于c++ - 没有 RTTI 的 ASIO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74098761/

相关文章:

c++ - DirectX 12 资源绑定(bind)

c++ - 带有 -isysroot 的 gcc 创建包含以等号 "="开头的包含路径并且编译失败

c++ - g++ 使用共享库时出现 undefined symbol 错误

c++ - 通过 C 接口(interface)传递 C++ i/o 类的特定于编译器的问题

c++ - boost 异步读/写失败, "Insufficient system resources exist to complete the requested service"

c++ - boost asio streambuf 在调用 consume 后不释放内存?

c++ - 如何通过基类指针调用派生类的虚函数

c++ - g++ 的 std::list::sort 是否使迭代器无效?

c++ - g++ 编译不识别 '*' 使用 Armadillo 库进行矩阵乘法

c++ - 'auto' 和显式变量声明的行为不同