zoukankan      html  css  js  c++  java
  • 关于#ifdef #ifndef

    https://www.cnblogs.com/agnily/p/5848768.html

    1、先看#ifdef的用法:

    复制代码
    #define   KEY1_PA0
    
    #ifdef    KEY1_PA0
    -------------第一段-----------------
    #define               macKEY1_GPIO_CLK                      RCC_APB2Periph_GPIOA
    #define               macKEY1_GPIO_PORT                     GPIOA              
    #define               macKEY1_GPIO_PIN                        GPIO_Pin_0
    
    #else    //KEY2_PC13
    -------------第二段-----------------
    #define               macKEY1_GPIO_CLK                      RCC_APB2Periph_GPIOC
    #define               macKEY1_GPIO_PORT                     GPIOC          
    #define               macKEY1_GPIO_PIN                        GPIO_Pin_13
    
    #endif
    复制代码

    如果定义过 KEY1_PA0,就执行第一段代码,否则就执行第二段代码!!

    2、#ifndef的用法:

    在文件bsp_usart1.h中:

    #ifndef __USART1_H
    #define    __USART1_H
    
    //statement.
    
    #endif  // usart1.h

    如果没有define过__USART1_H,就执行下面的语句;如果定义过,就不执行;

    所以在一个.c文件里面,多次调用到该文件的时候,就相当于只调用一次。

    这是头文件的常用写法!!

    比如:

    在文件main.c中:

    #include "stm32f10x.h"
    
    #include "bsp_usart1.h"

    在bsp_usart1.h中:

    #include "stm32f10x.h"

    在stm32f10x.h中:

    #ifndef __STM32F10x_H
    #define __STM32F10x_H
    
    #endif

    分析:

    (1)、在main.c中调用了一次文件stm32f10x.h,调用的时候发现__STM32F10x_H并没有被define,所以就执行#ifndef - #endif之间的代码,

    执行过后,就相当于define了__STM32F10x_H;

    (2)、当在bsp_usart1.h中再次调用文件stm32f10x.h的时候,发现__STM32F10x_H已经被define过了,所以这时就不再执行#ifndef - #endif 之间的代码了!

    如果没有#ifndef的话,在这里,stm32f10x.h文件里面,如果有定义变量之类的,就会报重定义的错误。

    所以这种写法的作用有:防止重复定义!!

  • 相关阅读:
    电脑提示无法装入/加载SolidWorks DLL文件:sldshellutils如何解决
    vmware vpshere 安装完的必备工作
    建立AD域,修改密码后不重启生效命令
    VMware vSphere 6 Enterprise Plus 注册码
    VMware-viclient-all
    域控中将组策略应用到安全组
    Windows server 2003域控迁移到2012
    SecureCRT 基本设置
    linux之"server" directive is not allowed here in
    wordpress(二)wordpress环境迁移
  • 原文地址:https://www.cnblogs.com/focus-z/p/10260164.html
Copyright © 2011-2022 走看看