C language extern usage and the content format of the header file *.h Note

Source: Internet
Author: User
Tags naming convention variable scope

With VS2013 separate write multi-file program, there are many redefinition problems, the summary solution is as follows:

Use the following format in the *.h file:

#ifndef < identity >
#define < identity >

......
......

#endif

< identity > can theoretically be freely named, but the "identity" of each header file should be unique. The naming convention for an identity is usually the header file name all uppercase, underlined, and the "." in the file name. Also becomes underlined, such as: stdio.h
#ifndef _stdio_h_
#define _stdio_h_
......
#endif

The main contents in the. h file are: function declaration (main function), macro definition (only applies to some variable definitions of the current CPP file), and function prototypes

Note: 1. Do not place the specific function in the. h file (in the. cpp file for the specific implementation function)

2. When it comes to specific global variables, "extern is often used in variable declarations, and you declare a global variable in the *.c file, and if the global variable is to be referenced, it is placed in *.h and declared with extern." "------(define the global in *.c and use extern to refer to in. h)

Usage of extern:

Basic explanation : extern can be placed before a variable or function to mark the definition of a variable or function in another file, prompting the compiler to look for its definition in other modules when encountering this variable and function. Also extern can be used for link designation.

That is, the extern has two functions, the first, when it is in conjunction with "C", such as: extern "C" void fun (int a, int b); then tell the compiler to compile the function name of fun by the C rule to translate the corresponding function name instead of C + + The rules in the translation of this function name will be the name of the fun to become unrecognizable, may be [email protected]_int_int#%$ may also be other, this depends on the compiler's "temper" (different compiler uses the same method), why do it, because C + + Support functions of the overloaded Ah, here do not go too much to discuss the problem, if you are interested can go online search, I believe you can get a satisfactory explanation!
Second, when extern does not modify the variable or function together with "C", as in the header file: extern int g_int; Its function is to declare the function or global variable scope of the keyword, its declared functions and variables can be used in other modules of this module, remember that it is a declaration is not a definition! That is, if the B module (the compilation unit) refers to a global variable or function defined in a module (compilation unit), As long as it contains the header file of the A module, in the compilation phase, module B, although it cannot find the function or variable, but it does not error, it will be in the connection when the module a generated from the target code to find this function.

Cite blog--http://www.cnblogs.com/yc_sunniwell/archive/2010/07/14/1777431.html

C language extern usage and the content format of the header file *.h Note

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.