簡體   English   中英

MakeFile無法編譯和生成.o文件

[英]MakeFile not compiling and building .o file

當我在終端中鍵入“ make”進行編譯時,出現錯誤消息:

gcc  -c -Wall -std=c99 a2lib.c -o a2lib.o -lm
gcc  -g -std=c99 assign2.o a2lib.o -o assign2
gcc: error: assign2.o: No such file or directory
make: *** [assign2] Error 1

我寫了一個a2lib.ha2lib.cassign2.c文件。 a2lib.cassign2.c都具有#include "a2lib.h"#include <math.h>#include <stdio.h>

在我寫的Makefile ,我有:

all: assign2

CC = gcc #Declaring new variable CC to replace gcc
#Compiling process

# Link
assign2: assign2.o a2lib.o
    $(CC) -g -std=c99 assign2.o a2lib.o -o assign2

# Compile
%.o: %.c
    $(CC) -c -Wall -std=c99 $< -o $@ -lm


assign2.o: a2lib.h

#Defining a dependency for assign2.o

a2lib.o: a2lib.h

#Defining a dependency for a2lib.o

.PHONY: all clean

#Declaring the PHONY targets

clean:
    rm assign2 assign2.o a2lib.o

我只是不確定為什么不創建assign2.o 當我輸入ls來查看目錄, assign2.o是不存在的,但a2lib.o是存在的。 因此,正在創建assign2.oa2lib.o創建。 同樣, assign2.c中的C代碼也沒有錯誤,因此我假設我的Makefile的代碼有問題。 我不知道為什么。

如果設置了VPATH環境變量(在外殼中導出)並且包含一個存在assign2.o的目錄,則可以重現您的問題。

有關VPATH的說明,請參見https://www.gnu.org/software/make/manual/html_node/General-Search.html

更新:我的第一個答案缺乏解決方案。 在運行make之前,在shell中鍵入以下命令:

unset VPATH

如果要查看當前為VPATH分配了什么值,請鍵入

echo $VPATH

VPATH可以在某些shell初始化腳本(例如~/.bashrc~/.profile或它們在系統范圍內的等效文件)中全局定義。

嘗試更改makefile的以下幾行:

assign2.o: a2lib.h

#Defining a dependency for assign2.o

a2lib.o: a2lib.h

#Defining a dependency for a2lib.o

assign2.o: a2lib.h  assign2.c

#Defining a dependency for assign2.o.
#Watch for changes in the source file too

a2lib.o: a2lib.h a2lib.c

#Defining a dependency for a2lib.o
#Watch for changes in the source file too

有了這個變化, make檢查是否有在的資源文件的變化。 以前,它僅考慮頭文件中的更改以觸發目標文件的重建。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM