file contains static Inline,extern inline

Source: Internet
Author: User
Tags constant definition define null function definition
file contains

——————————————————————————————————————
Symbolic meaning
——————————————————————————————————————
Static 1. Inside a function, the value of the variable is persisted between invocations
2. At the level of the function, it means that the function is visible only to this text
——————————————————————————————————————
extern 1 for function definitions, representing global visibility (redundant, default globally visible)
2. For variables, indicating that it is defined elsewhere
3. For functions, indicating that it is defined elsewhere
——————————————————————————————————————

What is a declaration, what is a definition.
A function or variable in the C language has and has only one definition, but it can have multiple extern declarations. Uniqueness
The rules also apply to constant definitions, struct definitions, type definitions, and macro definitions, except that they default only to the
Cells are visible, and the definition of functions and variables (outside functions) is globally visible by default.

Linux0.11 's Directory:
|-boot/
|-fs/
|-include/
|-asm/
|-linux/
|-sys/
|-init/
|-kernel/
|-blk_drv/
|-chr_drv/
|-math/
|-lib/
|-mm/
|-tools/
|-makefile

Header files (including)
Constant definition
Macro definition
Type definition
Data
Variable declaration
function declaration (extern inline function counted)

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

1. Variables and functions must be declared before use. In addition, if the function is used only in this unit, you can not
Declaration, but defined to precede the call. As in kerner/signal.c, the function save_old is defined in the previous
It is then invoked only in Sys_sigaction, and the Save_old has a qualifier static. Header File General
Contains "constant definition, macro definition, data structure, function declaration, variable declaration". Variable definition is not
Can be included in the header file, because either a function or a variable can only be defined once.
2. #define宏定义的作用域在源文件之内, which can be defined either in the header file or in the. c file
Defined in. For example, set_bit are defined in both FS/BITMAP.C and fs/super.c, and of course
Only individually in this file are called.
3. The structure can also be defined in the. c file, and the LIB/MALLOC.C defines some structures for its own use.
In the Linux kernel, in order to avoid more unnecessary headers, such as FS/CHAR_DEV.C, direct
Declare the function you want to call, so the function declaration can also be written in the. c file.
4.extern indicates in a function declaration that it is a declaration, not a definition, and that the function in the function definition is
The global. However, these two places can be omitted from the extern, function declaration can be omitted because the declaration did not
There is a function body, and the definition must have a function body; the function definition can be omitted because the default is globally visible.
In fact these two extern meanings are different. Linus added extern before some of the function declarations, some of them not,
It should only be habitual, there is no essential difference.
The 5.string.h header file contains some string manipulation functions defined in the form of inline inline functions.
The following inline specify. However, the function definition cannot be included in the header file, or it can cause
Duplicate-defined error.
6. Some functions that are declared but not defined in the header file are defined in the library and are, of course, not included in the
In the kernel code. For example, system calls in Unistd.h are encapsulated inside the library. Another example of sys/stat.h
The chmod function In fact has a statement in Unistd.h, I think this is Linus for convenience include header file
Only to declare again, and the two definitions are consistent.

