-3
我正在写一个C++程序和代码的一部分是像下面用static关键字声明一个对象是什么意思?
#include <cstdio>
class A
{
public:
A()
{
puts("Create");
}
~A()
{
puts("Destroy");
}
};
void Test()
{
puts("In");
static A a; //declare an object with static keyword.
}
int main()
{
Test();
puts("Out");
return 0;
}
我认为静态关键字将使它像一个全局变量,而这个程序的输出应该是
Create
In
Out
Destroy
但事实是,当我使用GCC编译它,我得到了一些链接错误,
/tmp/cc1JwaFb.o: In function `Test()':
test.cpp:(.text+0x23): undefined reference to `__cxa_guard_acquire'
test.cpp:(.text+0x46): undefined reference to `__cxa_guard_release'
test.cpp:(.text+0x6e): undefined reference to `__cxa_guard_abort'
/tmp/cc1JwaFb.o:(.eh_frame+0x6b): undefined reference to `__gxx_personality_v0'
collect2: error: ld returned 1 exit status
但我可以用G ++,这outputed
In
Create
Out
Destroy
所以编译它,为什么这会发生?当用静态关键字声明对象时,这意味着什么?
我用C编译器编译C++程序,为什么它不起作用? “GCC”是一个多编译器,“gcc”用于C程序。用于C++程序的“g ++”。 – Stargateur
本地静态变量具有静态存储持续时间,并且在通过控制达到它之前,它不会被初始化 – cshu