通用生成文件
Generic Makefile
本文关键字:文件 更新时间:2023-10-16
我正在寻找一个通用的makefile,它将构建当前目录和所有子目录中的所有C++文件和所有子目录(例如,源,测试文件,gtest等)
我花了几个小时尝试了几个,最终确定了带有源子目录的make文件的解决方案。
我需要对其进行三项更改:
- Gtest 使用 *.cc 作为其C++文件,我还有其他使用 *.cpp
- 我需要能够定义多个搜索路径。
- 添加编译器标志,如 -W all
我已经设法打破了makefile,如下所示,这样运行make给了我
make: *** 没有规则来制定目标
%.cpp=%.o', needed by
我的程序'。 停。
我怎样才能让它做这三件事?
# 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 fiel 扩展名,我可以说目录树中永远不会有我不希望包含在构建中的源文件。
我仍然在使用 SRC 时遇到问题,因为找不到输入文件。我想我应该更多地研究 find 命令,因为我使用 Linux 已经有一段时间了。
这是一个相当糟糕的makefile:它不会为您构建标头依赖项,因此您最终可能会得到损坏的构建。
我无耻地推荐这个。
Etan 指出了你的问题。 但是您不必执行两次替换,只需:
OBJ := $(addsuffix .o,$(basename $(SRCS)))
并且,在使用shell
函数时,您应该始终使用:=
而不是=
赋值。
相关文章:
- .cpp和.h文件中的模板专用化声明
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 文本文件中的单词链表
- CMake-按正确顺序将项目与C运行时对象文件链接
- 使用新行和不使用新行读取文件
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 挂起和取消挂起一个文件DLL
- 如何确定我已使用非编码文件到达 EOF?
- 命名空间中具有.h和.cpp文件的类
- 如何使用ndk-build.cmd构建Android.so文件
- 从包含m行的文件中提取n行,必要时(惰性地)重复该文件
- 读取文件并输入到矢量中
- 在C++中查找文件
- c++库的公共头文件中应该包含什么
- 用c++从输入文件中读取另一行
- Cppcheck生成xml转储文件
- 读取文件的最后一行并输入到链接列表时出错
- 无法编译 rtmidi 测试 cmidiin.cpp 文件, 非法指令
- 如何将内容数组写入文本文件?
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到