2013-05-13 41 views
0

我需要修改Makefile我只需要将与“record.c”关联的目标文件存储到bin文件夹中。这是执行Make之前我的目录结构的样子。使用Makefile将对象文件存储在两个不同的目录中? [C]

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 

...这里是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 [email protected] 

lib/libshell.so: obj/shutil.o 
    $(CC) $(LIBFLAGS) -shared $^ -o [email protected] 

lib/libhistory.so: obj/history.o 
    $(CC) $(LIBFLAGS) -shared $^ -o [email protected] 

lib/libhash_table.so: obj/hash_table.o 
    $(CC) $(LIBFLAGS) -shared $^ -o [email protected] 

lib/libvariables.so: obj/variables.o 
    $(CC) $(LIBFLAGS) -shared $^ -o [email protected] 

lib/librecord.so: bin/record.o //<---- 
    $(CC) $(LIBFLAGS) -shared $^ -o [email protected] 

#Recursively build object files 
obj/%.o: src/%.c //<---- I feel like this is causing the problem. 
    $(CC) $(CFLAGS) -I./include/ -c $< -o [email protected] 

#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 

随着我做了什么(最有可能完全关闭)它不会编译record.c,说斌/ record.o做不存在。我对Makefiles不是很有经验,所以我想知道我是否可以得到一些帮助。谢谢!

+0

考虑使用'cmake'或'automake'代替普通的'Makefile'。如果你有一些可用的资源,在处理不平凡的项目构建时,它们可以为你节省大量的工作。 – 2013-05-13 22:12:44

+0

好的,我会研究那些用于将来的项目,但是可以在此Makefile中进行更改吗? – 2013-05-13 22:16:29

+0

所以你想'record.o'进入'bin /'。其他目标文件应该放在哪里? – Beta 2013-05-13 22:54:12

回答

1

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

编辑: 如果不起作用,可能添加以下规则将做的工作:

bin/%.o: src/%.c

$(CC) $(CFLAGS) -I./include/ -c $< -o [email protected]

+0

我尝试使用'.c.o'和它的变体,但我不断收到此错误:'gcc:obj/shutil.o:没有这样的文件或目录'。我认为我没有正确理解你,你能否详细说明一下?谢谢。 – 2013-05-13 22:47:33

+0

哈哈,是的额外的规则工作。我不知道这很简单。非常感谢。 =) – 2013-05-13 23:05:54

相关问题