What is ifndef define endif used in the header file?

Updated on Car 2024-03-13
8 answers
  1. Anonymous users2024-02-06

    in a big software project.

    Inside, there may be multiple files that contain a header file at the same time.

    When these files are compiled and linked into an executable, there are a lot of redefinition errors. Use ifndef define endif in the header file to avoid redefining the header file.

    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 uppercase for the file name.

    End of header file.

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

    If the condition is false, the ** between ifndef test h and endif will not be included again, thus avoiding redefinition. It is mainly used to prevent the duplicate definition of macros.

    and duplicate header files.

  2. Anonymous users2024-02-05

    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 will be omitted to define abc; This statement; define abc;endif;For example, there is a paragraph **, sometimes it needs not to be 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;define abc;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 ifndef abc before that; define abc;endif;]

  3. Anonymous users2024-02-04

    Meaning of ifndef define endif: If it is not defined, then the definition of the completion assumption is generally used to prevent header files from being included repeatedly and improve compilation efficiency. ]

  4. Anonymous users2024-02-03

    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.

  5. Anonymous users2024-02-02

    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.

  6. Anonymous users2024-02-01

    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

  7. Anonymous users2024-01-31

    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.

  8. Anonymous users2024-01-30

    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
7 answers2024-03-13

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. >>>More

8 answers2024-03-13

The format requirements for the document number, title, and body of the red-headed document shall be implemented in accordance with the provisions of the "National Standard for the Format of Official Documents of Party and Government Organs". >>>More

7 answers2024-03-13

It's actually just a written function;

Each chip comes with one or several simple functions that are written; It is convenient for us to develop. >>>More

9 answers2024-03-13

I think it works, I did it once when I first started working, and I don't remember it very well, but you can try it with the seek function, locate it first, read it later, and I remember when I was doing this, I read the book The C Programmer, which is about 234 pages.

6 answers2024-03-13

It's the same thing.

IE caching. In order to improve the speed of visiting web pages, the Internet Explorer browser will use a cumulative acceleration method to store the content of the web pages you have visited (including ** and cookie files, etc.) on your computer. This storage space is called IE cache. >>>More