【问题标题】:Using a Makefile to store object files in two different directories? [C]使用 Makefile 将目标文件存储在两个不同的目录中? [C]
【发布时间】:2013-05-13 22:07:42
【问题描述】:

我需要修改 Makefile 我必须只将与“record.c”关联的目标文件存储到 bin 文件夹中。这是我在执行 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

...这里是 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

根据我所做的(很可能完全关闭),它不会编译 record.c 并说 bin/record.o 不存在。我对 Makefile 并没有真正的经验,所以我想知道是否可以得到一些帮助。谢谢!

【问题讨论】:

  • 考虑使用cmakeautomake 而不是普通的Makefile。如果您有一些空闲资源,它们可以在处理非平凡的项目构建时为您节省大量精力。
  • 好的,我会为未来的项目研究这些,但是否可以在这个 Makefile 中进行更改?
  • 所以你想让record.o 进入bin/。其他目标文件应该放在哪里?
  • 正确的record.o进入bin/,但其余的应该继续编译成obj/

标签: c unix makefile


【解决方案1】:

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

编辑: 如果这不起作用,也许添加以下规则就可以了:

bin/%.o: src/%.c

$(CC) $(CFLAGS) -I./include/ -c $&lt; -o $@

【讨论】:

  • 我尝试使用.c.o 及其变体,但我不断收到此错误:gcc: obj/shutil.o: No such file or directory。我想我没有正确理解你,你能详细说明一下吗?谢谢。
  • 哈哈,是的,附加规则有效。我不知道它是如此简单。非常感谢。 =)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-29
  • 1970-01-01
相关资源
最近更新 更多