为了编译整个工程,你可以简单的使用make或者在 make 命令后带上目标all。
$ make gcc -c -Wall test.c gcc -c -Wall anotherTest.c gcc -Wall test.o anotherTest.o -o test
你能看到 make 命令第一次创建的依赖以及实际的目标。
如果你再次查看目录内容linux命令,里面多了一些 .o 文件和执行文件:
$ ls anotherTest.c anotherTest.o Makefile test test.c test.h test.o
现在,假设你对 test.c 文件做了一些修改,重新使用 make 编译工程:
$ make gcc -c -Wall test.c gcc -Wall test.o anotherTest.o -o test
你可以看到只有 test.o 重新编译了,然而另一个 Test.o 没有重新编译。
现在清理所有的目标文件和可执行文件 test,你可以使用目标clean:
$ make clean rm -rf *.o test $ ls anotherTest.c Makefile test.c test.h
你可以看到所有的 .o 文件和执行文件 test 都被删除了。
到目前为止,你可能注意到 make 命令不会编译那些自从上次编译之后就没有更改的文件,但是,如果你想覆盖 make 这种默认的行为,你可以使用 -B 选项。
下面是个例子:
$ make make: Nothing to be done for `all’. $ make -B gcc -c -Wall test.c gcc -c -Wall anotherTest.c gcc -Wall test.o anotherTest.o -o test
你可以看到尽管 make 命令不会编译任何文件,然而make -B会强制编译所有的目标文件以及最终的执行文件。
如果你想知道 make 执行时实际做了什么,使用 -d 选项。
这是一个例子:
$ make -d | more GNU Make 3.81 Copyright (C) 2006 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. This program built for x86_64-pc-linux-gnu Reading makefiles… Reading makefile `Makefile’… Updating makefiles…. Considering target file `Makefile’. Looking for an implicit rule for `Makefile’. Trying pattern rule with stem `Makefile’. Trying implicit prerequisite `Makefile.o’. Trying pattern rule with stem `Makefile’. Trying implicit prerequisite `Makefile.c’. Trying pattern rule with stem `Makefile’. Trying implicit prerequisite `Makefile.cc’. Trying pattern rule with stem `Makefile’. Trying implicit prerequisite `Makefile.C’. Trying pattern rule with stem `Makefile’. Trying implicit prerequisite `Makefile.cpp’. Trying pattern rule with stem `Makefile’. --More--
下一个教程:Linux 一些简单 的命令