C 的 const 与 C++ 的 const 不一样。C 的 const 编译后会产生相应的全局符号,
C++ 的 const 不会产生全局符号。为了避免连接时出现符号重定义的错误,在 C
里面一般都只能用宏来替代 const。
$ cat const.h
#ifndef CONST_H__
#define CONST_H__
const int i = 100;
#endif /* CONST_H__ */
$ cat const.c
#include <stdio.h>
#include "const.h"
int main()
{
printf("%d\n", i);
return 0;
}
$ cat const.cpp
#include <iostream>
#include "const.h"
using namespace std;
int main()
{
cout << i << endl;
return 0;
}
$ gcc -c const.c
$ nm -s const.o
00000000 R i
00000000 T main
U printf
$ g++ -c const.cpp
$ nm -s const.o
U __cxa_atexit
U __dso_handle
000000a2 t _GLOBAL__I_main
U __gxx_personality_v0
00000000 T main
00000046 t __tcf_0
0000005e t _Z41__static_initialization_and_destruction_0ii
U _ZNSolsEi
U _ZNSolsEPFRSoS_E
U _ZNSt8ios_base4InitC1Ev
U _ZNSt8ios_base4InitD1Ev
U _ZSt4cout
U _ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_
00000000 b _ZSt8__ioinit
【 在 JulyClyde (七月) 的大作中提到: 】
: C语言有没有const啊?改const算了
--
FROM 162.105.242.*