简体   繁体   English

使一个简单的makefile通用

[英]Make a simple makefile generic

I have a simple makefile to help me compile some examples: 我有一个简单的makefile,可以帮助我编译一些示例:

   ex104: ex104.cpp
        $(CPP) ex104.cpp $(INCS) $(LINKFLAGS) $(COMPILEFLAGS) -o ex104  

   clean:   
        rm -f *.o $(EXECUTABLE)

Now, that works fine. 现在,这很好。 The problem is: I would need to explicitly write a rule for each of the examples, thus copying the following line many, many times: 问题是:我将需要为每个示例显式编写一条规则,从而多次复制以下行:

   ex104: ex104.cpp
        $(CPP) ex104.cpp $(INCS) $(LINKFLAGS) $(COMPILEFLAGS) -o ex104

Is there a way to generic-ize it a bit? 有没有一种通用的方法? So that I would end up with a command line like below that would build ex252 from ex252.cpp built automatically: 这样,我最终得到的命令行如下所示,它将从自动构建的ex252构建出ex252.cpp

make ex252

Fortunately, it's a template-only library, so I always have to build from just one cpp file, and I do not need to keep track of object files. 幸运的是,这是一个仅模板的库,因此我始终只需要从一个cpp文件进行构建,并且不需要跟踪目标文件。

Make has a bunch of default rules and what you want should works if instead of your variables you are using the default one for flags to be given. Make有很多默认规则,如果您使用默认规则代替标记,那么您想要的应该起作用。

If your version of make does not have such default, this should add one (it correspond to GNU make default rule with some intermediary variables inlined and obsolete one removed) : 如果您的make版本没有这样的默认值,则应添加一个(与GNU make default规则相对应,其中插入了一些中间变量,并删除了一个过时的变量):

.cpp:
    $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH) $^ $(LDLIBS) -o $@

CXX is the C++ compiler CXX是C ++编译器

CXXFLAGS is the flags for the C++ compiler CXXFLAGS是C ++编译器的标志

CPPFLAGS is the flags for the preprocessor (they are common between C and C++ in gnu make) CPPFLAGS是预处理器的标志(它们在gnu make中的C和C ++之间是常见的)

LDFLAGS are the flags for linking LDFLAGS是链接的标志

TARGET_ARCH allows to specify a target architecture TARGET_ARCH允许指定目标架构

LDLIBS is the libraries to link LDLIBS是要链接的库

Perhaps something like: 也许像这样:

%: %.cpp
    $(CXX) $< $(INCS) $(LINKFLAGS) $(COMPILEFLAGS) -o $@

For further details about $< and $@ take a look at automatic variables . 有关$<$@更多详细信息,请查看自动变量

If I understand your question correctly, you are looking for something like pattern rules . 如果我正确理解了您的问题,那么您正在寻找诸如模式规则之类的东西。 If that is correct, this makefile should compile every cpp file in the directory (taken from here ): 如果是正确的话,此makefile应该编译目录中的每个cpp文件(从此处获取 ):

%.o : %.cpp
        $(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $< -o $@

Like @AProgrammer said, use built-in rules. 就像@AProgrammer所说的那样,请使用内置规则。 I just want to expand on that. 我只想对此进行扩展。

You can find built-in rules and flags by recursively grepping make's database dump. 您可以通过递归grep make的数据库转储找到内置的规则和标志。 make -pq | grep %: make -pq | grep %: to get the list of rules that can produce executable and make -pq | grep '%: %.cpp' -A5 make -pq | grep %:获取可以生成可执行文件并生成make -pq | grep '%: %.cpp' -A5的规则列表。 make -pq | grep '%: %.cpp' -A5 to see the actual command it executes. make -pq | grep '%: %.cpp' -A5查看它执行的实际命令。

In your case, literally the whole makefile could look like: 就您而言,整个makefile看起来可能像这样:

all: ex104

As long as ex104 is required by the default goal and ex104.cpp exists, %: %.cpp built-in rule will be selected. 只要默认目标要求ex104并且ex104.cpp存在,将选择%: %.cpp内置规则。 You can build on that. 您可以以此为基础。 Add more dependencies and flags. 添加更多依赖项和标志。

CXXFLAGS += -Wall -Wextra -Wpedantic -std=c++14
all: ex104
ex104: common_utils.cpp

PS: In make CPP refers to C pre-processor, not C++. PS:在make CPP中指的是C预处理程序,而不是C ++。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM