今天,我发现了一个关于 g++
相当有趣的事情。或 nm
...构造函数定义在库中似乎有两个条目。
我有一个标题 thing.hpp
:
class Thing
{
Thing();
Thing(int x);
void foo();
};
和
thing.cpp
:#include "thing.hpp"
Thing::Thing()
{ }
Thing::Thing(int x)
{ }
void Thing::foo()
{ }
我编译这个:
g++ thing.cpp -c -o libthing.a
然后,我运行
nm
在上面:%> nm -gC libthing.a
0000000000000030 T Thing::foo()
0000000000000022 T Thing::Thing(int)
000000000000000a T Thing::Thing()
0000000000000014 T Thing::Thing(int)
0000000000000000 T Thing::Thing()
U __gxx_personality_v0
如您所见,
Thing
的两个构造函数在生成的静态库中列出了两个条目。我的 g++
是 4.4.3,但同样的行为发生在 clang
,所以它不仅仅是一个 gcc
问题。这不会导致任何明显的问题,但我想知道:
编辑 :对于 Carl,没有
C
的输出争论:%> nm -g libthing.a
0000000000000030 T _ZN5Thing3fooEv
0000000000000022 T _ZN5ThingC1Ei
000000000000000a T _ZN5ThingC1Ev
0000000000000014 T _ZN5ThingC2Ei
0000000000000000 T _ZN5ThingC2Ev
U __gxx_personality_v0
如您所见……同一个函数正在生成多个符号,这还是很奇怪的。
当我们在做的时候,这里是生成的程序集的一部分:
.globl _ZN5ThingC2Ev
.type _ZN5ThingC2Ev, @function
_ZN5ThingC2Ev:
.LFB1:
.cfi_startproc
.cfi_personality 0x3,__gxx_personality_v0
pushq %rbp
.cfi_def_cfa_offset 16
movq %rsp, %rbp
.cfi_offset 6, -16
.cfi_def_cfa_register 6
movq %rdi, -8(%rbp)
leave
ret
.cfi_endproc
.LFE1:
.size _ZN5ThingC2Ev, .-_ZN5ThingC2Ev
.align 2
.globl _ZN5ThingC1Ev
.type _ZN5ThingC1Ev, @function
_ZN5ThingC1Ev:
.LFB2:
.cfi_startproc
.cfi_personality 0x3,__gxx_personality_v0
pushq %rbp
.cfi_def_cfa_offset 16
movq %rsp, %rbp
.cfi_offset 6, -16
.cfi_def_cfa_register 6
movq %rdi, -8(%rbp)
leave
ret
.cfi_endproc
所以生成的代码是……嗯……是一样的。
编辑 :为了查看实际调用的构造函数,我更改了
Thing::foo()
对此:void Thing::foo()
{
Thing t;
}
生成的程序集是:
.globl _ZN5Thing3fooEv
.type _ZN5Thing3fooEv, @function
_ZN5Thing3fooEv:
.LFB550:
.cfi_startproc
.cfi_personality 0x3,__gxx_personality_v0
pushq %rbp
.cfi_def_cfa_offset 16
movq %rsp, %rbp
.cfi_offset 6, -16
.cfi_def_cfa_register 6
subq $48, %rsp
movq %rdi, -40(%rbp)
leaq -32(%rbp), %rax
movq %rax, %rdi
call _ZN5ThingC1Ev
leaq -32(%rbp), %rax
movq %rax, %rdi
call _ZN5ThingD1Ev
leave
ret
.cfi_endproc
所以它正在调用完整的对象构造函数。
最佳答案
我们首先声明 GCC follows the Itanium C++ ABI .
根据 ABI,您的 Thing::foo()
的错误名称很容易解析:
_Z | N | 5Thing | 3foo | E | v
prefix | nested | `Thing` | `foo`| end nested | parameters: `void`
您可以类似地读取构造函数名称,如下所示。注意构造函数“name”是如何没有给出的,而是一个 C
条款:_Z | N | 5Thing | C1 | E | i
prefix | nested | `Thing` | Constructor | end nested | parameters: `int`
但这是什么
C1
?您的拷贝有 C2
.这是什么意思?那么,this is quite simple too :
<ctor-dtor-name> ::= C1 # complete object constructor
::= C2 # base object constructor
::= C3 # complete object allocating constructor
::= D0 # deleting destructor
::= D1 # complete object destructor
::= D2 # base object destructor
等等,为什么这么简单?这个类没有基础。为什么它有一个“完整的对象构造函数”和 每个的“基本对象构造函数”?
c++filt
过去常常将此信息包含在其解构输出中,but doesn't any more .事实上,this is listed as a GCC "known issue" :
G++ emits two copies of constructors and destructors.
In general there are three types of constructors (and destructors).
- The complete object constructor/destructor.
- The base object constructor/destructor.
- The allocating constructor/deallocating destructor.
The first two are different, when virtual base classes are involved.
这些不同构造函数的含义seems to be as follows :
If you have no virtual base classes, [the first two] are are identical; GCC will, on sufficient optimization levels, actually alias the symbols to the same code for both.
关于c++ - 构造函数符号的双重发射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45267549/