简体   繁体   English

Makefile 获取多个子目录中的源文件

[英]Makefile to get source files in multiple subdirectories

I am creating a custom Makefile to to build a C++ Linux application.我正在创建一个自定义 Makefile 来构建 C++ Linux 应用程序。 I have my cpp source files in a folder called src on the same level as the Makefile.我的 cpp 源文件位于与 Makefile 相同级别的名为src的文件夹中。 So far I have been able to build my object files with the following :到目前为止,我已经能够使用以下内容构建我的目标文件:

$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp | $(OBJ_DIR)
    $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@ 

Now my project is starting to get a bit more complicated and I want subdirectories within src , such as src/common , and also, not every file is a cpp file now but also a c file.现在我的项目开始变得有点复杂,我想要src 中的子目录,例如src/common ,而且,现在不是每个文件都是cpp文件,但也是c文件。

I guess having a separate Makefile for each subdirectory is the best way but I am trying to keep this simple for now with just one Makefile.我想为每个子目录创建一个单独的 Makefile 是最好的方法,但我现在试图用一个 Makefile 来保持这个简单。

I tried the following but doesn't work我尝试了以下但不起作用

$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp $(SRC_DIR)/common/%.cpp $(SRC_DIR)/common/%.c | $(OBJ_DIR)
    $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@

I could have the Makefile do a shell find to find all cpp and c files but also trying to avoid this.我可以让 Makefile 做一个shell find来找到所有的 cpp 和 c 文件,但也试图避免这种情况。

I'd appreciate any recommendations.我很感激任何建议。

This:这个:

$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp $(SRC_DIR)/common/%.cpp $(SRC_DIR)/common/%.c | $(OBJ_DIR)

cannot work;不能工作; what it says that if make wants to build some file $(OBJ_DIR)/XXX.o and there is no explicit rule, then if and only if ALL the prerequisites $(SRC_DIR)/XXX.cpp , $(SRC_DIR)/common/XXX.cpp , and $(SRC_DIR)/common/XXX.c exist or can be created by make, then the rule will match.它说的是如果 make 想要构建一些文件$(OBJ_DIR)/XXX.o并且没有明确的规则,那么当且仅当所有先决条件$(SRC_DIR)/XXX.cpp , $(SRC_DIR)/common/XXX.cpp$(SRC_DIR)/common/XXX.c存在或可以由 make 创建,则规则将匹配。

If the same target could be built from multiple different prerequisites you must create multiple rules, one per prerequisite.如果可以从多个不同的先决条件构建同一个目标,则必须创建多个规则,每个先决条件一个。

Also, it doesn't make sense to compile both C files ( .c ) and C++ files ( .cpp ) using the same recipe.此外,使用相同的配方编译 C 文件 ( .c ) 和 C++ 文件 ( .cpp ) 也没有意义。 C++ compilers use the variables CXX and CXXFLAGS and C compilers use the variables CC and CFLAGS . C++ 编译器使用变量CXXCXXFLAGS ,C 编译器使用变量CCCFLAGS

As for avoiding find , you can't avoid informing make what files you want to be compiled, in some manner.至于避免find ,您无法避免以某种方式通知 make 您想要编译哪些文件。 You either have to list them in your makefile explicitly, or else use some method to generate them dynamically.您要么必须在 makefile 中明确列出它们,要么使用某种方法动态生成它们。 There's no way around this.没有办法解决这个问题。 You don't have to use find if you don't want to;如果您不想,则不必使用find for example if you know that all the files will be either one or two directories down you could use wildcard , like:例如,如果您知道所有文件都在一个或两个目录下,您可以使用wildcard ,例如:

SRCS := $(wildcard $(SRC_DIR)/*.cpp $(SRC_DIR)/*.c $(SRC_DIR)/*/*.cpp $(SRC_DIR)/*/*.c)

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

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