我在 temp2.h 中声明了一个变量 iextern i;
其中仅包含上面一行
并制作了另一个文件
温度3.c
#include<stdio.h>
#include<temp2.h>
int main ()
{
extern i;
i=6;
printf("The i is %d",i);
}
当我上面编译为cc -I ./ temp3.c
我收到以下错误
/tmp/ccJcwZyy.o: In function `main':
temp3.c:(.text+0x6): undefined reference to `i'
temp3.c:(.text+0x10): undefined reference to `i'
collect2: ld returned 1 exit status
正如我在上面的帖子中提到的,我已在上面的 temp3.c 中声明了 extern,如 K R 第 33 页所述。
我尝试了使用相同头文件 temp2.h 的 temp3.c 的另一种方法
#include<stdio.h>
#include<temp2.h>
int main ()
{
i=6;
printf("The i is %d",i);
}
并编译它cc -I ./ temp3.c
并得到以下错误
/tmp/ccZZyGsL.o: In function `main':
temp3.c:(.text+0x6): undefined reference to `i'
temp3.c:(.text+0x10): undefined reference to `i'
collect2: ld returned 1 exit status
我也尝试过
#include<stdio.h>
#include<temp2.h>
int main ()
{
extern i=6;
printf("The i is %d",i);
}
编译了这个
cc -I ./ temp3.c
遇到与帖子 1 相同的错误
temp3.c: In function ‘main’:
temp3.c:5: error: ‘i’ has both ‘extern’ and initializer
所以我尝试了至少 3 种不同的方法来使用 extern,但没有一个有效。