Hör*_*nHH 22 c++ makefile compilation
我希望找到一个简单的推荐的"minimal"c ++ makefile for linux,它将使用g ++编译和链接单个文件和h文件.理想情况下,make文件甚至不会包含物理文件名,只有.cpp到.o转换.生成这样一个makefile的最佳方法是什么,而不会陷入autoconf的恐怖?
例如,当前目录包含
t.cpp th
我想要一个makefile来创建它.我试过autoconf,但它假设.h是gcc而不是g ++.是的,虽然不是初学者,但我从几年前开始重新学习项目操作的最佳方法,因此我正在寻找自动化方法来为小项目创建和维护makefile.
haz*_*zen 30
如果是单个文件,则可以键入
make t
Run Code Online (Sandbox Code Playgroud)
它会调用
g++ t.cpp -o t
Run Code Online (Sandbox Code Playgroud)
这甚至不需要目录中的Makefile,但是如果你有t.cpp和tc以及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 $<
Run Code Online (Sandbox Code Playgroud)
flo*_*rin 18
这是我的代码片段目录中的通用makefile:
SOURCES=$(wildcard *.cpp)
OBJECTS=$(SOURCES:.cpp=.o)
DEPS=$(SOURCES:.cpp=.d)
BINS=$(SOURCES:.cpp=)
CFLAGS+=-MMD
CXXFLAGS+=-MMD
all: $(BINS)
.PHONY: clean
clean:
$(RM) $(OBJECTS) $(DEPS) $(BINS)
-include $(DEPS)
Run Code Online (Sandbox Code Playgroud)
只要您有一个.cpp源生成一个二进制文件,您就不需要任何其他内容了.我只在GNU make中使用它,依赖项生成使用gcc语法(也由icc支持).如果您使用的是SUN编译器,则需要将"-MMD"更改为"-xMMD".此外,确保clean:在粘贴此代码时,后面行开头的选项卡不会更改为空格,或者make会丢失分隔符错误.
你看过SCons吗?
只需使用以下命令创建SConstruct文件:
Program("t.cpp")
Run Code Online (Sandbox Code Playgroud)
然后输入:
scons
Run Code Online (Sandbox Code Playgroud)
完成!
假设没有预先配置的系统范围make设置:
CXX = g++
CPPFLAGS = # put pre-processor settings (-I, -D, etc) here
CXXFLAGS = -Wall # put compiler settings here
LDFLAGS = # put linker settings here
test: test.o
$(CXX) -o $@ $(CXXFLAGS) $(LDFLAGS) test.o
.cpp.o:
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $<
test.cpp: test.h
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
32696 次 |
| 最近记录: |