简体   繁体   English

Makefile:递归编译C++文件

[英]Makefile: Compile C++ Files recursively

I am new to makefiles and tried reading resources on the internet to solve my problem, yet I am unable to find a solution.我是 makefile 的新手,并尝试阅读互联网上的资源来解决我的问题,但我无法找到解决方案。

Basically I am working on a project which contains C++ and Cuda files.基本上我正在研究一个包含 C++ 和 Cuda 文件的项目。 Since I like to keep my things structured, I usually use a nested folder structure like this:由于我喜欢保持我的东西结构化,我通常使用这样的嵌套文件夹结构:

 |- bin
 |- build
 |  |- cc
 |  |- cu
 |- src
 |- makefile
 |- main.cpp

My current Makefile looks like this:我当前的 Makefile 看起来像这样:


CC       = g++
NVCC     = nvcc
CC_SRC   = $(wildcard *.cpp */*.cpp */*/*.cpp */*/*/*.cpp)
CU_SRC   = $(wildcard *.cu */*.cu */*/*.cu */*/*/*.cu)

CC_LIBS  = -pthread -Wl,--whole-archive -lpthread -Wl,--no-whole-archive

ROOT     = ./
FOLDER   = $(ROOT)bin/
OBJECTS  = $(ROOT)build/
CC_OBJ   = $(OBJECTS)cc/
CU_OBJ   = $(OBJECTS)cu/
NAME     = Test
EXE      = $(ROOT)$(FOLDER)$(NAME)

NVCC_FLAGS =
CC_FLAGS =

## Compile ##
run:build

build: $(EXE)

$(CC_SRC):
    @echo compiling
    $(CC) $(CC_FLAGS) -c $< -o $a

$(EXE) : $(CC_SRC)
    @echo test 123

Initially I wanted to compile all C++ files to the build/cc files.最初我想将所有C++文件编译为build/cc文件。 Similarly my .cu files should be compiled into the build/cu folder.同样,我的.cu文件应该编译到build/cu文件夹中。

Initially I made a wildcard which catches all the c++ files:最初,我制作了一个通配符,它可以捕获所有c++文件:

CC_SRC   = $(wildcard *.cpp */*.cpp */*/*.cpp */*/*/*.cpp)
CU_SRC   = $(wildcard *.cu */*.cu */*/*.cu */*/*/*.cu)

But for some reason the according rule is not being executed.但是由于某种原因,没有执行相应的规则。 I am happy if someone could help me here.如果有人可以在这里帮助我,我很高兴。

Greetings Finn问候芬恩

This rule doesn't make sense:这个规则没有意义:

$(CC_SRC):
        @echo compiling
        $(CC) $(CC_FLAGS) -c $< -o $a

(I assume you mean $@ here not $a ). (我假设你的意思是$@这里不是$a )。 This rule says that the way to create each of the source files is by compiling them.这条规则说创建每个源文件的方法是编译它们。 But, make doesn't need to build the source files: they already exist.但是,make 不需要构建源文件:它们已经存在。 So it never invokes your rule.所以它永远不会调用你的规则。

You need your target to be the file you want to create , not the file that already exists.您需要您的目标是您要创建的文件,而不是已经存在的文件。 The file you want to create is the object file, not the source file.您要创建的文件是 object 文件,而不是源文件。 Then the prerequisite is the file that already exists.那么先决条件是已经存在的文件。 So for a compilation you want a pattern rule, like this:因此,对于编译,您需要一个模式规则,如下所示:

$(CC_OBJ)%.o : %.cpp
        @echo compiling
        @mkdir -p $(@D)
        $(CC) $(CC_FLAGS) -c $< -o $@

Then you want your target to depend on the object files, not the source files, like this:然后,您希望您的目标依赖于 object 文件,而不是源文件,如下所示:

$(EXE) : $(CC_SRC:%.cpp=$(CC_OBJ)%.o)
        @echo test 123

I recommend migrating your project to CMake ( https://cmake.org/cmake/help/latest/ ), in particular if your project is not too big yet.我建议将您的项目迁移到 CMake ( https://cmake.org/cmake/help/latest/ ),特别是如果您的项目还不太大。

Makefiles can be really a pain; Makefile 真的很痛苦。 CMake can ease matters considerably. CMake 可以大大缓解问题。

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

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