通用Makefile

毛格说恢复莫妮卡

我正在寻找一个通用的makefile,它将在当前目录和所有子目录(例如源代码,测试文件,gtest等)中构建所有C ++文件。

我花了几个小时尝试了几个,最终确定了源子目录的make文件解决方案

我需要对其进行三处更改:

  1. Gtest的C ++文件使用* .cc,我还有其他人使用* .cpp
  2. 我需要能够定义多个搜索路径。
  3. 添加编译器标志,例如-W all

我设法破坏了makefile,如下所示,以便运行make可以给我

make:***没有将目标%.cpp=%.o', needed by设为myProgram的规则停止。

我该如何做这三件事?

# 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 $@

[更新]感谢您到目前为止的帮助。为了解决一些意见,我无法控制混合的* .cc和* .cpp扩展名,我可以说目录树中永远不会有我不希望包含的源文件。在构建中。

我仍无法使用SRC,因为找不到输入文件。我猜我应该更多地考虑find命令,因为自从我使用Linux已经有一段时间了。

疯狂的科学家

伊坦指出了您的问题。但是您不必执行两次替换,只需:

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

并且,使用该函数,应始终使用:=而不是=进行分配shell

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章