我试图了解头文件中外部和全局变量声明的用法,所以我想出了以下用 C 编写的测试程序。
main.c 文件
//main.c
#include "global.h"
#include <stdio.h>
int nExternValue = 6;
int main(int argc, char* argv[])
{
printf("%d \n", nValue);
printf("%d \n", nExternValue);
AddToValue();
printf("%d \n", nValue);
printf("%d \n", nExternValue);
}
global.h 文件
#ifndef _GLOBAL_H
#define _GLOBAL_H
//WRONG! do not declare a variable here
int nValue;
//OK! extern variable makes it a global accessable variable
extern int nExternValue;
//OK! function prototype can be placed in h file
int AddToValue();
#endif
和一个实现 AddToValue 函数的 AddValue.c 文件。
#include "global.h"
int AddToValue() {
nValue++;
nExternValue++;
}
我使用 gcc 编译了应用程序,并运行了它:
$ gcc main.c AddValue.c -o test
$./test
0
6
1
7
我使用 g++ 编译了应用程序并得到以下链接器错误:
$ g++ main.c AddValue.c -o test
/tmp/ccFyGDYM.o:(.bss+0x0): multiple definition of `nValue'
/tmp/cc3ixXdu.o:(.bss+0x0): first defined here
collect2: ld returned 1 exit status
为什么 gcc 链接器不产生错误?我虽然 nValue 变量会被声明多次,这会产生错误!
$ gcc --version
gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ g++ --version
g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.