次のコードで、特に header.c で問題が発生しました。ここでは、header.h の extern int x 変数にアクセスできません。なぜですか? .h の extern 変数はグローバルではありませんか? これを他のファイルで使用するにはどうすればよいですか?
===header.h===
#ifndef HDR_H
#define HDR_H
extern int x;
void function();
#endif
===header.c===
#include <stdio.h>
#include "header.h"
void function()
{
printf("%d", x); //****undefined reference to x, why?****
}
===sample.c===
int main()
{
int x = 1;
function();
printf("\n%d", x);
return 0;
}