ifndef define endif in the microcontroller head file

Updated on technology 2024-02-09
7 answers
  1. Anonymous users2024-02-06

    lz understands the same thing, and it is true that it is to avoid repetition, but not to avoid repeating declarations, but to avoid repeating definitions. In general, all declarations of C are repeatable, as long as the duplicate declarations are identical. However, definitions should not be repeated, even if they are identical, and repeating them will lead to compilation errors.

    Inclusion multiple times may lead to duplicate definitions. For example, if the file contains an and, and the file does, then when the file is compiled, it is included twice. We are familiar with the definition of the file type in the header file, so the file is defined twice, and there will be a compilation error.

    Use in header files.

    #ifndef _stdio_h

    #define _stdio_h

    ..Header file body.

    #endif

    If the content of the file body containing the header file is not defined when it is first included, then when it is included again, the stdio h is defined, and ifndef stdio h is false, and the header body (between ifndef stdio h and endif) will be discarded, thus avoiding duplicate definitions of the content containing the header body.

  2. Anonymous users2024-02-05

    If is if.

    ndef is notdef is notdefine is undefined.

    define is the definition.

    endif is the end if.

    If undefined.

    then defined. End if.

    This way, there will be no duplicate definition error if multiple files contain the header file at the same time, otherwise the duplicate definition will be prompted.

  3. Anonymous users2024-02-04

    ifndef and endif are a pair of conditional precompiled statements to give you an example:

    ifndef abc;This means that if the abc feature is not defined, the statement before the end after compilation is omitted.

    defineabc;This statement;

    defineabc;

    endif;

    For example, there is a paragraph **, sometimes it needs to be not compiled or compiled as needed, such as the following **:

    ifdef abc;

    A paragraph**; endif;

    This means that if the abc feature is defined in the program, then the compilation is what is said here"A certain paragraph **", otherwise it will not be compiled, if the program is like this:

    ifndef abc;

    defineabc;

    endif;

    ifdef abc;

    A paragraph**; endif;

    Then the "certain paragraph **" mentioned here will definitely be compiled, because at the beginning of the design program, it may be necessary to decide whether to compile "a certain paragraph **" according to a certain condition, but later it was found that such a condition compilation is no longer necessary, and it is necessary to compile "a certain paragraph **", two ways, one is to put ifdef abc and endif; Remove; The other is like here, in.

    ifdef abc;

    A paragraph**; endif;

    Add before. ifndef abc;

    defineabc;

    endif;

    Generally, in an application development system, the real logical implementation of functions is based on the hardware layer, and is completed in the driver, functional layer programs, and user applications. The main function of the header file is to reuse the global variables (functions) of multiple files, prevent the conflict of definitions, and give a description of each called function, which itself does not need to contain the logical implementation of the program, it only plays a descriptive role, the user program only needs to call the relevant functions or variables according to the interface declaration in the header file, and the linker will look for the corresponding actual definition from the library. (Compilation Mode[1]).

    From the above structure diagram, the header file is the bridge and link between the user application and the library. The header file is not the most important part of the whole software, but it is an indispensable part of the C language family. During compilation, the compiler finds the corresponding function library through the header file, and then exports the actual content of the referenced function to replace the original function.

    And then implement the function at the hardware level.

    As can be seen from the above examples, the header file generally consists of four parts: (1) the copyright and version notice at the beginning of the header file; (2) pretreatment block; (3) the definition of the inline function; (4) Function and class structure declarations, etc. In the header file, use the ifndef define endif structure to generate the preprocessing block, and use the include format to reference the library's header file.

    This structure of the header file is common knowledge for software development in C.

  4. Anonymous users2024-02-03

    This is a usage designed to avoid duplicate header file inclusions.

    Let's say that the header file you wrote yourself is , which contains. The main function file contains, and at the same time, it is included repeatedly, which is a mistake in the C language, because the problem of duplicate definitions has been dealt with, and there will be no duplicate inclusion error. The solution is to use the ifndef command.

    #ifndef my_h_h_

    #define my_h_h_

    #endif

    The above is the format to avoid the header file being repeatedly included, my h h is an identifier, and the common practice is to capitalize all the letters of your header file name, and set the .Change it to underline, if it is safe, you can add a few more H after it, and in the ellipsis place is the content of your header file.

    The first time it is included, my h h is not defined, so define my h h and then include the contents of the header file. The second time it is included, because my h h has already been defined in the first time, the content of the header file is no longer included, so as to prevent duplicate inclusions.

  5. Anonymous users2024-02-02

    ifndef test determines whether there is a test defined, and if so, execute statement 2, otherwise execute statement 1

    Statement 1 else

    Statement 2 endif

  6. Anonymous users2024-02-01

    In a large software project, there may be multiple files that contain a header file at the same time, and when these files are compiled and linked into an executable file, a large number of redefinition errors will occur. Utility ifndef in header files

    #define

    endif avoids the redefinition of header files.

    Method: For example, if you want to write a header file.

    Write two lines at the beginning of the header file:

    #ifndef

    test_h

    #define

    test h is generally capitalized of the file name.

    Analysis: When first contained, since test h is not defined, the condition is true, so that the inclusion (execution.)

    #ifndef

    ** between test h and endif, when the second time it is included, the previous test h has been defined, the condition is false, ifndef

    The ** between test h and endif will not be included again, thus avoiding redefinition. It is mainly used to prevent duplicate macro definitions and duplicate inclusion of header files.

  7. Anonymous users2024-01-31

    Q: I don't really know exactly how to skip it, I don't know if it's skipped directly when it is included or only the compiled part is skipped, and then the last compiled header file is included).

    A: Skip it when it is included. (However, this inclusion is only done at compile time).

    Q: This is the case for a cpp file, suppose ab writes ifndef endif, but ab appears in another cpp, this ifndef endif does not work, a: does.

    Q: i.e. ifndef endif just ensures that the header files in the same cpp will not be compiled repeatedly?

    A: ifndef will ensure that there will be no duplicate references to avoid compilation errors. However, it is not to avoid "using in the same CPP".

    Rather, it is to avoid "any situation that leads to double references", including "in the same cpp file". For example, in and in and in, duplicate definition errors will occur anywhere used or (including other header files).

Related questions
12 answers2024-02-09

A: The bit-addressing area is located between 20F and 2Fh in the data memory. The registers in this interval can be bit-addressed, and they can be bit-manipulated and bit-arithmetic. >>>More

4 answers2024-02-09

You use a P1 port to control the dynamic scanning of 8 digital tubes, and the dynamic scanning display of digital tubes needs to be blanked. >>>More

6 answers2024-02-09

In this case, it depends on sp, ret is equal to (sp) pch, sp-1 sp (sp) pcl, sp-1 sp >>>More

7 answers2024-02-09

It's too urgent now, you haven't learned the principle of microcomputer yet! >>>More

6 answers2024-02-09

Crash, it may be that the on-site interference is too severe. Anyway, even have abandoned chips that don't have internal program memory and EEPROM. The reason is that it is often not a program problem, but a board and PCB design problem. >>>More