ヘッダー ファイルでの extern およびグローバル変数宣言の使用法を理解しようとしているので、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.