make 小技巧

165 阅读1分钟

直接编译

当测试一下小的功能时,可以直接编译,不需要撰写 Makefile。

譬如经典的 hello world:

// filename: hello.c
#include <stdio.h>

int main() {
	printf("Hello, world!\n");
	return 0;
}

可以使用 make 直接编译,make 命令后跟上 hello 即可(去掉扩展名):

$ make hello
cc     hello.c   -o hello
$ ./hello
Hello, world!
$

提供编译选项

可以通过命令行提供编译选项,如下面的 CFLAGS。

$ CFLAGS="-Wall -g" make hello
cc -Wall -g    hello.c   -o hello
$ ./hello
Hello, world!
$

From 《Learn C the Hard Way》Exercise 1 & 2