我有3个文件:
主要的cpp公司-
#include "test.hpp"
int main(void)
{
test x;
return (1);
}
测验水电站-
#ifndef TEST_HPP
#define TEST_HPP
class test
{
static int a;
public:
void func(void);
};
#endif
测验cpp公司-
#include "test.hpp"
int test::a = 0; // removing this line makes compilation fail
void test::func(void)
{
a--;
}
我编译时使用:
clang++ *.cpp -I .
,只有这3个文件在我的目录中。
编译失败消息为:
Undefined symbols for architecture x86_64:
"test::a", referenced from:
test::func() in test-8bbfc4.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
根据我的理解:
int test::a = 0; // removing this line makes compilation fail
正在初始化静态成员
a
到0,因为它已经是0,实际上没有任何意义。
为什么这会对汇编产生影响?