开发者

undefined reference to variable i

开发者 https://www.devze.com 2023-03-12 19:38 出处:网络
here is my piece of code #include<stdio.h> main () { extern int i; i=20; printf(\"%d\",i); } When I compile it I get error

here is my piece of code

#include<stdio.h>
 main ()
{
        extern int i;
        i=20;
     printf("%d",i);
}

When I compile it I get error

ka2.c: In function ‘main’:
ka2.c:6: warning: format ‘%d’ expects type ‘int’, but 开发者_运维问答argument 2 has type ‘long unsigned int’
/tmp/ccGXrSE5.o: In function `main':
**ka2.c:(.text+0x6): undefined reference to `i'**
collect2: ld returned 1 exit status

I want to know the reason of error in lines which I have bolded.


You have declared i but haven't defined it, that's why the linker is complaining.


You declared i as extern. Removing this keyword fixes the issue, because extern means something which is defined in another module

0

精彩评论

暂无评论...
验证码 换一张
取 消