当我声明一个类bar的静态对象在一个函数中有一个显式的构造函数时,我得到了很多我不期望的stdc ++库开销(包括异常处理,尽管-fno-exceptions)
class bar
{
public:
bar() { a=0; };
private:
int a;
};
void func()
{
static bar tbar;
}
如果bar的构造函数没有显式声明,我不会得到这个开销。我正在使用gcc V4.5.2。
那里发生了什么?
答案 0 :(得分:1)
通过检查Itanium C ++ ABI,我发现编译器试图实现函数范围静态变量线程安全的保护变量:
if (obj_guard.first_byte == 0) {
if ( __cxa_guard_acquire (&obj_guard) ) {
try {
... initialize the object ...;
} catch (...) {
__cxa_guard_abort (&obj_guard);
throw;
}
... queue object destructor with __cxa_atexit() ...;
__cxa_guard_release (&obj_guard);
}
}
由于这个原因,包含异常处理等导致代码大小的大量增加......嵌入式项目是不可接受的。要禁用此线程安全行为,请使用编译器选项
<强> -fno-线程-静强>