| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2453 人关注过本帖
标题:关于const, internal linkage, external linkage的问题
取消只看楼主 加入收藏
loriwang
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2007-8-24
收藏
 问题点数:0 回复次数:3 
关于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
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
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
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
快速回复:关于const, internal linkage, external linkage的问题
数据加载中...
 
   



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

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