为什么在使用Visual Studio编译的C++中,const变量在obj文件中不可见?

huangapple go评论72阅读模式
英文:

Why aren't const variables visible in obj file after compilation in C++ using Visual Studio?

问题

我有一个VisualStudio的C++项目,里面有一个名为Test.c的文件,标记为'Compile as C++ Code (/TP)'。

Test.c文件中只包含以下变量:
const int test123 = 100;

编译后,我在Test.obj文件中找不到这个变量。我使用了'dumpbin /symbols'命令导出了obj文件。这个变量不在obj文件中可能的原因是什么?

  • obj文件只包含以下项目:
File Type: COFF OBJECT

COFF SYMBOL TABLE
000 010575C2 ABS    notype       Static       | @comp.id
001 80010390 ABS    notype       Static       | @feat.00
002 00000002 ABS    notype       Static       | @vol.md
003 00000000 SECT1  notype       Static       | .drectve
    Section length   41, #relocs    0, #linenums    0, checksum        0
    Relocation CRC 00000000
006 00000000 SECT2  notype       Static       | .debug$S
    Section length   F8, #relocs    0, #linenums    0, checksum        0
    Relocation CRC 00000000
009 00000000 SECT3  notype       Static       | .debug$T
    Section length   78, #relocs    0, #linenums    0, checksum        0
    Relocation CRC 00000000
00C 00000000 SECT4  notype       Static       | .chks64
    Section length   20, #relocs    0, #linenums    0, checksum        0
    Relocation CRC 00000000

String Table Size = 0x0 bytes

  Summary

          20 .chks64
          F8 .debug$S
          78 .debug$T
          41 .drectve

删除'const'后,变量在obj文件中可用。

英文:

I have a VisualStudio C++ project with a Test.c file and it is marked as 'Compile as C++ Code (/TP)'.

The Test.c contains only the below variable,
const int test123 = 100;

After compilation, I could not find this variable in Test.obj file. I have used 'dumpbin /symbols' command to export the obj file. What could be the reason for this variable not available in obj file?

  • objfile contains only below items
File Type: COFF OBJECT

COFF SYMBOL TABLE
000 010575C2 ABS    notype       Static       | @comp.id
001 80010390 ABS    notype       Static       | @feat.00
002 00000002 ABS    notype       Static       | @vol.md
003 00000000 SECT1  notype       Static       | .drectve
    Section length   41, #relocs    0, #linenums    0, checksum        0
    Relocation CRC 00000000
006 00000000 SECT2  notype       Static       | .debug$S
    Section length   F8, #relocs    0, #linenums    0, checksum        0
    Relocation CRC 00000000
009 00000000 SECT3  notype       Static       | .debug$T
    Section length   78, #relocs    0, #linenums    0, checksum        0
    Relocation CRC 00000000
00C 00000000 SECT4  notype       Static       | .chks64
    Section length   20, #relocs    0, #linenums    0, checksum        0
    Relocation CRC 00000000

String Table Size = 0x0 bytes

  Summary

          20 .chks64
          F8 .debug$S
          78 .debug$T
          41 .drectve

The variable is available in obj after removing the 'const'.

答案1

得分: 4

全局的C++ const变量具有静态链接,这就解释了为什么在目标文件中看不到它作为一个符号。

链接:https://learn.microsoft.com/en-us/cpp/error-messages/tool-errors/global-constants-in-cpp?view=msvc-170

此外,这在这个回答中也有很好的解释(https://stackoverflow.com/questions/998425/why-does-const-imply-internal-linkage-in-c-when-it-doesnt-in-c)。

英文:

Global const variables in C++ have static linkage and that explains why you don't see it as a symbol in the object file.

https://learn.microsoft.com/en-us/cpp/error-messages/tool-errors/global-constants-in-cpp?view=msvc-170

Also, this is well explained in this answer.

huangapple
  • 本文由 发表于 2023年5月26日 00:22:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76334413.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定