The ifdef of makefile, the use and discrimination of ifeq

Source: Internet
Author: User
#可以用命令行传递变量
RELEASE = ABC

#ifdef variable name cannot be added $ ()
Ifdef RELEASE
$ (warning RELEASE defined)
Else
$ (warning RELEASE not defined)
endif

#ifeq the following argument to be called $ (), because it is a value reference, the value can be numeric or string
Ifeq ($ (RELEASE), ABC)
$ (Warning RELEASE eqal ABC)
Else
$ (Warning RELEASE not equal ABC)
endif

All
@echo ok!
**************************************************
Make compiles a simple example of different versions, such as Debug, release.
Use make variable ver to control the cflags variable, thus compiling different versions.
[/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

Ifeq ($ (ver), Debug)
$ (Warning ver is debug)
CFLAGS =-g-ddebug
Else
$ (Warning ver is not debug)
CFLAGS =-c-o3
endif

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

Clean
RM Test TEST.O

Note: The makefile uses the IFEQ-ELSE-ENDIF structure
You can determine if a make variable is defined.
The make variable can be defined in makefile, or it can be passed by the Make command line.
Because makefile supports environment variables, you pre-define environment variables, or you can use environment variables directly without passing them on the command line
This mechanism makes it possible to write scripts that control different, complex compilations.
For example, different versions of various regions are supported. Control compilation options for Make/D, control compilation of different versions with GEO variables
----------------------------------------
Compiling debug versions, passing variables from the command line
----------------------------------------
[/pts/2@hjj ~/test]$ make Ver=debug
Makefile:6: ver is debug
Gcc-g-ddebug-c-o test.o test.c
Gcc-o Test TEST.O

----------------------------------------
Clean up, no matter version information
----------------------------------------
[/pts/2@hjj ~/test]$ make clean
Makefile:9: ver is not debug
RM Test TEST.O

----------------------------------------
Compiling release version
----------------------------------------
[/pts/2@hjj ~/test]$ make
Makefile:9: ver is not debug
Gcc-c-o3-c-o test.o test.c
Gcc-o Test TEST.O

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.