2016-10-30 48 views
-1

我似乎無法弄清楚爲什麼我的makefile沒有正確執行。我收到以下錯誤:Makefile C Linux錯誤

gcc hr_timer.c -o hr 
gcc hr_timer.o -o hr_timer 
gcc: error: hr_timer.o: No such file or directory 
gcc: fatal error: no input files 
compilation terminated. 
make: *** [hr_timer] Error 4 

這裏是我的makefile:

CC = gcc 
CFLAGS = -pthread 

all: hr_timer q2q3 process_switching thread_switching 

hr_timer.o: hr_timer.c 
    $(CC) hr_timer.c -o hr 

q2q3.o: q2q3.c 
    $(CC) q2q3.c -o qq 

process_switching.o: process_switching.c 
    $(CC) process_switching.c -o pr 

thread_switching.o: thread_switching.c 
    $(CC) thread_switching.c -o th 

,這裏是它的目錄:enter image description here

所有.c文件編譯就好了,而不makefile文件。謝謝!

編輯:

新的Makefile:

CC = gcc 
CFLAGS = -pthread 

all: hr_timer q2q3 process_switching thread_switching 

hr_timer.o: hr_timer.c 
    $(CC) hr_timer.c -o hr hr_timer.o 

q2q3.o: q2q3.c 
    $(CC) q2q3.c -o qq q2q3.o 

process_switching.o: process_switching.c 
    $(CC) process_switching.c -o pr process_switching.o 

thread_switching.o: thread_switching.c 
    $(CC) $(CFLAGS) thread_switching.c -o th thread_switching.o 

錯誤:

gcc hr_timer.c -o hr hr_timer.o 
gcc: error: hr_timer.o: No such file or directory 
make: *** [hr_timer.o] Error 1 

EDIT2(FIX):

CC = gcc 
CFLAGS = -pthread 

all: hr qq pr th 

hr: hr_timer.c 
    $(CC) hr_timer.c -o hr 

qq: q2q3.c 
    $(CC) q2q3.c -o qq 

pr: process_switching.c 
    $(CC) process_switching.c -o pr 

th: thread_switching.c 
    $(CC) $(CFLAGS) thread_switching.c -o th 

回答

1

你現在看到的主要問題是你沒有把你的目標說成喲你是!規則

hr_timer.o: hr_timer.c 
    $(CC) hr_timer.c -o hr 

創建一個名爲hr的目標文件(這就是-o hr所做的)。相反,它應該是:

hr_timer.o: hr_timer.c 
    $(CC) hr_timer.c -o hr_timer.o 

這個文件中的其餘目標也是如此。簡而言之,Makefile具有目標和依賴關係。他們遵循的語法如下:

target: dependency1 dependency2 dependency3 ... 
    command that makes target from the dependencies 

每個規則告訴make,你可以使目標如果所有依賴的存在,然後執行以下命令。這允許make試圖通過首先創建它的依賴關係來創建最終的可執行文件,如果這些依賴關係具有依賴關係,那麼它也會使這些依賴關係成爲可執行文件(等等)。但是,如果在執行規則之後,冒號左側列出的目標未被創建,那麼稍後引用它時會出現問題,該文件將不存在!

另外,值得注意的是,您有一個變量CFLAGS定義爲-pthread。你可能想在每條規則中傳遞給你的編譯器,如下所示:

hr_timer.o: hr_timer.c 
    $(CC) hr_timer.c $(CFLAGS) -o hr_timer.o 
+0

感謝您的回覆。我提出了您提到的更改,但仍然出現錯誤。請檢查編輯。 – user5056973

+0

它不應該是'-o hr hr_timer.o'。它應該只是'-o hr_timer.o',它表示輸出應該放入文件hr_timer.o中。中間不需要小時。仔細閱讀 –

+1

謝謝。它現在有效。 – user5056973