[英]getting different value from source code in the main function in c
我試圖了解如何使用這個小代碼鏈接不同的文件
main.c
#include <stdio.h>
#include <stdlib.h>
#include "try.h"
void main()
{
printf("%i",a);
}
在 try.h
#include "stdint.h"
uint64_t a ;
在try.c
#include "try.h"
uint64_t a=1;
結果為 0,在 try.c 中另有提及,我似乎無法理解,這是編譯器還是 linker 錯誤。 我如何讓主要的 function 打印 1
它實際上表明鏈接不成功。 如果您嘗試與 try.c 鏈接,它將中斷。
當您包含 try.h 時,編譯器基本上會復制其內容:
#include "stdint.h"
uint64_t a ;
#include <stdio.h>
#include <stdlib.h>
#include "try.h"
void main()
{
printf("%i",a);
}
請注意,此時與“try.c”沒有任何關系。 如果你運行它,那么a
沒有初始化,它恰好是 0(因為未初始化的文件范圍變量將被分配給 0 值的.bss)。
如果您決定與“try.c”鏈接,則會發生錯誤,因為a
被定義了兩次。 因此有兩點需要修復:
extern uint64_t a;
這意味着您承諾將在某處定義a
,但不在 header 中定義。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.