這真是一個新手問題。我正在學習C,並且我不知道如何將不同的文件鏈接到一起 。gcc連接器如何在包含文件中工作
我有一個像
/* file2.h */
int increment(int x);
頁眉和C文件
/* file2.c */
#include "file2.h"
int increment(int x)
{
return x+1;
}
現在我想包括在file1.c
頭,以使用功能increment
。 從我明白我必須做一些事情,如:
/* file1.c*/
#include "file2.h"
int main()
{
int y = increment(1);
return 0;
}
但是,當我嘗試編譯整個事情,使用
我得到這樣
/tmp/ccDdiWyO.o: In function `main':
file1.c:(.text+0xe): undefined reference to `increment'
collect2: ld returned 1 exit status
的錯誤信息
但是,如果我還包括file2.c
/* file1.c*/
#include "file2.h"
#include "file2.c" /* <--- here it is! */
int main()
{
int y = increment(1);
return 0;
}
一切按預期工作。
但是,如果我只理解頭文件(只包含聲明)必須包括在內。那麼如何告知gcc
中聲明的函數increment
的定義是在file2.c
?
我覺得你最後一句話被切斷了。 – Mysticial
對不起,我在上傳問題時遇到問題:) –