makefile编译所有文件,而不是最后编辑的文件 [英] makefile compiles all files instead of the last edited ones

查看:74
本文介绍了makefile编译所有文件,而不是最后编辑的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请注意,SO上也有类似的问题,但是我认为我的情况有所不同,此外,我的Makefile非常简单明了.我是Makefile的新手.

Note that there are similar questions on SO, however I think my situation is different, moreover my Makefile is extremely simple and straight forward. I am new to Makefile.

假设我需要编译一个项目,看起来像这样 --

Suppose I need to compile a project, that looks like this --

.
├── [4.0K]  bin
├── [ 517]  Makefile
├── [4.0K]  obj
└── [4.0K]  src
    ├── [ 117]  func1.cpp
    ├── [  76]  func2.cpp
    ├── [ 137]  global.h
    └── [  97]  main1.cpp

我的Makefile看起来像这样-

and my Makefile looks like this --

CC          := g++
CFLAGS      := -g -Wall -std=c++0x
LDFLAGS     := -lm

NAMES   := func1.cpp func2.cpp main1.cpp
SRC     := $(addprefix src/,$(NAMES))
OBJ     := $(addprefix obj/,$(NAMES:.cpp=.o))
DEPS    := $(OBJ:.o=.d)

.PHONY: clean all debug

all: prog

debug:
    $(info $$SRC: ${SRC})
    $(info $$OBJ: ${OBJ})
    $(info $$DEPS: ${DEPS})

prog: bin/prog

bin/prog: $(OBJ)
    $(CC) $^ -o $@

$(OBJ): $(SRC)
    $(CC) $(CFLAGS) -I/src/global.h -c $(addprefix src/,$(notdir $(@:.o=.cpp))) -o $@

-include $(DEPS)

clean:
    rm -rf bin/*
    rm -rf obj/*

假设我打开了文件 func1.cpp 并进行了一些更改.当我调用 make 时,它会编译所有文件,但是应该只编译一个文件( func1.cpp ).

Suppose I opened a file func1.cpp and made some changes. When I invoke make it compiles all files, but it was supposed to compile only one (func1.cpp).

我该如何解决?

注意:出于其他原因,我需要 prog bin/prog ,而且我也无法做类似 obj的食谱/%.o:src/%.c ,因为我的目标可能与相同对象的子集不同.

Note: I need prog, bin/prog for a different reason, also I can't do recipes like obj/%.o: src/%.c because I might have different target from the subset of the same objects.

推荐答案

编写如下规则:

$(OBJ): $(SRC)
    cmd

在你的情况下是

obj/func1.o obj/func2.o obj/main.o : src/func1.cpp src/func2.cpp src/main1.cpp
    cmd

先决条件不会被压缩.这将为每个目标生成一个规则,具有所有先决条件.那就是:

The prerequisites don't get zipped across. That generates one rule for each target, with all of the prerequisites. That is:

obj/func1.o : src/func1.cpp src/func2.cpp src/main1.cpp
    cmd

obj/func2.o : src/func1.cpp src/func2.cpp src/main1.cpp
    cmd

obj/main.o : src/func1.cpp src/func2.cpp src/main1.cpp
    cmd

由于 src/func1.cpp 是所有目标文件的先决条件,因此它们都将重新编译.

Since src/func1.cpp is a prereq for all of the object files, they all get recompiled.

您要使用的是静态模式规则:

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

请注意, -I 用于包含目录,而不包括文件.

Note that -I is for include directories, not include files.

这篇关于makefile编译所有文件,而不是最后编辑的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