测试用例如下:
// test.cpp
class X {
public:
X();
};
X::X() { }
void foo() {
X x;
}
编译它并读取目标文件中的符号,如下所示:
[root@localhost tmp]# g++ -c test.cpp
[root@localhost tmp]# readelf -s -W test.o
符号表 '.symtab' 包含 12 个条目:
Num:值大小类型绑定 Vis Ndx 名称
0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
1: 0000000000000000 0 FILE LOCAL DEFAULT ABS test.cpp
2: 0000000000000000 0 SECTION LOCAL DEFAULT 1
3: 0000000000000000 0 SECTION LOCAL DEFAULT 3
4: 0000000000000000 0 SECTION LOCAL DEFAULT 4
5: 0000000000000000 0 SECTION LOCAL DEFAULT 6
6: 0000000000000000 0 SECTION LOCAL DEFAULT 7
7: 0000000000000000 0 SECTION LOCAL DEFAULT 5
8: 0000000000000000 10 FUNC GLOBAL DEFAULT 1 _ZN1XC2Ev => X::X()
9: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND __gxx_personality_v0
10: 0000000000000000 10 FUNC GLOBAL DEFAULT 1 _ZN1XC1Ev => X::X()
11: 000000000000000a 22 FUNC GLOBAL DEFAULT 1 _Z3foov
[root@localhost tmp]# c++filt _ZN1XC1Ev
X::X()
[root@localhost tmp]# c++filt _ZN1XC2Ev
X::X()
为什么g ++会生成两个具有不同名称的构造函数(_ZN1XC1Ev
和_ZN1XC2Ev
)?