c++ - 用于 linux 的最小 c++ make 文件

标签 c++ makefile compilation

我正在寻找一个简单的推荐用于 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/

相关文章:

c++ - 在 VS2012 静态库、控制台应用程序和 clr 程序上使用 cryptopp 出现链接错误

linux - Makefile 自动依赖生成

tomcat - Hudson "stop"/取消构建留下进程

haskell - 如何为 Haskell 语言程序制作 Makefile?

multithreading - 有多线程跟踪JIT编译器的示例吗?

gcc - gfortran : error: -fuse-linker-plugin is not supported in the compilation

c++ - 为什么时间测量有时会返回重复值(15.625 毫秒的倍数)?

c++ - QtCreator - 将文件夹导入资源?

c++ - 如何以线程安全的方式从多个文本文件中删除某些字符?

编译脚本