我在下面的代码中遇到了一些问题,特别是在header.c中,我无法在header.h中访问extern int x变量...为什么? .h中的extern变量不是全局变量吗?我如何在其他文件上使用它?C未定义的参考
=== 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;
}
可能之前'在主功能x'删除'int'。这将阻止在主函数中创建一个新的局部变量,其名称与全局变量 – bph 2012-08-08 09:08:48
(已删除;意外添加的注释)相同 – 2012-08-08 09:13:14
另请参见有关[http:// stackoverflow。COM /问题/ 7610321 /差-A-的extern-INT-A-42之间-的extern-INT-] [1] [1]:http://stackoverflow.com/questions/7610321/difference -between-extern-int-a-extern-int-a-42 – 2012-08-08 09:15:05