如何停止g ++链接不需要的异常处理代码? [英] How can I stop g++ from linking unwanted exception-handling code?

查看:210
本文介绍了如何停止g ++链接不需要的异常处理代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用为arm-eabi编译的GCC / G ++开发嵌入式应用程序。由于资源限制,我试图禁用标准的C ++异常处理。我用-fno-exceptions
-nostartfiles -ffreestanding编译代码。



当一个类的全局实例存在,并且该类包含另一个类的实例作为成员,那么很多异常处理代码被链接。这不会那么糟,除了它还带来了很多stdio东西,如printf,fopen,fclose和其他FILE函数。这个应用程序没有文件系统,即使是这样,这些函数也浪费了太多的代码空间。



我理解即使使用-fno-exceptions, new使用异常,因为库没有非异常使用运算符new(除了new(nothrow))。我创建了用于操作符new和delete的替换,这些链接到输出以及不需要的标准库函数。



令我困惑的是,我不是呼叫新的任何地方。只有当全局对象包含所有这些代码都链接的另一个对象时。



例如:

  class UartA {
...
private:
Ringbuffer * rxbuf;
};

class UartB {
...
private:
Ringbuffer rxbuf;
};

如果创建了UartA的全局实例,则异常处理,运算符new和stdio不



如果创建了UartB的全局实例(其中rxbuf是一个实例而不是一个指针),那么不需要的代码会链接到。



UartA和UartB都不使用运算符new,exceptions或stdio。它们的区别仅在于rxbuf的类型。



你能建议如何防止链接额外的代码?

解决方案

我认为最接近你可以得到的是编译链接与-fno-exceptions和-fno-rtti。如果有更好的方法摆脱休息,我很高兴听到自己。



至于摆脱新的,试试-nostdlib。 / p>

I'm developing an embedded application using GCC/G++ compiled for arm-eabi. Due to resource constraints, I'm trying to disable the standard C++ exception handling. I'm compiling the code with "-fno-exceptions -nostartfiles -ffreestanding".

When a global instance of a class exists, and that class contains an instance of another class as a member, then a lot of exception handling code is being linked in. This wouldn't be so bad, except that it's also bringing in lots of stdio stuff, like printf, fopen, fclose and other FILE functions. This application has no filesystem, and even if it did, these functions waste too much code space.

I understand that even with -fno-exceptions, G++ links in an operator new that uses exceptions, because the library doesn't have a non-exception-using operator new (except new(nothrow)). I created replacements for operator new and delete, and these are linked into the output as well as the unwanted standard-library functions.

What puzzles me is that I'm not calling new anywhere. It's only when a global object contains another object that all this code is linked in.

For example:

class UartA {
...
private:
  Ringbuffer* rxbuf;
};

class UartB {
...
private:
  Ringbuffer rxbuf;
};

If a global instance of UartA is created, the exception handling, operator new, and stdio stuff are not linked in. This is what I want.

If a global instance of UartB is created (where rxbuf is an instance instead of a pointer), the unwanted code is linked in.

Neither UartA nor UartB use operator new, exceptions or stdio. They differ only by the type of rxbuf.

Can you suggest how to prevent linking the extra code? Also, why is this being linked in for UartB, but not UartA?

解决方案

I think the closest you can get is compiling and linking with -fno-exceptions and -fno-rtti. If there's a better way to get rid of the rest, I'd be glad to hear it myself.

As far as getting rid of new, try -nostdlib.

这篇关于如何停止g ++链接不需要的异常处理代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