2

我是 Stack Overflow 的新手。我目前很难解决一个简单的问题。

在我的shell/目录中,我有:

CVS/
include/
Makefile
obj
src

我的问题发生在尝试直接构建目标文件时obj,但是当我make使用以下代码运行时:

# Beginning of Makefile
OBJS = obj/shutil.o obj/parser.o obj/sshell.o
HEADER_FILES = include/shell.h include/parser.h
EXECUTABLE = simpleshell
CFLAGS = -Wall
CC = gcc
# End of configuration options

#What needs to be built to make all files and dependencies
all: $(EXECUTABLE)

#Create the main executable
$(EXECUTABLE): $(OBJS)
         $(CC) -o $(EXECUTABLE) $(OBJS)

#Recursively build object files
%.o: %.c
        $(CC) $(CFLAGS) -c -o $@ $<

#Define dependencies for objects based on header files
#We are overly conservative here, parser.o should depend on parser.h only
$(OBJS) : $(HEADER_FILES)

clean:
        -rm -f $(EXECUTABLE) obj/*.o
run: $(EXECUTABLE)
        ./$(EXECUTABLE)

tarball:
         -rm -f $(EXECUTABLE) obj/*.o
         (cd .. ; tar czf Kevin_Fairchild_a3.tar.z shell )

# End of Makefile

我收到此错误:

gcc -o simpleshell obj/shutil.o obj/parser.o obj/sshell.o
gcc: obj/shutil.o: No such file or directory
gcc: obj/parser.o: No such file or directory
gcc: obj/sshell.o: No such file or directory
gcc: no input files
make: *** [simpleshell] Error 1

我错过了什么简单的作品?我将继续研究并了解有关 Makefile 的更多信息

4

2 回答 2

1

问题是模式规则

%.o: %.c
    ...

实际上与您尝试做的不匹配。源文件实际上是src/shutil.c,所以这个规则不适合。所有 Make 看到的是这条规则:

$(OBJS) : $(HEADER_FILES)

没有命令,因此 Make 得出结论,不需要任何操作。然后它继续执行 for 的规则simpleshell,但由于对象不存在而失败。

尝试这个:

obj/%.o: src/%.c
    $(CC) $(CFLAGS) -c -o $@ $<

有更复杂的变化,一旦这么多工作。

于 2013-10-16T20:19:28.720 回答
0

在添加了那个简单的修改之后,我最初在此处发布之前尝试过,即

obj/%.o: src/%.c 

我收到了这个错误,所以最初我认为它是别的东西。

gcc -Wall -c -o obj/shutil.o 
src/shutil.c
src/shutil.c:14:19: error: shell.h: No such file or directory
src/shutil.c: In function ‘signal_c_init’:
src/shutil.c:72: error: ‘waitchildren’ undeclared (first use in this function)
src/shutil.c:72: error: (Each undeclared identifier is reported only once
src/shutil.c:72: error: for each function it appears in.)
src/shutil.c: In function ‘checkbackground’:
src/shutil.c:90: warning: implicit declaration of function ‘striptrailingchar’
src/shutil.c: At top level:
src/shutil.c:101: warning: conflicting types for ‘striptrailingchar’
src/shutil.c:90: note: previous implicit declaration of ‘striptrailingchar’ was here
make: *** [obj/shutil.o] Error 1` 

顺便感谢您的快速回复!

于 2013-10-21T20:30:28.727 回答