c++ #ifdef的用法
http://www.tuicool.com/articles/mIJnumB
#ifdef的用法
灵活使用#ifdef指示符,我们可以区隔一些与特定头文件、程序库和其他文件版本有关的代码。
代码举例:新建define.cpp文件
- #include "iostream.h"
- int main()
- {
- #ifdef DEBUG
- cout<< "Beginning execution of main()";
- #endif
- return 0;
- }
运行结果为:Press any key to continue
改写代码如下:
- #include "iostream.h"
- #define DEBUG
- int main()
- {
- #ifdef DEBUG
- cout<< "Beginning execution of main()";
- #endif
- return 0;
- }
运行结果为:Beginning execution of main()
Press any key to continue
更一般的情况是,#define语句是包含在一个特定的头文件中。
比如,新建头文件head.h,在文件中加入代码:
- #ifndef DEBUG
- #define DEBUG
- #endif
- 而在define.cpp源文件中,代码修改如下:
- #include "iostream.h"
- #include "head.h"
- int main(){
- #ifdef DEBUG
- cout<< "Beginning execution of main()";
- #endif
- return 0;
- }
运行结果如下:Beginning execution of main()
Press any key to continue
结论:通过使用#ifdef指示符,我们可以区隔一些与特定头文件、程序库和其他文件版本有关的代码
#if, #ifdef, #ifndef, #else, #elif, #endif的用法:
这些命令可以让编译器进行简单的逻辑控制,当一个文件被编译时,你可以用这些命令去决定某些代码的去留,
这些命令式条件编译的命令。
常见的条件编译的三种形式:
①第一种形式:
#if defined(或者是ifdef)<标识符(条件)>
<程序段1>
#endif
②第二种形式:
#if !defined(或者是ifndef)<标识符(条件)>
<程序段1>
#ifdef …
[#elif … ]
[#elif …]
#else …
#endif
示例:
#include <iostream>
using namespace std;
int main()
{
#if DEBUG /*或者是#ifdef DEBUG*/
cout << "条件成立,DEBUG已经定义了!" <<endl;
#else
cout << "条件不成立,DEBUG还没定义" <<endl;
#endif
return 0;
}
//结果输出: 条件不成立,DEBUG还没定义
//如果是添加了#define DEBUG ,输出结果是:条件成立,DEBUG已经定义了!
#include <iostream>
using namespace std;
#define DEBUG
int main()
{
#ifdef DEBUG /*或者是#ifdef DEBUG*/
cout << "条件成立,DEBUG已经定义了!" <<endl;
#else
cout << "条件不成立,DEBUG还没定义" <<endl;
#endif
return 0;
}
//要注意的是,如果是#define 宏名,没有宏体如 #define DEBUG,就必须使用#ifdef或#ifndef与之对应,
//如果是#define 宏名 宏体,如 #define NUM 1,#if 和#ifdef都可以使用。
/*
#define的用法:
*/
示例二:
#include <iostream>
using namespace std;
#define NUM 10
int main()
{
#ifndef NUM
cout << "NUM没有定义!"<<endl;
#elif NUM >= 100
cout << "NUM >100" <<endl;
#elif NUM <100 && NUM >10
cout << "10 < NUM < 100" <<endl;
#elif NUM == 10
cout << "NUM ==10" <<endl;
#else
cout << "NUM < 10" << endl;
#endif
return 0;
}
//输出NUM ==10
也可以在mk文件定义NUM
ifeq ($(BOARD_SCREENRECORD_LANDSCAPE_ONLY),true)
LOCAL_CFLAGS += -DNUM
endif
c++ #ifdef的用法的更多相关文章
- #ifdef的用法【转】
#ifdef的用法 #ifdef的用法灵活使用#ifdef指示符,我们可以区隔一些与特定头文件.程序库和其他文件版本有关的代码.代码举例:新建define.cpp文件 #include &qu ...
- 【转】#ifdef _DEBUG用法小结
原文地址:http://blog.csdn.net/shijizhisheng/article/details/1908054 1 #ifdef _DEBUG virtual void AssertV ...
- #ifdef endif 用法
"#ifdef 语句1 程序2 #endif“ 可翻译为:如果宏定义了语句1则程序2. 作用:我们可以用它区隔一些与特定头文件.程序库和其他文件版本有关的代码. 代码举例:新建define. ...
- 编译预处理命令--define和ifdef的使用
这里将对常用的预处理命令进行学习. 一.宏定义 ·defined 格式:`defined 宏名 数值 或者 `define 宏名 注意:后面没有‘;‘,和单片机不一样: ...
- #if 与 #ifdef 之间的区别
先来看个例子: #define TARGET_LITTLE_ENDINA 1 #define TARGET_BIG_ENDINA 0 #ifdef TARGET_LITTLE_ENDINA call ...
- 关于#ifdef #ifndef
https://www.cnblogs.com/agnily/p/5848768.html 1.先看#ifdef的用法: #define KEY1_PA0 #ifdef KEY1_PA0 ------ ...
- C++ "#"的作用和用法
本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/48879093 1 #和##的作用和用法 ...
- 90.#define高级用法
define把参数变成字符串 #define f(x) printf("%s",#x); define连接两个字符串 #define a(x) a##x define把参数变成字符 ...
- #if 和#ifdef的区别
转自:https://blog.csdn.net/zhangchiytu/article/details/7563329 先看个例子:#define TARGET_LITTLE_ENDINA 1#de ...
随机推荐
- kibana提示"[illegal_argument_exception] mapper [hits] cannot be changed from type [long] to [integer]"
=============================================== 2019/1/30_第1次修改 ccb_warlock == ...
- [How to]HBase集群备份方法
1.简介 当HBase数据库中存在非常重要的业务数据的时候为了保护数据的可以对数据进行备份处理.对于HBase来说从备份操作来看可分为离线备份和在线备份. 2. 前准备 在测试环境上准备有哦两套HBa ...
- 如何使用windows的计划任务?计划任务?
我们经常有一些程序想要过了几小时来运行:比如定时关机 或者说希望能够每天的几点执行一个什么程序: 这些所有的操作都需要用到windows的任务计划:或者叫计划任务:反正都一样 下面小编将指导大家创建一 ...
- 使用TensorFlow给花朵🌺分类
第一步:准备好需要的库 tensorflow-gpu 1.8.0 opencv-python 3.3.1 numpy skimage os pillow 第二步:准备数据集: 链接:http ...
- spring-mvc集成 swagger
问题1:spring-mvc集成 swagger, 配置好后界面 404, 原因: dispatcher-servlet.xml 文件中, 要在这上面 <!-- 启用spring mvc 注解 ...
- 5 个 Laravel Eloquent 小技巧
1. 快速生成 Model & Migration 这并不是一个很多人知道的小技巧,为文章生成 Model 和 Migration. $ php artisan make:migration ...
- 【AtCoder】Dwango Programming Contest V题解
A - Thumbnail 题意简述:给出N个数,找出N个数中和这N个数平均值绝对值最小的数 根据题意写代码即可= = #include <bits/stdc++.h> #define f ...
- 上海高校金马五校赛 J - 小Y写文章
题目大意: 给你n个数字, 定义不连贯值为, max(abs(a[ i ] - b[ i ])) ,现在让你把m个新的数字插入n + 1 个空位中,使得不连贯值最小. 思路:二分不连贯值, 每次进行二 ...
- CSS------ul与div如何排成一行
如图: 代码:(需要给div的float属性设置为left) <div style="margin-top:10px"> <div style="flo ...
- 数据库相关--mysql中的单表查询
一.完整的单表查询语句 select [distinct] *|字段1,字段2, .... from 表名 [where 条件1] [group by 字段名 [having 条件2] ] [orde ...