我可以在调用纯虚函数时禁用异常吗? [英] Can I disable exceptions for when a pure virtual function is called?
问题描述
我有一些如下所示的代码:
class可写{b $ b public:
virtual void putc(const char ch)= 0;
保护:
virtual〜Writable(){};
};
类可读{
public:
virtual char getc()= 0;
保护:
virtual〜Readable(){};
};
注意两个虚函数。使用 arm-none-eabi-gcc
,并与 -fno-exceptions
产生这个输出:
arm-none-eabi-size --format = berkeley bareCortexM.elf
文本数据bss dec hex文件名
108948 2304 2372 113624 1bbd8 bareCortexM.elf
用方法再次运行它存根代替纯虚函数产生:
arm-none-eabi-size --format = berkeley bareCortexM.elf
文本数据bss十进制十六进制文件名
47340 2296 304 49940 c314 bareCortexM.elf
巨大的差异似乎是由于例外。有没有什么办法可以防止这种情况发生?
http://elegantinvention.com/blog/information/smaller-binary-size-with-c-on-baremetal-g/rel =nofollow>在baremetal(g ++)上使用C ++减小二进制大小
提供
__ cxa_pure_virtual()
实现
如果您在任何地方使用纯虚函数,但禁用了异常,您可能会
注意到您的代码再次膨胀。
这发生在我身上,并花了一段时间来追查,哎呀!
检查最终二进制文件(来自objdump -h -C -S
)的汇编列表,它看起来像例外
正在回来!
我试过的一件事是完全链接
-nostdlib
,完全将libstdc ++拉出
图片。我提供了malloc,realloc,
免费的虚拟实现,以及其他一些我使用过的stdlib函数,但后来avr32-g ++
抱怨我之前没有见过:我缺少
__ cxa_pure_virtual()
。
Aha ,我认为,必须是它!在
特定函数的源代码中,可以在libstdc ++中找到,是调用
std :: terminate()
,在此处显示
。这次电话会在我可怜的AVR32的
闪存中抛出一个可爱的派对,在他们的路上践踏-fno-exceptions
。
无论如何,
__ cxa_pure_virtual()
是您
调用纯虚函数时实际调用的内容。像新
和删除
,
这可能是您想重写的东西,所以您自己的
调试/跟踪代码可以给你有用的反馈。这个实现是
的简单方法,只要确保使它externC,这样名称就不会被损坏: externCvoid __cxa_pure_virtual(){while(1); }
I have some code that looks like this:
class Writable {
public:
virtual void putc(const char ch) = 0;
protected:
virtual ~Writable() {};
};
class Readable {
public:
virtual char getc() = 0;
protected:
virtual ~Readable() {};
};
Notice the two virtual functions. Compiling this (along with my other code) using arm-none-eabi-gcc
, and linking with -fno-exceptions
produces this output:
arm-none-eabi-size --format=berkeley bareCortexM.elf
text data bss dec hex filename
108948 2304 2372 113624 1bbd8 bareCortexM.elf
Running it again with method stubs in place of pure virtual functions yields:
arm-none-eabi-size --format=berkeley bareCortexM.elf
text data bss dec hex filename
47340 2296 304 49940 c314 bareCortexM.elf
This huge difference seems to be due to exceptions. Is there any way that I can prevent this from happening?
This is described by this blog post: Smaller binary size with C++ on baremetal (g++)
Provide a
__cxa_pure_virtual()
implementationIf you use pure virtual functions anywhere but have disabled exceptions, you may notice your code suddenly inflate again.
This happened to me, and it took a while to track down, whoops!
Inspecting assembly listing of the final binary (fromobjdump -h -C -S
), it looked like exceptions were coming back!One thing I tried was linking with
-nostdlib
, completely pulling libstdc++ out of the picture. I provided dummy implementations of malloc, realloc, free, and a few other stdlib functions I used, but thenavr32-g++
complained about something I hadn’t seen before: I was missing__cxa_pure_virtual()
."Aha," I thought, "this has to be it!" In the source of that particular function, found in libstdc++, is a call to
std::terminate()
, seen here. That call threw a lovely party all over my poor AVR32′s flash memory, trampling on-fno-exceptions
on their way in.Anyway,
__cxa_pure_virtual()
is what actually gets called when you call a pure virtual function. Likenew
anddelete
, this is probably something you want to override anyway so your own debug/trace code can give you useful feedback. The implementation is straightforward, just be sure to make itextern "C"
so the name doesn’t get mangled:extern "C" void __cxa_pure_virtual() { while(1); }
这篇关于我可以在调用纯虚函数时禁用异常吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!