Makefile的ifdef和ifeq,以及debug和release

#可以用命令行传递变量
RELEASE = abc

#ifdef 变量名称不能加$()

#它是用来判断变量本身是否定义过.
ifdef RELEASE
$(warning RELEASE defined)
else
$(warning RELEASE not defined)
endif

#ifeq 后面参数要叫$(), 因为是值引用, 值可以为数值或字符串

#它是对变量的引用, 判断其值是否等于一个常量.

ifeq ($(RELEASE),abc)
$(warning RELEASE eqal abc)
else
$(warning RELEASE not equal abc)
endif

all:
    @echo ok!
**************************************************

make 编译不同版本,例如debug, release 的简单示例。
用make 变量type, 控制CFLAGS 变量,从而编译出不同版本。
[/pts/2@hjj ~/test]$ cat test.c
#include <stdio.h>
#include <unistd.h>

int main(int argc,char *argv[])
{
    char *tty=ttyname(0);
    printf("tty is %s\n",tty);
    return 0;
}

[/pts/2@hjj ~/test]$ cat Makefile
CC = gcc
TARGET = test
OBJS = test.o

all: $(TARGET)

ifeq ($(type), debug)
$(warning type is debug)
CFLAGS = -g -O0 -Ddebug
else
$(warning type is not debug)
CFLAGS =  -O2
endif

$(TARGET): $(OBJS)
    $(CC) -o $@ $^

clean:
    rm test test.o

注释: makefile 采用了ifeq-else-endif 结构
可以判别莫个make变量是否定义。
make变量可以在makefile中定义,也可以由make命令行传递。
由于makefile 支持环境变量,所以你预先定义了环境变量,也可以不在命令行中传递而直接使用环境变量
这种机制使得编写脚本控制不同的复杂的编译成为可能,
例如支持各种地域的不同的版本。用地域变量,控制make的编译选项/D,控制编译出不同的版本

这里使用了默认的.c.o规则,你可以用make -p 打印内部数据库查看默认.c.o规则,它会使用CFLAG 变量.
----------------------------------------
编译debug 版本, 从命令行传递变量
----------------------------------------
[/pts/2@hjj ~/test]$ make type=debug
Makefile:6: type is debug
gcc -g -Ddebug    -c -o test.o test.c
gcc -o test test.o

----------------------------------------
清理,无所谓版本信息
----------------------------------------
[/pts/2@hjj ~/test]$ make clean
Makefile:9: type is not debug
rm test test.o

----------------------------------------
编译release 版本
----------------------------------------
[/pts/2@hjj ~/test]$ make
Makefile:9: type is not debug
gcc -c -O3    -c -o test.o test.c
gcc -o test test.o

**************************************************

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值