为什么 C++ 异常处理, 运行与预期的不一致?
源码 main.cpp
#include <rtthread.h>
#define DBG_TAG "main"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>
#include <iostream>
#include <array>
#include <exception>
using namespace rtthread;
int main(int argc, const char *argv[])
{
using namespace std;
array<int, 5> arr{};
try
{
cout << arr.at(5) << endl;
}
catch (exception &e)
{
cout << e.what() << endl;
}
return RT_EOK;
}
预期
array::at: __n (which is 5) >= _Nm (which is 5)
结果
thread:main abort!
MCU | Frequency | ROM | RAM |
---|---|---|---|
STM32F411CEU6 | 100Mhz | 512KB | 128KB |
RT-Thread 版本 | RT-Thread Studio 版本 | 构建ID | OS | Java version |
---|---|---|---|---|
4.0.3(latest) | 1.1.3 | 202007312100 | Windows 10, v.10.0, x86_64 / win32 | 1.8.0_144 |
比 RT-Thread Studio 下好多了,不会因为throw
异常导致thread abort
MDK-ARM Plus | C Compiler | Assembler | Linker/Locator |
---|---|---|---|
V5.31.0.0 | V6.14 | V6.14 | V6.14 |
添加编译参数 -fexceptions
源码 main.cpp
#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
#include <exception>
#include <array>
int main(int argc, const char *argv[])
{
std::array<int, 5> arr{};
try
{
rt_kprintf("%d\n", arr.at(5));
}
catch (const std::exception& e)
{
rt_kprintf("%s\n", e.what());
}
rt_kprintf("Nice!\n");
return RT_EOK;
}
运行结果
GNU ARM GCC
设置问题,那么应该如何设置?
请问rtconfig.py里面怎么写?
您好,我已经在rtconfig.py中修改了如下行



编译运行之后还是报错
相关测试代码和楼主一致
@imgcre 工具链、c++版本都是rt thread studio默认,整个项目就开了libc和c++支持