The macro definition in the header file in C language,

Source: Internet
Author: User

The macro definition in the header file in C language,

I don't know what it means when I see Conditional compilation in the header file, but the master says, "I have to write it like that." I did it, but I don't know why.

Okay, I admit that I am just a code Porter. Oh, no, maybe not.

The following is your understanding after learning ~~~

Code:

1 # ifndef DSP_ADC_BSP_H _ // prevent header files from being repeatedly contained 2 # define DSP_ADC_BSP_H _ 3 4 # ifdef _ DSP_ADC_BSP_C _ // determine whether the file is defined 5 # define ADCBSPEXT // 6 # else 7 # define ADCBSPEXT extern // when undefined, execute 8 # endif // _ DSP_ADC_BSP_C _ 9 10 ADCBSPEXT void InitileAdc (void ); 11 ADCBSPEXT void CaptureAdcInt (); 12 ADCBSPEXT unsigned char CaptureAdcInt_data (); 13 14 # endif/* DSP_ADC_BSP_H _*/

Maybe I am a zombie. In short, I did not.

Explanation:

Rows 1st, 2, and 14th: avoid repeated definitions.

# Ifndef DSP_ADC_BSP_H _ // If DSP_ADC_BSP_H _ is not defined, run the following command:
# Define DSP_ADC_BSP_H _ // defines DSP_ADC_BSP_H _
...
# Endif/* DSP_ADC_BSP_H _ * // The annotation indicates the corresponding # ifndef command to the programmer.

When this file is contained for the first time, the macro DSP_ADC_BSP_H _ is not defined, and the Preprocessor is allowed to retain multiple lines between # ifndef and # endif.

If the file is contained again, the Preprocessor deletes the content between # ifndef and # endif.

This means that if two. c files contain the. h file at the same time, it will be defined only once.

3rd ~ Row 12: external functions and variables can be used in other modules only once in the. h file.

First, describe extern:

Extern int I;

The declared information provided by the compiler is that I is an int variable, but the compiler does not allocate storage units for I. The above Declaration is not a definition of variables, but a prompt that the compiler needs to access the variables defined elsewhere.

Variables can be declared multiple times in the program, but can only be defined once.

# Ifdef _ DSP_ADC_BSP_C _ // judge whether _ DSP_ADC_BSP_C _ is defined
# Define ADCBSPEXT // if the execution is defined
# Else
# Define ADCBSPEXT extern // if no execution is defined
# Endif

ADCBSPEXT void InitileAdc (void );
ADCBSPEXT void CaptureAdcInt ();
ADCBSPEXT unsigned char CaptureAdcInt_data ();

The. c file of the corresponding module contains the following sentence:

# Define _ DSP_ADC_BSP_C _ // defines _ DSP_ADC_BSP_C _

When this module. c file is used, the ADCBSPEXT is replaced with a blank one.

The function declaration is:

void InitileAdc(void);void CaptureAdcInt();unsigned char CaptureAdcInt_data();

When these functions are used for other. c files, ADCBSPEXT is replaced with extern for use.

extern void InitileAdc(void);extren void CaptureAdcInt();extern unsigned char CaptureAdcInt_data();

  

One cainiao for your reference only.

 

Related Article

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.