Inline
Inlining of functions is a optimization and it really "works" in
Optimizing compilation. If you don ' t use-o, the No function is really inline.
The inlining of a function is an optimization, and it will only "work" in the compilation with optimized options. If you
The-o option is not used (in GCC) and no function is inline.
When a function was both inline and static, if all calls to the function are
Integrated into the caller, and the function ' s address is never used, then
The function ' s own assembler The code is never referenced. In the case, GCC
does not actually output assembler code for the function, unless you specify
The option-fkeep-inline-functions. Some calls cannot is integrated for
Various reasons (in particular, calls that precede the function ' s definition
cannot be integrated, and neither can recursive calls within the definition).
If There is a nonintegrated call, then the function was compiled to assembler
Code as usual. The function must also is compiled as usual if the program
Refers to it address,because that can ' t be inlined.
When a function is inline and static at the same time, all calls to this function can be directly invoked in the
function, and the address of this function is not referenced (my understanding is that the function pointer calls), that is, the function's
The assembly code is not referenced. In this case, GCC does not actually generate assembly code for this function unless you specify
-fkeep-inline-functions option. Some function calls cannot be directly expanded under certain conditions (for example, in a function
Defines the previous function call, as in the function definition, which has recursion. If there is a function call that is not expanded directly, this
function routinely generate assembly code. When a program references the address of this function, this function cannot be directly in the call
Expands, so routinely is also compiled.
When a inline function is isn't static, then the compiler must assume that there
May is calls from other source files; Since a global symbol can defined only
Once in no program, the function must is defined in the other source files,
So the calls therein cannot is integrated. Therefore, a non-static inline
The function is always compiled on its own the usual fashion.
When a inline function is not static, the compiler assumes that it will be invoked by other compilation units because a global
Symbols can only be defined once in a program, so this function is not defined in other compilation units, so the
function calls cannot be expanded directly. Therefore, inline functions that are not static will be routinely compiled.
If you are specify both inline and extern in the function definition, then the
The definition is used a for inlining. In no, the function compiled on its
Own, not even if your refer to it address explicitly. Such an address becomes
External reference, as if you are had only declared the function, and had not
Defined it.
When a function is inline and extern, the function definition will only expand in the call and not be compiled, even if
You explicitly refer to its address. This address becomes an external reference, as if you just declared the function without
Define it.
This combination of inline and extern has almost the effect of a macro. The way to
Use it's 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 would cause most calls to the function
inlined. If any uses of the function remain, they'll refer to the single
In the library.
Inline and extern functions are essentially equivalent to a macro definition. The method used is to use this in the header file
Two keyword definitions, while another copy of the same definition without inline and extern keywords is placed in the library. The first article
The definition in a piece makes most function calls unfold directly. If a function call exists (usually overhead), it causes the
Use the definitions in the library.

This is the description document for GCC, released with GCC for free. It boils down to the following points:
1.inline is a compilation optimization option that needs to meet certain conditions to achieve the goal of optimization, that is, directly in the call
and eliminates the overhead of calling the function.
2.static inline indicates that this function is visible only in this compilation unit. inline or extern inline all mean
The function is globally visible, and the inline-qualified function is expanded directly in the defined compilation unit.
It is called directly in the compilation unit of other call relationships, while the extern inline qualified function
Calls are expanded directly, which is equivalent to macro expansion, and forced function calls are not valid unless you define a non-extern
The inline-qualified version is placed in the library for invocation, so this extern indicates that this is just a statement.

There are two ways to use linux0.11:
Functions such as Fork,pause defined in 1.init/main.c use static inline because they are used only in this unit, limiting
Visible only in this unit.
The string manipulation functions in 2.include/string.h use an extern inline and include the string.h header when used
File. The definition here is also equivalent to a declaration, and is not declared elsewhere, nor is it because multiple files contain
String.h the header file to cause a duplicate definition error. I tried it, and in linux0.11 the String.h
strcpy the qualifier before the function is removed, the compilation causes a duplicate definition error (note modify String.h
The header file is followed by a directory execution make with string.h dependencies, such as fs/, because the outermost makefile and
Does not depend on the string.h, will prompt nothing can do.

Conditional compilation
1. #ifndef
#else/****** Optional ******/
#endif

(1) When a header file contains some commonly used constant definitions, conditional compilation is used to prevent duplicate definitions when containing multiple headers
Defined only when not defined
#ifndef NULL
#define NULL ((void *) 0)
#endif
(2) The most commonly used to prevent the inclusion of multiple headers, a header file contains two times, resulting in a duplicate definition
#ifndef _filename_h
#define _filename_h
#endif/* _filename_h * *

2. #ifdef
#else/****** Optional ******/
#endif

(1) Determining whether a part of a program participates in compiling or not
#ifdef __LIBRARY__
#endif/****__library__****/
Need this part of the code before #define __library__

3. #if defined
#elif defined
#else
#endif

The single operand operator defined can be used in #if or #elif pseudo directives.
#if defined _symbol_ is equivalent to #ifdef _SYMBOL_
#if!defined _symbol_ is equivalent to #ifndef _symbol

(1) The use of a number of options is very convenient
#if defined (VAX) | | Defined (hp300) | | Defined (Pyr)
#define Segment_size Page_size
#endif

(2) The expression of judgment
#ifdef MAJOR_NR
#if (MAJOR_NR = 1)
#elif (MAJOR_NR = 2)
#elif (MAJOR_NR = 3)
#elif
#elif

(3) Can comment out the large section of the code,/*...*/can not comment out itself contains * * * code, because/**/can not nest
#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.