The file contains static inline and extern inline.

Source: Internet
Author: User
Tags constant definition define null
File Inclusion

--------------------------------------
Symbolic Meaning
--------------------------------------
Static 1. In the function, it indicates that the value of the variable remains continuous throughout each call.
2. At the function level, this function is only visible to this text.
--------------------------------------
Extern 1. Used for Function Definition, indicating global visibility (redundant, global visibility by default)
2. Used for variables, indicating that they are defined elsewhere
3. Used for functions, indicating that they are defined elsewhere
--------------------------------------

What is Declaration and definition?
C functions or variables have only one definition, but they can have multiple extern declarations. Uniqueness
Rules also apply to constant definitions, structure definitions, type definitions, and macro definitions, except that they are only
The Unit is visible, while the definitions of functions and variables (outside the function) are globally visible by default.

Linux0.11 directory:
|-Boot/
|-Fs/
|-Include/
|-ASM/
|-Linux/
|-Sys/
|-Init/
|-Kernel/
|-Blk_drv/
|-Chr_drv/
|-Math/
|-Lib/
|-Mm/
|-Tools/
|-Makefile

Header file (including)
Constant Definition
Macro definition
Type Definition
Data Structure
Variable Declaration
Function declaration (the extern inline function is included)

All content contained in the header file is only visible to the current compilation unit. If you want to use it, include
This header file can also be defined without being included.

1. Declare variables and functions before using them. In addition, if this function is only used in this unit, you do not need
Declaration, but the definition must be before the call. For example, the function save_old in Kerner/signal. C is defined before,
It is then called only in sys_sigaction, and save_old has a qualified word static. General header files
Contains "constant definition, macro definition, data structure, function declaration, and variable Declaration ". Variable definition is not allowed
Can be included in the header file, because the function or variable can only be defined once.
2. # The scope of the define macro can be defined in the source file, either in the header file or in the. c file.
. For example, set_bit is defined in FS/bitmap. C and fs/super. C, and it is different. Of course
It is called only in this file.
3. The structure can also be defined in the. c file. Some structures are defined in lib/malloc. C for your own use.
In the Linux kernel, to avoid unnecessary header files, such as FS/char_dev.c
Declare the function to be called, so the function declaration can also be written in the. c file.
4. extern indicates in the function declaration that it is a declaration, not a definition; In the function definition, it indicates that the function is
Global. However, extern can be omitted in both cases. The function declaration can be omitted because the Declaration does not
Function bodies are available, while function bodies must be defined. function definitions can be omitted because they are globally visible by default.
In fact, the two extern have different meanings. Linus adds extern before some function declarations, and some do not,
It should be just habitual and there is no essential difference.
5. The string. h header file contains some string operation functions defined in the form of inline embedded functions.
The following is a detailed description in inline. However, the function definition cannot be included in the header file. Otherwise
Duplicate definition error.
6. Some declared but not defined functions in the header file are defined in the library. Of course, these libraries are not included in
In the kernel code. For example, system calls in unistd. h are encapsulated in the library. Another example is sys/STAT. h.
In fact, the CHMOD function is also declared in unistd. h. I think this is Linus to facilitate the include header file.
And the definitions of the two parts are consistent.

