2013-02-01 79 views
0

我有一個自定義頭文件example.h,它有幾個函數的原型。有一個.C文件example.c,我實現了其中的「include」(#include「example.h」),並且具有example.h中原型函數的實現。現在,我有另一個函數test.c調用example.h中原型並在example.c中定義的函數。C makefile錯誤

我的化妝文件如下

test: test.o 
    gcc -o test -g test.o 

test.o: test.c example.c example.h 
    gcc -g -c -Wall test.c 
    gcc -g -c -Wall example.c 

clean: 
    rm -f *.o test 

我獲得以下消息在example.c

在文件中未定義第一次引用 符號

功能1 test.o中定義的函數

function2 test.o

功能3 test.o

function4 test.o

LD:致命的:符號引用錯誤。無輸出寫入測試

collect2:LD返回1退出狀態

*錯誤代碼1

化妝:致命錯誤:命令失敗目標'測試」

任何幫助是最讚賞。

+0

爲什麼你不鏈接示例?你編譯它... – thang

回答

2
%.o: %.c 
    gcc -c -g -o [email protected] $^ 

test: test.o example.o 
    gcc -o -g [email protected] $^ 

%.o: %.c這意味着任何*.o文件應該從它的equivalen從c文件被建造。 gcc -o test example.o test.o

例如test.o應從test.c被建造和example.o應該example.c

+0

OP似乎並沒有用於makefile規則和模式,你應該多解釋一下。爲了澄清你的答案,你應該:1.定義什麼是$(CC),$(CFLAGS)和$(LDFLAGS)(給出一個基本選項的例子),2.解釋什麼是@ @和$^ – Rerito

+0

我改變了他們用簡單的命令'gcc' – MOHAMED

1

首先來建造,你必須生成可執行文件時包括example.o文件。然後,您爲目標test.o編寫的依賴關係不正確。你應該像這樣把它分解:

test: test.o example.o 
    gcc -o test test.o example.o 
test.o: test.c 
    gcc -c -Wall test.c 
example.o: example.c 
    gcc -c -Wall example.c 

然後,考慮使用的變量來存儲你的目標文件的名稱,你想傳遞給連接器/編譯器等標誌...這將讓您的生活更容易。

+0

對'test.o:test.c'和'example.o:example.c'使用相同的規則太多了。你可以在一條規則中減少這個數字。使用'%.o:%。c',而對於build命令,使用'gcc -c -Wall -g -o $ @ $ ^' – MOHAMED

+0

順便說一下,OP在他的gcc命令中使用-g作爲選項。你必須在你的gcc命令中加入它 – MOHAMED

+0

我知道,但是我想給OP一個基本的例子,而不使用特殊的makefile規則(比如'.c.o:')。爲他澄清你的答案,我會upvote :) – Rerito

0

test.o: test.c example.c example.h
gcc -g -c -Wall test.c gcc -g -c -Wall example.c

按你的代碼test.o的目標是調用test.c的example.c example.h文件的目標,我現在無法看到。