How to add exception support to newlib-nano?

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

How to add exception support to newlib-nano?

跳至解决方案
5,917 次查看
v_canoz
Contributor III

Hi,

I'm using MCUXpresso with a C++ project.


Today I ran into an issue: my very simple try catch block was not working.

I found out the issue was due to newlib-nano having exceptions handling disabled by default.

In this project, I don't want to use newlib, it would simply not fit into the small flash of the LPC845 I am using.

On this post Question #230716 : Questions : GNU Arm Embedded Toolchain, I see it is possible to add support of exceptions to newlib-nano, but is this possible using MCUXpresso?

I am re using a lot of code using exceptions, I would rather spend some time adding the support to newlib nano than modify all the existing code base...

Many thanks,

Victor

标签 (1)
0 项奖励
回复
1 解答
5,804 次查看
ErichStyger
Specialist I

Exception handling is usually the most expensive feature in a language (e.g. C++ or C#). Just keep in mind or imagine what needs to be done to support exceptions.

Not only it requires special hooks on each stack frame (increasing stack size), it requires a runtime system to deal with exceptions: going through a list of descriptors, finding the matching catch block. This alone is rather complex, but gets more complex due the fact that the exception runtime needs to unwind the stack, including caring and calling all the different destructors, and continuing iterating the lists of more catch blocks and finding matching handlers. Even more, you can throw new exceptions in a catch block making things yet a bit more complex to deal with.

The exception feature is very useful and comes handy, but compared to all the other language features it is the most expensive one to me. That's why in some programming languages you can turn it off. Which might lead itself to a problem: check the case of the Ariane V rocket failure where exception handling had been turned off to meet the performance requirements.

Other than that: have a read at How is the C++ exception handling runtime implemented? - Stack Overflow  or a good video about implementation details:  YouTube 

I hope it helps,

Erich

在原帖中查看解决方案

0 项奖励
回复
6 回复数
4,114 次查看
HosseinSagha
Contributor I

Use CException. It's got limitations, but better than nothing.

https://github.com/ThrowTheSwitch/CException

3,931 次查看
HosseinSagha
Contributor I

Ahh. not really. longjmp bypasses destructors. not c++ safe.

0 项奖励
回复
5,804 次查看
converse
Senior Contributor V

AFAIK, MCUXpresso uses the standard GNU ARM distribution, so anything that is possible with that, is possible with MCUXpresso. Just download the source, and build...

However, adding Exceptions back into newlib-nano will almost turn it back into newlib - you are going to lose most (but not all, admittedly) of the size advantages of 'nano.

0 项奖励
回复
5,804 次查看
v_canoz
Contributor III

So I guess I'll add a lot of this:

#ifdef __cpp_exceptions

...

#endif

to my exisiting code.

Just wonder why exceptions takes so much room. Can a part of their functionality be disabled in order to keep the core of the exceptions?

Thanks

0 项奖励
回复
5,805 次查看
ErichStyger
Specialist I

Exception handling is usually the most expensive feature in a language (e.g. C++ or C#). Just keep in mind or imagine what needs to be done to support exceptions.

Not only it requires special hooks on each stack frame (increasing stack size), it requires a runtime system to deal with exceptions: going through a list of descriptors, finding the matching catch block. This alone is rather complex, but gets more complex due the fact that the exception runtime needs to unwind the stack, including caring and calling all the different destructors, and continuing iterating the lists of more catch blocks and finding matching handlers. Even more, you can throw new exceptions in a catch block making things yet a bit more complex to deal with.

The exception feature is very useful and comes handy, but compared to all the other language features it is the most expensive one to me. That's why in some programming languages you can turn it off. Which might lead itself to a problem: check the case of the Ariane V rocket failure where exception handling had been turned off to meet the performance requirements.

Other than that: have a read at How is the C++ exception handling runtime implemented? - Stack Overflow  or a good video about implementation details:  YouTube 

I hope it helps,

Erich

0 项奖励
回复
5,804 次查看
v_canoz
Contributor III

It does, thanks !
Victor

0 项奖励
回复