【发布时间】:2014-05-06 09:03:27
【问题描述】:
假设我有以下这些 c++ 文件,我应该如何为它们编写基本的 makefile(使用 g++)?
a.cpp a.h, b.cpp b.h, c.cpp c.h, main.h
当b.h包含a.h时,c.h包含b.h,main.h包含c.h?
非常感谢
【问题讨论】:
标签: makefile
假设我有以下这些 c++ 文件,我应该如何为它们编写基本的 makefile(使用 g++)?
a.cpp a.h, b.cpp b.h, c.cpp c.h, main.h
当b.h包含a.h时,c.h包含b.h,main.h包含c.h?
非常感谢
【问题讨论】:
标签: makefile
这样你就可以写了。
EXE := exe
CC := g++
CPP_FILES := a.cpp b.cpp c.cpp
HPP_FILES := a.h b.h c.h main.h
$(EXE) : $(CPP_FILES) $(HPP_FILES)
$(CC) -o $@ $(CPP_FILES)
.PHONY : clean
clean:
rm -rf $(EXE) *.o
【讨论】:
如何编译文件?假设你现在有test.cpp 和test.h,来编译和链接它:
g++ -c test.c
g++ -o test test.o
最简单的Makefile:
test: test.o #means test depends on test.o
g++ -o test test.o
test.o: test.cpp test.h #means test.o depends on test.cpp and test.h
g++ -c test.cpp
#if you want clean? add below line too.
clean:
rm test test.o
如果你的应用依赖多个文件,那么
test: test1.o test2.o test3.o #means your app depends on test1.o test2.o and test3.o
g++ -o test test1.o test2.o test3.o
test1.o: test1.cpp test1.h
g++ -c test1.cpp
test2.o: test2.cpp test2.h
g++ -c test2.cpp
...
【讨论】: