为什么太阳 C++使用调试信息进行编译时,编译器会更改符号名称吗?
我有这个源文件:
// ConstPointer.cpp
const short * const const_short_p_const = 0;
const short * const_short_p = 0;
并使用和不使用调试信息对其进行编译(SUN C++ 编译器 5.10):
# CC ConstPointer.cpp -c -o ConstPointer.o
# CC -g ConstPointer.cpp -c -o ConstPointer-debug.o
以下是目标文件的符号名称不使用调试信息:
# nm -C ConstPointer.o
ConstPointer.o:
[Index] Value Size Type Bind Other Shndx Name
[2] | 0| 0|SECT |LOCL |0 |10 |
[3] | 0| 0|SECT |LOCL |0 |9 |
[4] | 0| 0|OBJT |LOCL |0 |6 |Bbss.bss
[1] | 0| 0|FILE |LOCL |0 |ABS |ConstPointer.cpp
[5] | 0| 0|OBJT |LOCL |0 |3 |Ddata.data
[6] | 0| 0|OBJT |LOCL |0 |5 |Dpicdata.picdata
[7] | 0| 0|OBJT |LOCL |0 |4 |Drodata.rodata
[9] | 4| 4|OBJT |GLOB |0 |3 |const_short_p
[8] | 0| 4|OBJT |LOCL |0 |3 |const_short_p_const
以下是目标文件的符号名称带有调试信息:
# nm -C ConstPointer-debug.o
ConstPointer-debug.o:
[Index] Value Size Type Bind Other Shndx Name
[4] | 0| 0|SECT |LOCL |0 |9 |
[2] | 0| 0|SECT |LOCL |0 |8 |
[3] | 0| 0|SECT |LOCL |0 |10 |
[10] | 0| 4|OBJT |GLOB |0 |3 |$XAHMCqApZlqO37H.const_short_p_const
[5] | 0| 0|NOTY |LOCL |0 |6 |Bbss.bss
[1] | 0| 0|FILE |LOCL |0 |ABS |ConstPointer.cpp
[6] | 0| 0|NOTY |LOCL |0 |3 |Ddata.data
[7] | 0| 0|NOTY |LOCL |0 |5 |Dpicdata.picdata
[8] | 0| 0|NOTY |LOCL |0 |4 |Drodata.rodata
[9] | 4| 4|OBJT |GLOB |0 |3 |const_short_p
为什么变量const_short_p_const
有另一个符号名称?使用调试信息进行编译时,g++
不会更改它。对我来说这看起来像是一个编译器错误。你怎么认为?第二个const(常量指针)导致了这一点。
编辑德鲁·霍尔的评论: 例如,您有两个文件:
// ConstPointer.cpp
const short * const const_short_p_const = 0;
void foo();
int main(int argc, const char *argv[]) {
foo();
return 0;
}
编译
// ConstPointer2.cpp
extern const short * const const_short_p_const;
void foo() {
short x = *const_short_p_const;
}
没问题:
# CC ConstPointer2.cpp -g -c -o ConstPointer2.o
# CC ConstPointer.cpp -g -c -o ConstPointer.o
但链接不起作用,因为符号不同! ConstPointer2.o 中的符号名称为 const_short_p_const
,但 ConstPointer.o 中的符号名称为 $XAHMCqApZlqO37H.const_short_p_const
。
# CC ConstPointer.o ConstPointer2.o -o ConstPointer
Undefined first referenced
symbol in file
const_short_p_const ConstPointer2.o
I have this source file:
// ConstPointer.cpp
const short * const const_short_p_const = 0;
const short * const_short_p = 0;
and compiled it with and without debug infos (SUN C++ Compiler 5.10):
# CC ConstPointer.cpp -c -o ConstPointer.o
# CC -g ConstPointer.cpp -c -o ConstPointer-debug.o
Here are the symbol names of the object file without debug information:
# nm -C ConstPointer.o
ConstPointer.o:
[Index] Value Size Type Bind Other Shndx Name
[2] | 0| 0|SECT |LOCL |0 |10 |
[3] | 0| 0|SECT |LOCL |0 |9 |
[4] | 0| 0|OBJT |LOCL |0 |6 |Bbss.bss
[1] | 0| 0|FILE |LOCL |0 |ABS |ConstPointer.cpp
[5] | 0| 0|OBJT |LOCL |0 |3 |Ddata.data
[6] | 0| 0|OBJT |LOCL |0 |5 |Dpicdata.picdata
[7] | 0| 0|OBJT |LOCL |0 |4 |Drodata.rodata
[9] | 4| 4|OBJT |GLOB |0 |3 |const_short_p
[8] | 0| 4|OBJT |LOCL |0 |3 |const_short_p_const
Here are the symbol names of the object file with debug information:
# nm -C ConstPointer-debug.o
ConstPointer-debug.o:
[Index] Value Size Type Bind Other Shndx Name
[4] | 0| 0|SECT |LOCL |0 |9 |
[2] | 0| 0|SECT |LOCL |0 |8 |
[3] | 0| 0|SECT |LOCL |0 |10 |
[10] | 0| 4|OBJT |GLOB |0 |3 |$XAHMCqApZlqO37H.const_short_p_const
[5] | 0| 0|NOTY |LOCL |0 |6 |Bbss.bss
[1] | 0| 0|FILE |LOCL |0 |ABS |ConstPointer.cpp
[6] | 0| 0|NOTY |LOCL |0 |3 |Ddata.data
[7] | 0| 0|NOTY |LOCL |0 |5 |Dpicdata.picdata
[8] | 0| 0|NOTY |LOCL |0 |4 |Drodata.rodata
[9] | 4| 4|OBJT |GLOB |0 |3 |const_short_p
Why has the variable const_short_p_const
another symbol name? g++
does not change it, when compiling with debug information. It looks like a compiler bug to me. What do you think? The second const
(constant pointer) leads to this.
EDIT for Drew Hall's comment:
For example you have two files:
// ConstPointer.cpp
const short * const const_short_p_const = 0;
void foo();
int main(int argc, const char *argv[]) {
foo();
return 0;
}
and
// ConstPointer2.cpp
extern const short * const const_short_p_const;
void foo() {
short x = *const_short_p_const;
}
Compiling is fine:
# CC ConstPointer2.cpp -g -c -o ConstPointer2.o
# CC ConstPointer.cpp -g -c -o ConstPointer.o
but linking does not work because the symbols differ! The symbol name in ConstPointer2.o is const_short_p_const
, but the symbol name in ConstPointer.o is $XAHMCqApZlqO37H.const_short_p_const
.
# CC ConstPointer.o ConstPointer2.o -o ConstPointer
Undefined first referenced
symbol in file
const_short_p_const ConstPointer2.o
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许这与全局
const
变量在 C++ 中隐式static
的事实有关?Maybe this is linked to the fact that a global
const
variable is implicitelystatic
in C++?