c++ extern 示例
来源:互联网 发布:淘宝 sc 查询 编辑:程序博客网 时间:2024/06/03 17:49
test.h
#ifndef HEAD_H#define HEAD_Hextern double pi;#endif
test.cpp
#include"test.h"double pi = 3.14;int add(int x, int y){ return x + y;}
main.cpp
#include<iostream>#include<string>#include"test.h"using namespace std;string str;int iVal;extern double pi;extern int add(int, int);int main(){ const int iBufsize = 100; string str("inter str"); string str2; cout << "hello world." << endl; cout << iVal << endl; cout << str <<"|" << endl; cout << str2 <<"|" << endl; cout << "const int : " << iBufsize << endl; cout << pi << endl; cout << "-------------------" << endl; int j = 100; const int &ref1 = iBufsize; const int &ref2 = j; j = j + 1; cout << ref1 << endl; cout << ref2 << endl; cout << add(ref1 , ref2) << endl; return 0;}
makefile
exe: main.o test.o
g++ main.o test.o -o exe
main.o: main.cpp test.h
g++ -c main.cpp
test.o: test.cpp test.h
g++ -c test.cpp
clean :
rm *.o exe
另外:当我们在头文件中定义了const变量后,每个包含该头文件的源文件都有自己的const变量,其名称和值是一样。但不是一个。因为const变量默认的作用域是定义它的文件,因此各个文件定义自己的const变量是合法的。
试了一下,如果在test.h中加入 const int iConst = 100 , 编译通过,运行通过
如果在test.h加入 int inonConst = 100; 编译报错: multiple definition of `inonConst'
- extern &&extern c
- extern, extern "C",__stdcall
- extern与extern "C"
- extern 和extern "C"
- extern与extern “C”
- extern 与 extern "C"
- extern 和 extern“C”
- c/c++:extern,extern "C"
- extern“C” ???
- extern "C"
- C extern
- extern"c"
- extern "C"
- extern "C"
- extern "C" {}
- C extern
- extern "C"
- extern c
- 正则表达式参考手册
- Java字符串问题Top10
- 用dos命令破除unix管理员口令
- [Erlang 0009] Erlang 杂记
- java读取文件数据进行一些简单处理的例子
- c++ extern 示例
- mootools 的源代码TYPE 及Class解析
- BCB修改Console重新默认图标
- Linux 内核参数 和 Oracle相关参数调整 .
- centos下读取某个文件的头几行或末几行 (head 、tail)
- 网站asp漏洞小总结
- javaweb三大框架SSH解读
- 系统总结
- 盐蒸橙子治疗咳嗽 放倒一切止咳药消炎药