标题:关于const, internal linkage, external linkage的问题
只看楼主
loriwang
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2007-8-24
 问题点数:0 回复次数:7 
关于const, internal linkage, external linkage的问题
书上说"A const in C++ defaults to internal linkage; that is, it is visible only within the file where it is defined and cannot be seen at link time by other translation units."

我的程序如下

//file test.cpp is a file which defines a const int which should have internal linkage.
//test.cpp

const int i=60;

----------------------

//file main.cpp uses the const int defined in file test.cpp
//main.cpp

#include <iostream>
using namespace std;

extern const int i;

int main()
{
cout<<i<<endl;
}

上面的程序编译和运行都没有问题, 但是如果test.cpp中i的定义变成 static int i=60; main.cpp 中的声明变成extern int i; 编译错误显示变量i未定义。请问internal linkage的意义到底是什么?

[此贴子已经被作者于2007-9-25 11:57:51编辑过]

搜索更多相关主题的帖子: linkage const internal cpp external 
2007-09-25 11:49
yuyunliuhen
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:1435
专家分:0
注 册:2005-12-12
得分:0 

Linkage
To understand the behavior of C and C++ programs, you need to know about linkage. In an executing program, an identifier is represented by storage in memory that holds a variable or a compiled function body. Linkage describes this storage as it is seen by the linker. There are two types of linkage: internal linkage and external linkage.


Internal linkage means that storage is created to represent the identifier only for the file being compiled. Other files may use the same identifier name with internal linkage, or for a global variable, and no conflicts will be found by the linker – separate storage is created for each identifier. Internal linkage is specified by the keyword static in C and C++.


External linkage means that a single piece of storage is created to represent the identifier for all files being compiled. The storage is created once, and the linker must resolve all other references to that storage. Global variables and function names have external linkage. These are accessed from other files by declaring them with the keyword extern. Variables defined outside all functions (with the exception of const in C++) and function definitions default to external linkage. You can specifically force them to have internal linkage using the static keyword. You can explicitly state that an identifier has external linkage by defining it with the extern keyword. Defining a variable or function with extern is not necessary in C, but it is sometimes necessary for const in C++.


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.


Go confidently in the  directions of your dreams,live the life you have imagined!Just do it!
It is no use learning without thinking!
2007-09-25 13:33
loriwang
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2007-8-24
得分:0 

Thanks yuyunliuhen, however my question is why "const int i" which has internal linkage in test.cpp file can be accessed in main.cpp via "extern const int i" statement. My understanding is that because const int i has the internal linkage in test.cpp file, it can't be accessed in other files even use an extern modifier. So when compiling, compiler should generates an error shows that viriable i is undefined. But the fact is there is no error and program shows the value of i.

rgds/lori


Nothing is everything
2007-09-25 14:18
yuyunliuhen
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:1435
专家分:0
注 册:2005-12-12
得分:0 
以下是引用loriwang在2007-9-25 14:18:48的发言:

My understanding is that because const int i has the internal linkage in test.cpp file, it can't be accessed in other files even use an extern modifier. So when compiling, compiler should generates an error shows that viriable i is undefined.

you are right.when the programme runs at my machine,It turns out to be: [Linker error] undefined reference to `i' .my environment id DEVC++


Go confidently in the  directions of your dreams,live the life you have imagined!Just do it!
It is no use learning without thinking!
2007-09-25 14:36
exqus
Rank: 1
等 级:新手上路
帖 子:24
专家分:0
注 册:2007-7-16
得分:0 
在C中static还有一层含义使用来限制全局变量/函数的作用域,是他们的作用域仅限制在本文件。
防止不同的人编写不同的函数/变量,在不同的文件重名
2007-09-25 16:21
loriwang
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2007-8-24
得分:0 
以下是引用yuyunliuhen在2007-9-25 14:36:20的发言:

you are right.when the programme runs at my machine,It turns out to be: [Linker error] undefined reference to `i' .my environment id DEVC++

My compiling environment is MinGW344. So, it seems that different environment with different result?


Nothing is everything
2007-09-25 21:34
yuyunliuhen
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:1435
专家分:0
注 册:2005-12-12
得分:0 
I hava never used it before,and it is my first time hear that.
Maybe the compile tools's standard is different,but I trust gcc.you can try to use it,then you will love it.

Go confidently in the  directions of your dreams,live the life you have imagined!Just do it!
It is no use learning without thinking!
2007-09-25 22:55
loriwang
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2007-8-24
得分:0 
er....MinGW344 uses gcc version 3.4.4....however I will check documents and test myself. Thanks yuyunliuhen again.

Nothing is everything
2007-09-26 10:22



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-172727-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.135838 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved