如何使用makefile独立编译和更新一个文件夹中的所有文件 [英] How to use makefile to compile and update all files in one folder independently

查看:155
本文介绍了如何使用makefile独立编译和更新一个文件夹中的所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多个.cpp文件的文件夹,例如A.cpp,B.cpp和C.cpp它们彼此独立.我可以将A.cpp编译为A.o,然后编译为二进制文件A.B.cpp和C.cpp也是如此.

I have a folder containing several .cpp files, e.g. A.cpp, B.cpp and C.cpp. They are all independent from each other. I could compile A.cpp to A.o and then to binary file A. So it is with B.cpp and C.cpp.

我想编写一个可以一次编译所有这些文件的makefile.将来,如果将某些新文件(例如D.cpp)放入此文件夹,makefile仍可以自动处理它.

I want to write a makefile that could compile all these files at once. And in the future, if there are some new files such as D.cpp is put into this folder, makefile can still handle it automatically.

我当前的文件是

CC=g++
CFLAGS=-c -Wall
LDFLAGS=
SOURCES=$(wildcard *.cpp)
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=$(SOURCES:.cpp=)

all: $(SOURCES) $(OBJECTS) $(EXECUTABLE)

$(OBJECTS): $(SOURCES)  
    $(CC) $(CFLAGS) $(@:.o=.cpp) -o $@

$(EXECUTABLE): $(SOURCES)
    $(CC) $(LDFLAGS) $(@:=.o) -o $@

问题在于,一旦一个.cpp文件被更改或添加到该文件夹​​中,如果我制作了,所有其他文件将被更新并重新编译.

The problem is that once one .cpp file is changed or added into the folder, all other files will be updated and re-compiled if I do make.

请让我知道如何解决此问题.

Please let me know how to solve this issue.

谢谢.

推荐答案

您的makefile列出了每个源文件,作为每个可执行文件和每个目标文件的先决条件.

Your makefile lists every source file as a prerequisite of every executable and every object file.

它也不喜欢将对象文件作为可执行文件的先决条件(这意味着make不能自动为您构建它们).

It also doesn't like the object files as prerequisites of the executables (which means make can't automatically build them for you).

这些就是问题.

所有人都说您根本不需要此Makefile中的规则.

That all being said you don't need the rules in this makefile at all.

一个公正的文件

CC=g++
CXXFLAGS=-c -Wall

将允许您运行make A并从A.cpp构建A(尽管由于不需要,它不会构建A.o来执行此操作).如果您不需要设置CCCXXFLAGS(或者不介意在命令行中设置它们),甚至甚至不需要makefile即可从A.cpp构建A.

will let you run make A and build A from A.cpp (though it won't build A.o to do that since it doesn't need to). You don't even need a makefile at all to build A from A.cpp if you don't need to set CC or CXXFLAGS (or don't mind setting them on the command line.

$ mkdir test
$ cd test
$ ls
$ touch A.cpp
$ make CC=g++ CXXFLAGS='-c -Wall' A
g++ -c -Wall    A.cpp   -o A

但是,如果您要求它制作A.o,它将成为

But if you ask it to make A.o it will.

$ make CC=g++ CXXFLAGS='-c -Wall' A.o
g++ -c -Wall   -c -o A.o A.cpp

这时,make将尝试从A.o构建A.

At which point make will try to build A from A.o.

$ make CC=g++ CXXFLAGS='-c -Wall' A
g++   A.o   -o A

要获得对make构建所有可执行文件的支持,您需要这样的行:

To get back support for make building all the executables you need a line like:

all: $(subst .cpp,,$(wildcard *.cpp))

如果您要做仍想手动指定所有内容(并强制构建.o),则需要更多类似内容. (使用 10.5定义和重新定义模式规则 4.12静态模式规则.)

If you do want to manually specify everything nonetheless (and force .o building) you want something more like this. (Which uses 10.5 Defining and Redefining Pattern Rules and 4.12 Static Pattern Rules.)

CC=g++
CFLAGS=-c -Wall
LDFLAGS=
SOURCES=$(wildcard *.cpp)
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=$(SOURCES:.cpp=)

all: $(EXECUTABLE)

%.o: %.cpp
    $(CC) $(CFLAGS) $^ -o $@

$(EXECUTABLE): % : %.o
    $(CC) $(LDFLAGS) $^ -o $@

然后让您编写make来构建它们,并编写make A来构建特定树.

Which then lets you write make to build them all and make A to build specific ones.

这篇关于如何使用makefile独立编译和更新一个文件夹中的所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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