Prevent multiple introduction of header files

Source: Internet
Author: User

1. The consequences of repeated references to the header file:

The header file is repeatedly referenced multiple times: a header file is referenced multiple times in the source file.

Take a look at the code first:

/*test.h*/#include <stdio.h>int a = 10;
/*test.c*/#include "Test.h" #include "Test.h" int main () {printf ("D o o\n"); return 0;}

Show Results:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7F/F8/wKioL1czIknhwaV4AAAL7lxrTAE001.png "title=" Untitled. png "alt=" Wkiol1cziknhwav4aaal7lxrtae001.png "/>

An error occurred, due to multiple introduction of the same file, resulting in variable redefinition, at this time, some people will say, who will * * to introduce two times the header file, oh, then look at the following example:

/*test.h*/#include <stdio.h>int a = 10;
/*test.c*/#include "Test.h" int a = 20;int main () {printf ("a =%d\n", a); return 0;}

This is very likely to be written out, of course, first look at the results:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7F/F8/wKioL1czIknhwaV4AAAL7lxrTAE001.png "title=" Untitled. png "alt=" Wkiol1cziknhwav4aaal7lxrtae001.png "/>

It's the exact same thing as the above.


Summarize:

When referencing the same file multiple times, variable redefinition is possible, causing the code to fail at compile time. So the canonical code should avoid this happening.

This is the discussion on this issue and some of Daniel's views------- Daniel


2. How to avoid this error

The first way:

#ifndef __test_h__#define __test_h__//your Code#endif

With conditional compilation, when the Test_h is first introduced, the compiler detects that the test_h is not yet defined, then executes the following, and when introduced again, detects that the test_h has been defined, then the following is not done, as is the case with the macro definition, can be very effective to avoid the occurrence of such errors.

The second way:

#pragma once//your Code

This is a compiler -supported way to prevent two compilations of the same file, where the same file refers to a physical file (a real-world file ).

Two ways to compare:

#ifndef的方式依赖于宏名字不能冲突, this will not only guarantee that the same file will not be included multiple times, you can also guarantee that two files with exactly the same content will not be included accidentally. Of course, the disadvantage is that if the macro name of the different header file is not careful, it may cause the header file to exist, but the compiler insists it cannot find the state of the statement.


#pragma once is guaranteed by the compiler: the same file will not be included more than once. Note that the "same file" here refers to a physical file, not two files of the same content. The advantage is that you don't have to bother to think about a macro name, and of course you won't get the strange problem caused by the same macro name. The disadvantage is that if there are multiple copies of a header file, there is no guarantee that they will not be included again. Of course, the problem of "cannot find a declaration" is raised in the same way as the macro name, and repeated inclusion is more likely to be found and positive.


The way one is supported by the language so that portability is good , and the way two can avoid name collisions.

This article is from the "Pzd Chuan Feng" blog, please make sure to keep this source http://xujiafan.blog.51cto.com/10778767/1772380

Prevent multiple introduction of header files

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.