我尝试在没有 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, unlessBOOST_EXCEPTION_DISABLE
is also defined, users can still examine the exception object for any data added at the point of the throw, or useboost::diagnostic_information
(of course underBOOST_NO_EXCEPTIONS
, the user-definedboost::throw_exception
is not allowed to return to the caller.)
我认为这与独立的 Asio 密切相关
关于c++ - 没有 RTTI 的 ASIO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74098761/