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,
|