由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - c++ does not check const for extern variable?
相关主题
问一个windows下编译openGL code的问题请教一个用gsl库的问题
fatal error LNK1120: 1 unresolved externalsdefault linkage for const global variable in C/C++
不用头文件,如何调用函数?C++What're the three types of memory allocated for C++ variables?
一个VC++ .net里编译C程序的问题请教struct inside class的问题(C++)
C++ template question with friend ostreamc++ 弱问题:static const char* const 这两个const 分别是什么意思?
调用win32 DLL的问题How to link math library in VC2005?
Visual C++ 高手帮忙,一个Link ErrorC++ Q07: unnamed namespace
function declarationinline到底能省多少时间?
相关话题的讨论汇总
话题: extern话题: const话题: linkage话题: int话题: linker
进入Programming版参与讨论
1 (共1页)
r*******y
发帖数: 1081
1
//1.cpp
extern const int i = 1;
//2.cpp
#include
using namespace std;
extern int i;
int main(){
i = 2;
cout < }
Nothing wrong to compile but segmentation fault when running
p***o
发帖数: 1252
2
I got a linking error:
>cl 1.cpp 2.cpp /EHsc
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.30319.01 for 80
x86
Copyright (C) Microsoft Corporation. All rights reserved.
1.cpp
2.cpp
Generating Code...
Microsoft (R) Incremental Linker Version 10.00.30319.01
Copyright (C) Microsoft Corporation. All rights reserved.
/out:1.exe
1.obj
2.obj
2.obj : error LNK2019: unresolved external symbol "int i" (?i@@3HA) referenc
ed in function _main
1.exe : fatal error LNK1120: 1 unresolved externals

【在 r*******y 的大作中提到】
: //1.cpp
: extern const int i = 1;
: //2.cpp
: #include
: using namespace std;
: extern int i;
: int main(){
: i = 2;
: cout <: }

r*******y
发帖数: 1081
3
g++ -o 2 2.cpp 1.cpp
it is ok in this way to compile

80

【在 p***o 的大作中提到】
: I got a linking error:
: >cl 1.cpp 2.cpp /EHsc
: Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.30319.01 for 80
: x86
: Copyright (C) Microsoft Corporation. All rights reserved.
: 1.cpp
: 2.cpp
: Generating Code...
: Microsoft (R) Incremental Linker Version 10.00.30319.01
: Copyright (C) Microsoft Corporation. All rights reserved.

p***o
发帖数: 1252
4
Good to know. Then we need someone to dig the standard to see
whether g++ is not conforming or VC is doing something extra.

【在 r*******y 的大作中提到】
: g++ -o 2 2.cpp 1.cpp
: it is ok in this way to compile
:
: 80

t****t
发帖数: 6806
5
3.5 [Program and Linkage]
10 After all adjustments of types (during which typedefs (7.1.3) are
replaced by their definitions), the types specified by all declarations
referring to a given object or function shall be identical, except that
declarations for an array object can specify array types that differ by the
presence or absence of a major array bound (8.3.4). A violation of this rule
on type identity does not require a diagnostic.

【在 p***o 的大作中提到】
: Good to know. Then we need someone to dig the standard to see
: whether g++ is not conforming or VC is doing something extra.

P********e
发帖数: 2610
6
const implies local linkage
extern means external linkage
combining them together won't work.
Solution:
define: const int i = 1; in every compilation unit, and linker will merge
the duplicates.
It has the same effect as extern const.

【在 r*******y 的大作中提到】
: //1.cpp
: extern const int i = 1;
: //2.cpp
: #include
: using namespace std;
: extern int i;
: int main(){
: i = 2;
: cout <: }

t****t
发帖数: 6806
7
typical PaulPierce c++ post: it's hard to find a correct statement.
const implies INTERNAL linkage (not local linkage, there is no such thing as
"local linkage") IF it is not declared extern. [3.5, 3]
extern const int c = 1;
is explicitly allowed by standard. it is even in the example. [3.1, 3] so
combining extern and const WILL work.
defining const int i=1 in every compilation unit is fine. however, linker
will NOT merge them, because each "const int i=1" has internal linkage and
they shall NOT be merged. in fact, most probably linker will NOT see this
symbol exported at all. and it does NOT has the same effect as extern const,
because in each compilation unit, the address of i is not the same. (most
probably you won't use the address, of course.)
any questions?

【在 P********e 的大作中提到】
: const implies local linkage
: extern means external linkage
: combining them together won't work.
: Solution:
: define: const int i = 1; in every compilation unit, and linker will merge
: the duplicates.
: It has the same effect as extern const.

M*********t
发帖数: 257
8
You are right
Automatic (local) variables exist only temporarily, on the stack, while a
function is being called. The linker doesn’t know about automatic variables
, and so these have no linkage.

as

【在 t****t 的大作中提到】
: typical PaulPierce c++ post: it's hard to find a correct statement.
: const implies INTERNAL linkage (not local linkage, there is no such thing as
: "local linkage") IF it is not declared extern. [3.5, 3]
: extern const int c = 1;
: is explicitly allowed by standard. it is even in the example. [3.1, 3] so
: combining extern and const WILL work.
: defining const int i=1 in every compilation unit is fine. however, linker
: will NOT merge them, because each "const int i=1" has internal linkage and
: they shall NOT be merged. in fact, most probably linker will NOT see this
: symbol exported at all. and it does NOT has the same effect as extern const,

1 (共1页)
进入Programming版参与讨论
相关主题
inline到底能省多少时间?C++ template question with friend ostream
C++里 variable declaration 有什么用?调用win32 DLL的问题
C++怎样设置全局变量Visual C++ 高手帮忙,一个Link Error
没完的“unresolved external symbol” in c++ compilingfunction declaration
问一个windows下编译openGL code的问题请教一个用gsl库的问题
fatal error LNK1120: 1 unresolved externalsdefault linkage for const global variable in C/C++
不用头文件,如何调用函数?C++What're the three types of memory allocated for C++ variables?
一个VC++ .net里编译C程序的问题请教struct inside class的问题(C++)
相关话题的讨论汇总
话题: extern话题: const话题: linkage话题: int话题: linker