我正在寻找一个简单的推荐用于 linux 的“最小”c++ makefile,它将使用 g++ 编译和链接单个文件和 h 文件。理想情况下,make 文件中甚至没有物理文件名,只有 .cpp 到 .o 的转换。生成这样一个 makefile 而又不陷入 autoconf 的恐惧的最佳方法是什么?
当前目录包含,例如
t.cpp t.h
我想要一个生成文件来创建它。我尝试了 autoconf,但它假设 .h 是 gcc 而不是 g++。是的,虽然不是初学者,但我正在从几年前重新学习项目操作的最佳方法,因此正在寻找自动化方法来为小型项目创建和维护 makefile。
最佳答案
如果是单个文件,可以输入
make t
它会调用
g++ t.cpp -o t
这甚至不需要目录中的 Makefile,尽管如果你有一个 t.cpp 和一个 t.c 和一个 t.java 等等,它会变得困惑。
也是一个真正的 Makefile:
SOURCES := t.cpp
# Objs are all the sources, with .cpp replaced by .o
OBJS := $(SOURCES:.cpp=.o)
all: t
# Compile the binary 't' by calling the compiler with cflags, lflags, and any libs (if defined) and the list of objects.
t: $(OBJS)
$(CC) $(CFLAGS) -o t $(OBJS) $(LFLAGS) $(LIBS)
# Get a .o from a .cpp by calling compiler with cflags and includes (if defined)
.cpp.o:
$(CC) $(CFLAGS) $(INCLUDES) -c $<
关于c++ - 用于 linux 的最小 c++ make 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/287259/