MakeFile没有编译和构建.o文件

时间:2015-02-04 05:58:34

标签: c makefile

当我输入" 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就在那里。因此,a2lib.o正在创建,但assign2.o不是。 assign2.c中的C代码中没有错误,所以我假设Makefile中的代码出错了。我不确定为什么。

2 个答案:

答案 0 :(得分:0)

如果设置了VPATH环境变量(在shell中导出)并且包含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或它们的系统范围等效。

答案 1 :(得分:0)

尝试更改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会检查源文件是否有更改。以前它只考虑标题中的更改来触发重建目标文件。