A Simple Makefile Tutorial

A Simple Makefile Tutorial

http://www.cs.colby.edu/maxwell/courses/tutorials/maketutor/

Colby College:科尔比学院
maxwell ['mækswel]:n. 麦克斯韦 (磁通量单位)
tutorial [tjuː'tɔːrɪəl]:adj. 辅导的,家庭教师的,个别指导的 n. 个别指导

Makefiles are a simple way to organize code compilation. This tutorial does not even scratch the surface of what is possible using make, but is intended as a starters guide so that you can quickly and easily create your own makefiles for small to medium-sized projects.
Makefile 是组织代码编译的简单方法。本教程甚至没有使用 make 进行肤浅的探讨,而是作为入门指南,以便您可以快速轻松地为中小型项目创建自己的 makefile。

A Simple Example

Let’s start off with the following three files, hellomake.c, hellofunc.c, and hellomake.h, which would represent a typical main program, some functional code in a separate file, and an include file, respectively.
让我们从以下三个文件开始,hellomake.chellofunc.chellomake.h,它们分别代表一个典型的主程序,一些单独文件中的功能代码和一个 include 文件。

separate [(for v.) ˈsepəreɪt; (for adj.) ˈseprət]:vt. 使分离,使分开,使分居 vi. 分开,隔开,分居 adj. 单独的,分开的,不同的,各自的 n. 分开,抽印本

hellomake.c

#include <hellomake.h>

int main() {
  // call a function in another file
  myPrintHelloMake();

  return(0);
}

hellofunc.c

#include <stdio.h>
#include <hellomake.h>

void myPrintHelloMake(void) {

  printf("Hello makefiles!\n");

  return;
}

hellomake.h

/*
example include file
*/

void myPrintHelloMake(void);

Normally, you would compile this collection of code by executing the following command:
通常,您可以通过执行以下命令来编译此代码集。

gcc -o hellomake hellomake.c hellofunc.c -I.

This compiles the two .c files and names the executable hellomake. The -I. is included so that gcc will look in the current directory (.) for the include file hellomake.h. Without a makefile, the typical approach to the test/modify/debug cycle is to use the up arrow in a terminal to go back to your last compile command so you don’t have to type it each time, especially once you’ve added a few more .c files to the mix.
这将编译两个 .c 文件并命名可执行文件 hellomake。-I. 以便 gcc 将在当前目录 (.) 中查找包含文件 hellomake.h。如果没有 makefile,测试/修改/调试周期的典型方法是使用终端中的向上箭头返回上一个编译命令,这样您就不必每次都输入它,特别是在添加一些 .c 文件之后。

mix [mɪks]:vt. 配制,混淆,使混和,使结交 vi. 参与,相混合,交往 n. 混合,混合物,混乱
up arrow:向上箭头键

Unfortunately, this approach to compilation has two downfalls. First, if you lose the compile command or switch computers you have to retype it from scratch, which is inefficient at best. Second, if you are only making changes to one .c file, recompiling all of them every time is also time-consuming and inefficient. So, it’s time to see what we can do with a makefile.
不幸的是,这种编译方法有两个缺点。 首先,如果您丢失了编译命令或切换计算机,则必须从头开始重新键入,这样效率最低。 其次,如果您只对一个 .c 文件进行更改,则每次重新编译所有这些文件也非常耗时且效率低下。所以,现在是时候看看我们可以用 makefile 做什么了。

downfall ['daʊnfɔːl]:n. 垮台,衰败,落下,大雨
inefficient [ɪnɪ'fɪʃ(ə)nt]:adj. 无效率的,效率低的,无能的

The simplest makefile you could create would look something like:

hellomake : hellomake.c hellofunc.c hellomake.h
     gcc -o hellomake hellomake.c hellofunc.c -I.

Makefile 1

# ============================================================================
# Name        : Makefile
# Author      : Yongqiang Cheng
# Version     : Feb 16, 2019
# Copyright   : Copyright 2019 ForeverStrong License
# Description : Workspace in C, Ansi-style
# ============================================================================

hellomake : hellomake.c hellofunc.c hellomake.h
	gcc -o hellomake hellomake.c hellofunc.c -I.

strong@foreverstrong:~/Desktop/makefile_work$ make -n
gcc -o hellomake hellomake.c hellofunc.c -I.
strong@foreverstrong:~/Desktop/makefile_work$ 
strong@foreverstrong:~/Desktop/makefile_work$ ls -l
total 16
-rw-rw-r-- 1 strong strong 120 Mar  9 22:18 hellofunc.c
-rw-rw-r-- 1 strong strong 113 Mar  9 22:18 hellomake.c
-rw-rw-r-- 1 strong strong  58 Mar  9 22:18 hellomake.h
-rw-rw-r-- 1 strong strong 436 Mar  9 22:56 Makefile
strong@foreverstrong:~/Desktop/makefile_work$ 
strong@foreverstrong:~/Desktop/makefile_work$ make
gcc -o hellomake hellomake.c hellofunc.c -I.
strong@foreverstrong:~/Desktop/makefile_work$ 
strong@foreverstrong:~/Desktop/makefile_work$ ls -l
total 28
-rw-rw-r-- 1 strong strong  120 Mar  9 22:18 hellofunc.c
-rwxrwxr-x 1 strong strong 8688 Mar  9 22:57 hellomake
-rw-rw-r-- 1 strong strong  113 Mar  9 22:18 hellomake.c
-rw-rw-r-- 1 strong strong   58 Mar  9 22:18 hellomake.h
-rw-rw-r-- 1 strong strong  436 Mar  9 22:56 Makefile
strong@foreverstrong:~/Desktop/makefile_work$
strong@foreverstrong:~/Desktop/makefile_work$ ./hellomake 
Hello makefiles!
strong@foreverstrong:~/Desktop/makefile_work$

If you put this rule into a file called Makefile or makefile and then type make on the command line it will execute the compile command as you have written it in the makefile. Note that make with no arguments executes the first rule in the file. Furthermore, by putting the list of files on which the command depends on the first line after the :, make knows that the rule hellomake needs to be executed if any of those files change. Immediately, you have solved problem #1 and can avoid using the up arrow repeatedly, looking for your last compile command. However, the system is still not being efficient in terms of compiling only the latest changes.
如果将此规则放入名为 Makefilemakefile 的文件中,然后在命令行中键入 make,它将执行编译命令,就像您在 makefile 中编写它一样。请注意,没有参数的 make 会执行文件中的第一条规则。此外,通过将命令所依赖的文件列表放在 : 之后的第一行,make 知道如果任何这些文件发生更改,则需要执行规则 hellomake。您立即解决了问题 #1 并且可以避免重复使用向上箭头,寻找您的上一个编译命令。但是,在仅编译最新更改方面,系统仍然没有效率。

One very important thing to note is that there is a tab before the gcc command in the makefile. There must be a tab at the beginning of any command, and make will not be happy if it’s not there.
需要注意的一件非常重要的事情是 makefile 中的 gcc 命令之前有一个 tab。任何命令的开头都必须有一个 tab,如果不存在,那么 make 就不会执行。

argument ['ɑːgjʊm(ə)nt]:n. 论证,论据,争吵,内容提要

In order to be a bit more efficient, let’s try the following:
为了提高效率,让我们尝试以下方法:

Makefile 2

CC=gcc
CFLAGS=-I.

hellomake: hellomake.o hellofunc.o
     $(CC) -o hellomake hellomake.o hellofunc.o
# ============================================================================
# Name        : Makefile
# Author      : Yongqiang Cheng
# Version     : Feb 16, 2019
# Copyright   : Copyright 2019 ForeverStrong License
# Description : Workspace in C, Ansi-style
# ============================================================================

CC=gcc
CFLAGS=-I.

hellomake: hellomake.o hellofunc.o 
	$(CC) -o hellomake hellomake.o hellofunc.o

strong@foreverstrong:~/Desktop/makefile_work$ ls -l
total 16
-rw-rw-r-- 1 strong strong 120 Mar  9 22:18 hellofunc.c
-rw-rw-r-- 1 strong strong 113 Mar  9 22:18 hellomake.c
-rw-rw-r-- 1 strong strong  58 Mar  9 22:18 hellomake.h
-rw-rw-r-- 1 strong strong 441 Mar 21 08:33 Makefile
strong@foreverstrong:~/Desktop/makefile_work$ 
strong@foreverstrong:~/Desktop/makefile_work$ cat Makefile 
# ============================================================================
# Name        : Makefile
# Author      : Yongqiang Cheng
# Version     : Feb 16, 2019
# Copyright   : Copyright 2019 ForeverStrong License
# Description : Workspace in C, Ansi-style
# ============================================================================

CC=gcc
CFLAGS=-I.

hellomake: hellomake.o hellofunc.o 
	$(CC) -o hellomake hellomake.o hellofunc.o

strong@foreverstrong:~/Desktop/makefile_work$ 
strong@foreverstrong:~/Desktop/makefile_work$ make
gcc -I.   -c -o hellomake.o hellomake.c
gcc -I.   -c -o hellofunc.o hellofunc.c
gcc -o hellomake hellomake.o hellofunc.o
strong@foreverstrong:~/Desktop/makefile_work$ 
strong@foreverstrong:~/Desktop/makefile_work$ ll
total 44
drwxrwxr-x 2 strong strong 4096 Mar 21 08:35 ./
drwxr-xr-x 8 strong strong 4096 Mar  8 09:51 ../
-rw-rw-r-- 1 strong strong  120 Mar  9 22:18 hellofunc.c
-rw-rw-r-- 1 strong strong 1520 Mar 21 08:35 hellofunc.o
-rwxrwxr-x 1 strong strong 8688 Mar 21 08:35 hellomake*
-rw-rw-r-- 1 strong strong  113 Mar  9 22:18 hellomake.c
-rw-rw-r-- 1 strong strong   58 Mar  9 22:18 hellomake.h
-rw-rw-r-- 1 strong strong 1384 Mar 21 08:35 hellomake.o
-rw-rw-r-- 1 strong strong  441 Mar 21 08:33 Makefile
strong@foreverstrong:~/Desktop/makefile_work$ 
strong@foreverstrong:~/Desktop/makefile_work$ ./hellomake 
Hello makefiles!
strong@foreverstrong:~/Desktop/makefile_work$

So now we’ve defined some constants CC and CFLAGS. It turns out these are special constants that communicate to make how we want to compile the files hellomake.c and hellofunc.c. In particular, the macro CC is the C compiler to use, and CFLAGS is the list of flags to pass to the compilation command. By putting the object files--hellomake.o and hellofunc.o--in the dependency list and in the rule, make knows it must first compile the .c versions individually, and then build the executable hellomake.
所以现在我们已经定义了一些常量 CCCFLAGS。事实证明,这些是特殊的常量,它们与 make 通信以便我们编译文件 hellomake.c 和 hellofunc.c。特别是,宏 CC 是要使用的 C 编译器,CFLAGS 是要传递给编译命令的标志列表。 通过将对象文件 - hellomake.o 和 hellofunc.o - 放在依赖项列表和规则中,make 知道它必须首先单独编译 .c 版本,然后构建可执行文件 hellomake。

individually [ɪndɪ'vɪdjʊ(ə)lɪ]:adv. 个别地,单独地
dependency [dɪ'pend(ə)nsɪ]:n. 属国,从属,从属物
compilation [kɒmpɪ'leɪʃ(ə)n]:n. 编译,编辑,汇编
particular [pəˈtɪkjʊlə(r)]:adj. 特别的,详细的,独有的,挑剔的 n. 详细说明,个别项目
macro ['mækrəʊ]:adj. 巨大的,大量的 n. 宏,巨
constant ['kɒnst(ə)nt]:adj. 不变的,恒定的,经常的 n. 常数,恒量

Using this form of makefile is sufficient for most small scale projects. However, there is one thing missing: dependency on the include files. If you were to make a change to hellomake.h, for example, make would not recompile the .c files, even though they needed to be. In order to fix this, we need to tell make that all .c files depend on certain .h files. We can do this by writing a simple rule and adding it to the makefile.
对于大多数小规模项目,使用这种形式的 makefile 就足够了。但是,缺少一件事:包含文件的依赖。例如,如果你要对 hellomake.h 进行更改,make 就不会重新编译 .c 文件,即使它们需要。为了解决这个问题,我们需要告诉 make 所有 .c 文件都依赖于某些 .h 文件。 我们可以通过编写一个简单的规则并将其添加到 makefile 来实现。

Makefile 3

CC=gcc
CFLAGS=-I.
DEPS = hellomake.h

%.o: %.c $(DEPS)
	$(CC) -c -o $@ $< $(CFLAGS)

hellomake: hellomake.o hellofunc.o 
	$(CC) -o hellomake hellomake.o hellofunc.o 
# ============================================================================
# Name        : Makefile
# Author      : Yongqiang Cheng
# Version     : Feb 16, 2019
# Copyright   : Copyright 2019 ForeverStrong License
# Description : Workspace in C, Ansi-style
# ============================================================================

CC=gcc
CFLAGS=-I.
DEPS = hellomake.h

%.o: %.c $(DEPS)
	$(CC) -c -o $@ $< $(CFLAGS)

hellomake: hellomake.o hellofunc.o 
	$(CC) -o hellomake hellomake.o hellofunc.o 
strong@foreverstrong:~/Desktop/makefile_work$ ls -l
total 16
-rw-rw-r-- 1 strong strong 120 Mar  9 22:18 hellofunc.c
-rw-rw-r-- 1 strong strong 113 Mar  9 22:18 hellomake.c
-rw-rw-r-- 1 strong strong  58 Mar  9 22:18 hellomake.h
-rw-rw-r-- 1 strong strong 507 Mar 21 15:19 Makefile
strong@foreverstrong:~/Desktop/makefile_work$ 
strong@foreverstrong:~/Desktop/makefile_work$ cat Makefile 
# ============================================================================
# Name        : Makefile
# Author      : Yongqiang Cheng
# Version     : Feb 16, 2019
# Copyright   : Copyright 2019 ForeverStrong License
# Description : Workspace in C, Ansi-style
# ============================================================================

CC=gcc
CFLAGS=-I.
DEPS = hellomake.h

%.o: %.c $(DEPS)
	$(CC) -c -o $@ $< $(CFLAGS)

hellomake: hellomake.o hellofunc.o 
	$(CC) -o hellomake hellomake.o hellofunc.o 
strong@foreverstrong:~/Desktop/makefile_work$ 
strong@foreverstrong:~/Desktop/makefile_work$ make
gcc -c -o hellomake.o hellomake.c -I.
gcc -c -o hellofunc.o hellofunc.c -I.
gcc -o hellomake hellomake.o hellofunc.o 
strong@foreverstrong:~/Desktop/makefile_work$ 
strong@foreverstrong:~/Desktop/makefile_work$ ls -l
total 36
-rw-rw-r-- 1 strong strong  120 Mar  9 22:18 hellofunc.c
-rw-rw-r-- 1 strong strong 1520 Mar 21 15:19 hellofunc.o
-rwxrwxr-x 1 strong strong 8688 Mar 21 15:19 hellomake
-rw-rw-r-- 1 strong strong  113 Mar  9 22:18 hellomake.c
-rw-rw-r-- 1 strong strong   58 Mar  9 22:18 hellomake.h
-rw-rw-r-- 1 strong strong 1384 Mar 21 15:19 hellomake.o
-rw-rw-r-- 1 strong strong  507 Mar 21 15:19 Makefile
strong@foreverstrong:~/Desktop/makefile_work$ 
strong@foreverstrong:~/Desktop/makefile_work$ ./hellomake 
Hello makefiles!
strong@foreverstrong:~/Desktop/makefile_work$

This addition first creates the macro DEPS, which is the set of .h files on which the .c files depend. Then we define a rule that applies to all files ending in the .o suffix. The rule says that the .o file depends upon the .c version of the file and the .h files included in the DEPS macro. The rule then says that to generate the .o file, make needs to compile the .c file using the compiler defined in the CC macro. The -c flag says to generate the object file, the -o $@ says to put the output of the compilation in the file named on the left side of the :, the $< is the first item in the dependencies list, and the CFLAGS macro is defined as above.
此添加首先创建宏 DEPS,它是 .c 文件所依赖的 .h 文件集合。然后我们定义一个规则,适用于以 .o 后缀结尾的所有文件。规则说 .o 文件依赖于文件的 .c 版本和 DEPS 宏中包含的 .h 文件。然后规则要生成 .o 文件,需要使用 CC 宏中定义的编译器编译 .c 文件。-c 标志表示要生成目标文件,-o $@ 表示将编译的输出放在 : 的左侧命名的文件中,$< 是依赖项列表中的第一项,并且 CFLAGS 宏定义如上。

As a final simplification, let’s use the special macros $@ and $^, which are the left and right sides of the :, respectively, to make the overall compilation rule more general. In the example below, all of the include files should be listed as part of the macro DEPS, and all of the object files should be listed as part of the macro OBJ.
作为最后的简化,让我们分别使用特殊的宏 $@$^,它们分别是 : 的左侧和右侧,使整个编译规则更加通用。在下面的示例中,所有包含文件都应作为宏 DEPS 的一部分列出,并且所有目标文件都应作为宏 OBJ 的一部分列出。

Makefile 4

CC=gcc
CFLAGS=-I.
DEPS = hellomake.h
OBJ = hellomake.o hellofunc.o 

%.o: %.c $(DEPS)
	$(CC) -c -o $@ $< $(CFLAGS)

hellomake: $(OBJ)
	$(CC) -o $@ $^ $(CFLAGS)
# ============================================================================
# Name        : Makefile
# Author      : Yongqiang Cheng
# Version     : Feb 16, 2019
# Copyright   : Copyright 2019 ForeverStrong License
# Description : Workspace in C, Ansi-style
# ============================================================================

CC=gcc
CFLAGS=-I.
DEPS = hellomake.h
OBJ = hellomake.o hellofunc.o 

%.o: %.c $(DEPS)
	$(CC) -c -o $@ $< $(CFLAGS)

hellomake: $(OBJ)
	$(CC) -o $@ $^ $(CFLAGS)
strong@foreverstrong:~/Desktop/makefile_work$ ls -l
total 16
-rw-rw-r-- 1 strong strong 120 Mar  9 22:18 hellofunc.c
-rw-rw-r-- 1 strong strong 113 Mar  9 22:18 hellomake.c
-rw-rw-r-- 1 strong strong  58 Mar  9 22:18 hellomake.h
-rw-rw-r-- 1 strong strong 501 Mar 21 15:35 Makefile
strong@foreverstrong:~/Desktop/makefile_work$ 
strong@foreverstrong:~/Desktop/makefile_work$ cat Makefile 
# ============================================================================
# Name        : Makefile
# Author      : Yongqiang Cheng
# Version     : Feb 16, 2019
# Copyright   : Copyright 2019 ForeverStrong License
# Description : Workspace in C, Ansi-style
# ============================================================================

CC=gcc
CFLAGS=-I.
DEPS = hellomake.h
OBJ = hellomake.o hellofunc.o 

%.o: %.c $(DEPS)
	$(CC) -c -o $@ $< $(CFLAGS)

hellomake: $(OBJ)
	$(CC) -o $@ $^ $(CFLAGS)
strong@foreverstrong:~/Desktop/makefile_work$ 
strong@foreverstrong:~/Desktop/makefile_work$ make
gcc -c -o hellomake.o hellomake.c -I.
gcc -c -o hellofunc.o hellofunc.c -I.
gcc -o hellomake hellomake.o hellofunc.o -I.
strong@foreverstrong:~/Desktop/makefile_work$ 
strong@foreverstrong:~/Desktop/makefile_work$ ll
total 44
drwxrwxr-x 2 strong strong 4096 Mar 21 15:35 ./
drwxr-xr-x 8 strong strong 4096 Mar  8 09:51 ../
-rw-rw-r-- 1 strong strong  120 Mar  9 22:18 hellofunc.c
-rw-rw-r-- 1 strong strong 1520 Mar 21 15:35 hellofunc.o
-rwxrwxr-x 1 strong strong 8688 Mar 21 15:35 hellomake*
-rw-rw-r-- 1 strong strong  113 Mar  9 22:18 hellomake.c
-rw-rw-r-- 1 strong strong   58 Mar  9 22:18 hellomake.h
-rw-rw-r-- 1 strong strong 1384 Mar 21 15:35 hellomake.o
-rw-rw-r-- 1 strong strong  501 Mar 21 15:35 Makefile
strong@foreverstrong:~/Desktop/makefile_work$ 
strong@foreverstrong:~/Desktop/makefile_work$ ./hellomake 
Hello makefiles!
strong@foreverstrong:~/Desktop/makefile_work$

So what if we want to start putting our .h files in an include directory, our source code in a src directory, and some local libraries in a lib directory? Also, can we somehow hide those annoying .o files that hang around all over the place? The answer, of course, is yes. The following makefile defines paths to the include and lib directories, and places the object files in an obj subdirectory within the src directory. It also has a macro defined for any libraries you want to include, such as the math library -lm. This makefile should be located in the src directory. Note that it also includes a rule for cleaning up your source and object directories if you type make clean. The .PHONY rule keeps make from doing something with a file named clean.
那么如果我们想要将我们的 .h 文件放在 include 目录中,我们的源代码放在 src 目录中,以及 lib 目录中的一些本地库怎么办? 此外,我们可以以某种方式隐藏那些遍布各地的令人讨厌的 .o 文件吗?答案当然是肯定的。以下 makefile 定义了 include 和 lib 目录的路径,并将目标文件放在 src 目录下的 obj 子目录中。它还为您要包含的任何库定义了一个宏,例如数学库 -lm。该 makefile 应位于 src 目录中。请注意,如果键入 make clean,它还包括清理源和目标目录的规则。.PHONY 规则使 make 不会使用名为 clean 的文件执行某些操作。

Makefile 5

IDIR =../include
CC=gcc
CFLAGS=-I$(IDIR)

ODIR=obj
LDIR =../lib

LIBS=-lm

_DEPS = hellomake.h
DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS))

_OBJ = hellomake.o hellofunc.o 
OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))


$(ODIR)/%.o: %.c $(DEPS)
	$(CC) -c -o $@ $< $(CFLAGS)

hellomake: $(OBJ)
	$(CC) -o $@ $^ $(CFLAGS) $(LIBS)

.PHONY: clean

clean:
	rm -f $(ODIR)/*.o *~ core $(IDIR)/*~
# ============================================================================
# Name        : Makefile
# Author      : Yongqiang Cheng
# Version     : Feb 16, 2019
# Copyright   : Copyright 2019 ForeverStrong License
# Description : Workspace in C, Ansi-style
# ============================================================================

IDIR =../include
CC=gcc
CFLAGS=-I$(IDIR)

ODIR=obj
LDIR =../lib

_DEPS = hellomake.h
DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS))

_OBJ = hellomake.o hellofunc.o 
OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))


$(ODIR)/%.o: %.c $(DEPS)
	$(CC) -c -o $@ $< $(CFLAGS)

hellomake: $(OBJ)
	$(CC) -o $@ $^ $(CFLAGS)

.PHONY: clean

clean:
	rm -f $(ODIR)/*.o *~ core $(IDIR)/*~ 
strong@foreverstrong:~/Desktop/makefile_work$ ls -l
total 12
drwxrwxr-x 2 strong strong 4096 Mar 22 14:14 include
drwxrwxr-x 2 strong strong 4096 Mar 22 14:14 lib
drwxrwxr-x 3 strong strong 4096 Mar 22 14:15 src
strong@foreverstrong:~/Desktop/makefile_work$ 
strong@foreverstrong:~/Desktop/makefile_work$ cd src/
strong@foreverstrong:~/Desktop/makefile_work/src$ ll
total 24
drwxrwxr-x 3 strong strong 4096 Mar 22 14:15 ./
drwxrwxr-x 5 strong strong 4096 Mar 22 14:14 ../
-rw-rw-r-- 1 strong strong  120 Mar  9 22:18 hellofunc.c
-rw-rw-r-- 1 strong strong  113 Mar  9 22:18 hellomake.c
-rw-rw-r-- 1 strong strong  700 Mar 22 14:13 Makefile
drwxrwxr-x 2 strong strong 4096 Mar 22 14:15 obj/
strong@foreverstrong:~/Desktop/makefile_work/src$ 
strong@foreverstrong:~/Desktop/makefile_work/src$ make
gcc -c -o obj/hellomake.o hellomake.c -I../include
gcc -c -o obj/hellofunc.o hellofunc.c -I../include
gcc -o hellomake obj/hellomake.o obj/hellofunc.o -I../include
strong@foreverstrong:~/Desktop/makefile_work/src$ 
strong@foreverstrong:~/Desktop/makefile_work/src$ ll
total 36
drwxrwxr-x 3 strong strong 4096 Mar 22 14:18 ./
drwxrwxr-x 5 strong strong 4096 Mar 22 14:14 ../
-rw-rw-r-- 1 strong strong  120 Mar  9 22:18 hellofunc.c
-rwxrwxr-x 1 strong strong 8688 Mar 22 14:18 hellomake*
-rw-rw-r-- 1 strong strong  113 Mar  9 22:18 hellomake.c
-rw-rw-r-- 1 strong strong  700 Mar 22 14:13 Makefile
drwxrwxr-x 2 strong strong 4096 Mar 22 14:18 obj/
strong@foreverstrong:~/Desktop/makefile_work/src$ 
strong@foreverstrong:~/Desktop/makefile_work/src$ make clean
rm -f obj/*.o *~ core ../include/*~ 
strong@foreverstrong:~/Desktop/makefile_work/src$ 
strong@foreverstrong:~/Desktop/makefile_work/src$ ll
total 36
drwxrwxr-x 3 strong strong 4096 Mar 22 14:18 ./
drwxrwxr-x 5 strong strong 4096 Mar 22 14:14 ../
-rw-rw-r-- 1 strong strong  120 Mar  9 22:18 hellofunc.c
-rwxrwxr-x 1 strong strong 8688 Mar 22 14:18 hellomake*
-rw-rw-r-- 1 strong strong  113 Mar  9 22:18 hellomake.c
-rw-rw-r-- 1 strong strong  700 Mar 22 14:13 Makefile
drwxrwxr-x 2 strong strong 4096 Mar 22 14:19 obj/
strong@foreverstrong:~/Desktop/makefile_work/src$ 
strong@foreverstrong:~/Desktop/makefile_work/src$ make
gcc -c -o obj/hellomake.o hellomake.c -I../include
gcc -c -o obj/hellofunc.o hellofunc.c -I../include
gcc -o hellomake obj/hellomake.o obj/hellofunc.o -I../include
strong@foreverstrong:~/Desktop/makefile_work/src$ 
strong@foreverstrong:~/Desktop/makefile_work/src$ cat Makefile 
# ============================================================================
# Name        : Makefile
# Author      : Yongqiang Cheng
# Version     : Feb 16, 2019
# Copyright   : Copyright 2019 ForeverStrong License
# Description : Workspace in C, Ansi-style
# ============================================================================

IDIR =../include
CC=gcc
CFLAGS=-I$(IDIR)

ODIR=obj
LDIR =../lib

_DEPS = hellomake.h
DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS))

_OBJ = hellomake.o hellofunc.o 
OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))


$(ODIR)/%.o: %.c $(DEPS)
	$(CC) -c -o $@ $< $(CFLAGS)

hellomake: $(OBJ)
	$(CC) -o $@ $^ $(CFLAGS)

.PHONY: clean

clean:
	rm -f $(ODIR)/*.o *~ core $(IDIR)/*~ 
strong@foreverstrong:~/Desktop/makefile_work/src$ 
strong@foreverstrong:~/Desktop/makefile_work/src$ ./hellomake 
Hello makefiles!
strong@foreverstrong:~/Desktop/makefile_work/src$

So now you have a perfectly good makefile that you can modify to manage small and medium-sized software projects. You can add multiple rules to a makefile; you can even create rules that call other rules. For more information on makefiles and the make function, check out the GNU Make Manual, which will tell you more than you ever wanted to know (really).
所以现在你有一个非常好的 makefile,你可以修改它来管理中小型软件项目。您可以向 makefile 添加多个规则,您甚至可以创建调用其他规则的规则。有关 makefile 和 make 函数的更多信息,请查看 GNU Make Manual,它将告诉你比你想知道的更多。
http://www.gnu.org/software/make/manual/make.html

References

GNU make
http://www.gnu.org/software/make/manual/make.html
A Simple Makefile Tutorial
http://www.cs.colby.edu/maxwell/courses/tutorials/maketutor/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值