今天,我发现了一个关于 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
问题。
这不会导致任何明显的问题,但我想知道:
- 为什么定义的构造函数会被列出两次?
- 为什么这不会导致“符号__的多重定义”问题?
EDIT:对于 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
。这是什么意思?
<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
等等,为什么这简单?这个类没有基础。为什么它有一个“完整的对象构造函数”和每个都有一个“基础对象构造函数”?
This Q&A对我来说这意味着这只是多态性支持的一个副产品,尽管在这种情况下实际上并不需要它。
请注意,
c++filt
曾在其解码输出中包含此信息,but doesn't any more .This forum post问了同样的问题,唯一的回答并没有更好地回答它,除了暗示GCC 可以在不涉及多态性时避免发出两个构造函数,并且这种行为应该是以后会改进的。
This newsgroup posting描述了由于这种双重发射而在构造函数中设置断点的问题。再次声明,问题的根源在于对多态性的支持。
事实上,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 :
“完整的对象构造器”。它还构造了虚拟基类。
“基础对象构造函数”。它创建对象本身,以及数据成员和非虚拟基类。
“分配对象构造函数”。它完成了完整的对象构造函数所做的所有事情,而且它调用 operator new 来实际分配内存...但显然这通常不常见。
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/6921295/