4

私はこのソースファイルを持っています:

// 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++デバッグ情報を使用してコンパイルする場合、変更されません。私にはコンパイラのバグのように見えます。どう思いますか?2番目のconst(定数ポインター)はこれにつながります。

Drew Hallのコメントを編集し ます。たとえば、2つのファイルがあります。

// 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
4

1 に答える 1

2

constたぶん、これはグローバル変数が暗黙的staticにC ++にあるという事実に関連していますか?

于 2011-10-28T07:29:13.540 に答える