简体   繁体   English

通用Makefile

[英]Generic Makefile

I am looking for a generic makefile, which will will build all C++ files in the current directory and all sub-directories (eg, source, test files, gtest, etc) 我正在寻找一个通用的makefile,它将在当前目录和所有子目录(例如源代码,测试文件,gtest等)中构建所有C ++文件。

I have spent a few hours trying out several and finally settled on the solution from make file with source subdirectories . 我花了几个小时尝试了几次,最终确定了源子目录的make文件解决方案。

I need to make three changes to it: 我需要对其进行三处更改:

  1. Gtest uses *.cc for its C++ files and I have others which use *.cpp Gtest的C ++文件使用* .cc,我还有其他人使用* .cpp
  2. I need to be able to define multiple search paths. 我需要能够定义多个搜索路径。
  3. Add compiler flags, like -W all 添加编译器标志,例如-W all

I have managed to break the makefile, shown below, such that running make gives me 我设法破坏了makefile,如下所示,这样运行make就可以

make: *** No rule to make target %.cpp=%.o', needed by myProgram'. make:***没有规则可以使myProgram'成为目标%.cpp=%.o', needed by '。 Stop. 停止。

How can I make it do those three things? 我该如何做这三件事?

# Recursively get all *.cpp in this directory and any sub-directories
SRC = $(shell find . -name *.cc) $(shell find . -name *.cpp)

INCLUDE_PATHS = -I ../../../ -I gtest -I dummies

#This tells Make that somewhere below, you are going to convert all your source into 
#objects
# OBJ =  src/main.o src/folder1/func1.o src/folder1/func2.o src/folder2/func3.o

OBJ = $(SRC:%.cc=%.o %.cpp=%.o)

#Tells make your binary is called artifact_name_here and it should be in bin/
BIN = myProgram

# all is the target (you would run make all from the command line). 'all' is dependent
# on $(BIN)
all: $(BIN)

#$(BIN) is dependent on objects
$(BIN): $(OBJ)
    g++ 

#each object file is dependent on its source file, and whenever make needs to create
# an object file, to follow this rule:
%.o: %.cc
    g++ -c $(INCLUDE_PATHS) $< -o $@

[Update] Thanks for the help so far. [更新]谢谢您到目前为止的帮助。 To address a few of the comments, I have no control over the mixed *.cc and *.cpp fiel extensions, and I can say that there won't ever be a source file in the directory tree which I do not wish to include in the build. 为了解决一些意见,我无法控制混合的* .cc和* .cpp扩展名,我可以说目录树中永远不会有我不希望包含的源文件。在构建中。

I am still having trouble with the SRC, as no input files are found. 我仍无法使用SRC,因为找不到输入文件。 I guess that I should look more into the find command as it has been a while since I used Linux. 我想我应该更多地考虑find命令,因为自从我使用Linux已经有一段时间了。

Etan points out your problem. 伊坦指出了您的问题。 But you don't have to perform two replacements, just: 但是您不必执行两次替换,只需:

OBJ := $(addsuffix .o,$(basename $(SRCS)))

And, you should always be using := , not = , for assignment when using the shell function. 并且,在使用shell函数时,应该始终使用:=而不是=进行分配。

That is a rather poor makefile: it does not build header dependencies for you, so you are likely to end up with corrupted builds. 那是一个相当糟糕的makefile:它不会为您构建头文件依赖项,因此您最终可能会损坏构建文件。

I shamelessly recommend this one . 我毫不客气地推荐这个

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

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