0

I need to modify the Makefile I have to store only the object file associated with "record.c" into the bin folder. Here is what my directory structure looks like before executing Make.

bin/
include/
    -hash_table.h
    -history.h
    -parser.h
    -record.h
    -shell.h
    -variables.h
lib/
obj/
src/
    -hash_table.c
    -history.c
    -parser.c
    -record.c
    -shutil.c
    -sshell.c
    -variables.c

...and here is the Makefile:

# Beginning of Makefile
SRC = src/shutil.c src/parser.c src/sshell.c src/history.c src/hash_table.c src/variables.c src/record.c
OBJS = obj/shutil.o obj/parser.o obj/sshell.o obj/history.o obj/hash_table.o obj/variables.o bin/record.o //<----
HEADER_FILES = include/shell.h include/parser.h include/history.h include/hash_table.h include/variables.h include/record.h
EXECUTABLE = sshell
LIBS = lib/libshell.so lib/libparser.so lib/libhistory.so lib/libhash_table.so lib/libvariables.so lib/librecord.so
LIBCFLAGS = $(CFLAGS) -D_REENTRANT -fPIC
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) $(LIBS)
    $(CC) -o $(EXECUTABLE) obj/sshell.o -Llib -lparser -lshell -lhistory -lhash_table -lvariables -lrecord

#Create the library files
lib/libparser.so: obj/parser.o
    $(CC) $(LIBFLAGS) -shared $^ -o $@

lib/libshell.so: obj/shutil.o
    $(CC) $(LIBFLAGS) -shared $^ -o $@

lib/libhistory.so: obj/history.o
    $(CC) $(LIBFLAGS) -shared $^ -o $@

lib/libhash_table.so: obj/hash_table.o
    $(CC) $(LIBFLAGS) -shared $^ -o $@

lib/libvariables.so: obj/variables.o
    $(CC) $(LIBFLAGS) -shared $^ -o $@

lib/librecord.so: bin/record.o //<----
    $(CC) $(LIBFLAGS) -shared $^ -o $@

#Recursively build object files
obj/%.o: src/%.c //<---- I feel like this is causing the problem.
    $(CC) $(CFLAGS) -I./include/ -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 lib/*.so lib/*.a bin/*.o
    -rm -f .sshell_history.txt

run: $(EXECUTABLE)
    (export LD_LIBRARY_PATH=lib; ./$(EXECUTABLE))

# End of Makefile

With what I have done (most likely completely off) it doesn't compile record.c and says bin/record.o does not exist. I am not really experienced with Makefiles so I am wondering if I can have some help. Thanks!

4

1 回答 1

1

尝试使用规则.c.o而不是obj/%.o: src/%.c

编辑:如果这不起作用,也许添加以下规则可以完成这项工作:

bin/%.o: src/%.c

$(CC) $(CFLAGS) -I./include/ -c $< -o $@

于 2013-05-13T22:30:42.853 回答