Linux学习入门--make学习总结

1.  make的做用

能够根据模块的修改状况从新编译连接目标代码,保证目标代码都是由最新的模块组成的。html

2. makefile的编写方法

  • 格式:

目标:依赖列表spa

    命令code

注意命令左侧是Table制表位htm

  • 注释

语句前加#表示注释blog

  • @避免该行显示,由于make默认是显示执行过程的
  • \用来接续行比较长的状况,与C语言相似
  • 变量的使用,变量通常用大小字母表示,格式: 变量名 = 字符串;引用方式 $(变量名)
  • ifeq(val1,val2), else, endif

举个例子来讲明字符串

gcc_hello_world.c
--------------------------------
#include <stdio.h>
#include "print.h"

int main(void)
{
 
  print_hello();

  return 0;
}


print.c
--------------------------------
#include <stdio.h>
#include "print.h"

void print_hello(void)
{
  printf("Hello world!\n");
}

print.h
---------------------------------
#ifndef PRINT_H
#define PRINT_H

void print_hello(void);

#endif

i. Makefile代码:get

hello:gcc_hello_world.o print.o
	@gcc gcc_hello_world.o print.o -o hello
gcc_hello_world.o:gcc_hello_world.c print.h
	gcc -c gcc_hello_world.c
print.o:print.c print.h
	gcc -c print.c
	
clean:
	rm -f *.o hello

其中clean是伪目标,在make参数时能够使用,执行结果以下:io

ii. 使用变量编写Makefile编译

OBJ=gcc_hello_world.o print.o

hello:$(OBJ)
	gcc $(OBJ) -o hello
gcc_hello_world.o:gcc_hello_world.c print.h
	gcc -c gcc_hello_world.c
print.o:print.c print.h
	gcc -c print.c
	
clean:
	$(RM) *.o hello

-------------------------------------------------------------------------------------------------------------class

明天继续宏定义的实验操做。

修改gcc_hello_world.c以下:

#include <stdio.h>
#include "print.h"

int main(void)
{
#ifdef MACRO  
  print_hello();
#endif
  return 0;
}
CC=gcc

CFLAGS += -DMACRO


TARGETS := hello

all:$(TARGETS)

OBJ=gcc_hello_world.o print.o

$(TARGETS):$(OBJ)
	$(CC) $(CFLAGS) $^ -o $@
	
gcc_hello_world.o:gcc_hello_world.c print.h
	gcc -c $(CFLAGS) gcc_hello_world.c
print.o:print.c print.h
	gcc -c print.c

clean:
	$(RM) *.o hello

:=     +=   ?= 参考文章:Makefile 中:= ?= += =的区别

$^ -o $@能够参考makefile使用说明

其实添加宏定义方法和gcc -D的命令同样。

相关文章
相关标签/搜索