extern use method specific explanation

Source: Internet
Author: User
Tags variable scope

In the C language, the modifier extern is used before the declaration of a variable or function to illustrate that "this variable/function is defined elsewhere." To be referenced here. (extern can be placed before a variable or function to indicate the definition of a variable or function in another file, prompting the compiler to find its definition in other modules when encountering this variable and function)

Probably extern uses methods for example in the following ways:

The main use methods are:

    • Use extern when declaring variables for other files in this file
    • Use extern when using the C library function in the CPP program file.

Header file. h

extern volatile Serv103_link servlink;extern yaoxinstructype *pstyx;static int ycspace = 500;static int yxspace = 1000;sta tic int sjbgspace = 10*1000;static int gzbgspace = 30*1000;
Then you need to reference it in a. cpp file

One. CPP (multiple CPP)

IEC103_SERV_SEND_SJBG ((Serv103_link *) &servlink, sjbgdata, 1);

St_ret IEC103_SERV_SEND_ZJBG (Serv103_link *servlink, Soeitemtype *zjbgdata, St_uchar SendReason);


Description: I was calling in a different CPP


1. Declaration of the extern modifier variable. For example, supposefileA.C need to refer to the variable int v in B.C, just declare extern int V in A.C and then reference the variable v on the line. It's important to note here. The referenced variableThe link property of v must be an outer link (external), that is, A.C to refer to V, not only depends on declaring extern int V in A.C. It also depends on the variable v itself that can be referenced. This involves another topic of the C language-the scope of the variable. Variables that can be referenced by other modules with the extern modifier are generally global variables.

It is also very important that the extern int v can be placed in the A.C wherever, for example, you can declare the extern int v at the beginning of the function fun definition in A.C. Then it is possible to refer to the variable V, just so it can only refer to V in the function fun scope, which is still a variable scope problem. For this point. A lot of people are worried when they use it. It seems that the extern declaration can only be used for file scopes.


Functions are the same as variables: (The example above is used in a function)

2. extern modifier function declaration.

In essence, there is no difference between a variable and a function.

A function name is a pointer to the beginning of a functional binary block.

Suppose the file a.c need to refer to a function in B.C. For example, in B.C the prototype is an int fun (int mu), then you can declare the extern int fun (int mu) in the A.C, and then you can use fun to do whatever it is. Just like the declaration of a variable, the extern int fun (int mu) can be placed anywhere in the A.C, not necessarily in the scope of the A.C file scope.

A reference to a function in another module. The most frequently used method is to include the header files for these function declarations.

What is the difference between using extern and including a header file to reference a function? extern is much simpler to refer to than including header files! The use of extern is straightforward. Which function is declared with extern to refer to.

This is probably a reflection of the kiss principle! One obvious advantage of this is that it accelerates the process of compiling the program (or, indeed, preprocessing). Save Time .

In the process of compiling large C programs, such differences are obvious.

3. In addition, the extern modifier can be used to indicate the invocation specification of C or C + + functions. For example, call C library functions in C + +. You need to declare the function to be referenced in the C + + program with extern "C".

This is for the linker, telling the linker to link with the C function specification when linking.

The main reason is that when C + + and C programs are compiled, the naming rules are different in the target code.



extern use method specific explanation

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.