Inline
Inlining of functions is an optimization and it really "works" only in
Optimizing compilation. If you don't use-o, no function is really inline.
The inlining of a function is an optimization, and it will only "work" compilation with optimization options. If you
The-O option is not used (in GCC), and no function is called inline.
When a function is both inline and static, if all cballs to the function are
Integrated into the caller, and the function's address is never used, then
The function's own extends er code is never referenced. In this case, GCC
Does not actually output aggreger code for the function, unless you specify
The option-fkeep-inline-functions. Some callcannot be integrated
Various reasons (in particle, cballs that precede the function's definition
Cannot be integrated, and neither can recursive callwithin the definition ).
If there is a nonintegrated call, then the function is compiled to aggreger
Code as usual. The function must also be compiled as usual if the program
Refers to its address, because that can't be inlined.
When a function is both inline and static, all calls to this function can be directly called
Expand the function, and the address of this function is not referenced (I understand it as a function pointer call), that is, the function's
The assembly code is not referenced. In this case, GCC does not actually generate the assembly code of this function unless you specify
-Fkeep-inline-functions option. Some function calls cannot be expanded directly under some conditions (for example
Define previous function calls, such as recursion in the function definition ). If there are no function calls that are directly expanded
The function generates assembly code by example. When the program references the address of this function, this function cannot be called directly.
Expand, so it is also compiled by example.
When an inline function is not static, then the compiler must assume that there
May be callfrom other source files; since a global symbol can be defined only
Once in any program, the function must not be defined in the other source files,
So the calltherein cannot be integrated. Therefore, a non-static inline
Function is always compiled on its own in the usual fashion.
When an inline function is not static, the compiler assumes that it will be called by other compilation units, because a global
The symbol can only be defined once in a program, so this function will not be defined in other compilation units, so
Function calls cannot be expanded directly. Therefore, non-static inline functions are compiled by example.
If you specify both inline and extern in the function definition, then
Definition is used only for inlining. In no case is the function compiled on its
Own, not even if you refer to its address explicitly. Such an address becomes
External reference, as if you had only declared the function, and had not
Defined it.
When a function is inline or extern, the function definition will only be expanded in the call and will not be compiled, even if
You explicitly reference its address. This address will become an external reference, as if you just declared this function, but there is no
Define it.
This combination of inline and extern has almost the effect of a macro. The way
Use it is to put a function definition in a header file with these keywords, and
Put another copy of the definition (lacking inline and extern) in a library file.
The definition in the header file will cause most callto the function to be
Inlined. If any uses of the function remain, they will refer to the single copy
In the library.
The functions of inline and extern are basically the same as those of a macro definition. You can use
The two keywords are defined, while the other copy with the same definition but without the inline and extern keywords is put in the library. Header
The definition in the component directly expands most function calls. If there is still a function call (usually overhead), it will make
Definition in the library.

The above is the GCC documentation, which is released with GCC for free. To sum up, there are the following points:
1. inline is a compilation optimization option, which can be optimized only when certain conditions are met.
This saves the cost of calling a function.
2. Static inline indicates that this function is only visible in the current compilation unit. Both inline and extern inline mean
This function is globally visible. The difference is that inline-defined functions are directly expanded in the defined compilation unit,
In the compilation unit of other call relations, the function is called directly, while the function defined by extern inline is
Is expanded directly in the call, which is equivalent to macro expansion. The forced function call is invalid unless a non-extern is defined.
The version specified by inline is put in the library for calling, so this extern indicates that this is only a declaration.

There are two methods to use linux0.11:
1. In init/Main. C, the fork, pause and other functions use static inline, because they are only used in this unit and are limited
Only visible in this unit.
2. the string operation functions in include/string. h All use extern inline, and contain the string. h header in use.
File. The definition here is equivalent to Declaration. It is not declared elsewhere, nor is it included in multiple files.
The string. h header file causes repeated definitions. I tried it. In linux0.11, Set
If the qualifier extern before the strcpy function is removed, compilation will cause repeated definitions (note that modifying string. h
After the header file is entered, execute make, such as FS/, in a directory that is dependent on string. h.
The message "Nothing can do" is not dependent on string. h ).

Conditional compilation
1. # ifndef
# Else/****** optional ******/
# Endif

(1) When the header file contains some common constant definitions, to prevent repeated definitions when multiple head files are contained, Conditional compilation is used.
That is, it is defined only when no definition is available.
# Ifndef null
# Define null (void *) 0)
# Endif
(2) The most common practice is to prevent a header file from being contained twice when multiple head files are contained, resulting in repeated definitions.
# Ifndef _ filename_h
# DEFINE _ filename_h
# Endif/* _ filename_h */

2. # ifdef
# Else/****** optional ******/
# Endif

(1) determine whether a part of the program is compiled or not.
# Ifdef _ Library __
# Endif/***** _ Library __****/
Before this code is required, # DEFINE _ Library __

3. # If defined
# Elif defined
# Else
# Endif

The single-operand operator defined can be used in the # If or # Elif directive.
# If defined _ symbol _ is equivalent to # ifdef _ symbol _
# If! Defined _ symbol _ is equivalent to # ifndef _ symbol

(1) It is convenient to use multiple options.
# If defined (VAX) | defined (hp300) | defined (Pyr)
# Define segment_size page_size
# Endif

(2) judgment Expression
# Ifdef major_nr
# If (major_nr = 1)
# Elif (major_nr = 2)
# Elif (major_nr = 3)
# Elif
# Elif

(3) you can comment out the code of a large segment./*... */cannot comment out the code containing */, because/**/cannot be nested.
# If 0
# Endif

 

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.