开始在特定目录中创建目标文件 [英] Getting make to create object files in a specific directory
问题描述
GNU Make 3.82
gcc 4.7.2
c89
我有以下make文件:
I have the following make file:
INC_PATH=-I/home/dev_tools/apr/include/apr-1
LIB_PATH=-L/home/dev_tools/apr/lib
LIBS=-lapr-1 -laprutil-1
RUNTIME_PATH=-Wl,-rpath,/home/dev_tools/apr/lib
CC=gcc
CFLAGS=-Wall -Wextra -g -m32 -O2 -D_DEBUG -D_THREAD_SAFE -D_REENTRANT -D_LARGEFILE64_SOURCE $(INC_PATH)
SOURCES=$(wildcard src/*.c)
OBJECTS=$(patsubst %.c, %.o, $(SOURCES))
EXECUTABLE=bin/to
all: build $(EXECUTABLE)
$(EXECUTABLE): $(OBJECTS)
$(CC) $(CFLAGS) -o $@ $(RUNTIME_PATH) $(OBJECTS) $(LIB_PATH) $(LIBS)
$(OBJECTS): $(SOURCES)
$(CC) $(CFLAGS) -c $(SOURCES) $(LIB_PATH) $(LIBS)
build:
@mkdir -p bin
clean:
rm -rf $(EXECUTABLE) $(OBJECTS) bin
find . -name "*~" -exec rm {} \;
find . -name "*.o" -exec rm {} \;
我的目录结构是这样的project/src project/bin
.我的Makefile位于project (root)
文件夹中,而我所有的* .h和* .c都位于src目录中.目前,我只有一个名为timeout.c的源文件.
My directory structure is like this project/src project/bin
. My Makefile is in the project (root)
folder, and all my *.h and *.c are in the src directory. Currently I have only one source file called timeout.c
我收到此错误:
gcc: error: src/timeout.o: No such file or directory
我已使用它来获取所有源文件:
I have used this to get all the source files:
SOURCES=$(wildcard src/*.c)
目标文件:
OBJECTS=$(patsubst %.c, %.o, $(SOURCES))
但是,make似乎在Makefile所在的项目根文件夹中创建了目标文件.难道不应该把它放在src目录中吗?
However, the make seems to create the object file in the project root folder where the Makefile is. Should it not put it in the src directory?
推荐答案
此规则有两个问题(三个):
You have two problems in this rule (well, three):
$(OBJECTS): $(SOURCES)
$(CC) $(CFLAGS) -c $(SOURCES) $(LIB_PATH) $(LIBS)
您还没有注意到,但是规则使每个对象依赖于 all 源,并尝试以这种方式构建.只要您只有一个来源,这不是问题.使用静态模式规则和
You haven't noticed yet, but the rule makes each object dependent on all sources, and tries to build that way. Not a problem as long as you have only one source. Easy to fix with a static pattern rule and an automatic variable:
$(OBJECTS): src/%.o : src/%.c
$(CC) $(CFLAGS) -c $< $(LIB_PATH) $(LIBS)
此外,命令("$(CC)...")没有指定输出文件名,因此gcc将从源文件名中推断出该文件名;如果给它src/timeout.c
,它将产生timeout.o
(在工作目录project/
中).因此,您应该指定输出文件的所需路径.易于使用另一个自动变量:
Also, the command ("$(CC)...") doesn't specify an output file name, so gcc will infer it from the source file name; if you give it src/timeout.c
, it will produce timeout.o
(in the working directory, project/
). So you should specify the desired path to the output file. Easy to do with another automatic variable:
$(OBJECTS): src/%.o : src/%.c
$(CC) $(CFLAGS) -c $< $(LIB_PATH) $(LIBS) -o $@
这篇关于开始在特定目录中创建目标文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!