Embedded operating system kernel principle and development (header file adjustment)

Source: Internet
Author: User
Tags prototype definition

[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]

 

For a long time, I personally did not understand the functions of the file. Although I have not made any major mistakes in header files during normal development, I always feel that the opposite file is not very thorough. Therefore, taking advantage of this embedded development opportunity, we have carefully analyzed and summarized the contents of this part of the file. Next we will mainly analyze the opposite file from two aspects: What is the header file, and what should we pay attention to during the header file writing process?

 

(1) functions of header files

In fact, many programming languages do not have header files, such as C # and Java. Why? Because the data structures and function operations of these languages are bundled together. The C language is different. It separates the header file from the implementation file. What are the main content of the header file, that is, the nested header file, macro definition, data type, function prototype definition, static function, and so on.

 

(2) Compiling of header files

 

A) the header file should be concise

Many source files often like to add many header files when writing, whether required or not. However, we need to know that header files mainly define data types and function types. In essence, header files seldom create substantive code, whether it is the content of the data segment or the content of the Code segment. The concise header file not only helps to quickly troubleshoot compilation, but also improves Compilation speed. Experienced users know that the compilation errors of source files are easier to solve, and the compilation errors of header files are often very complicated. Therefore, we must ensure that the header file is concise under all possible conditions.

 

B) Attention to mutual exclusion in header files

Note that header files are mutually exclusive and we need to develop good programming habits during development. Whether it is to create a header file, the first thing to do is to add a compilation macro. It seems that this is a very inconspicuous action, but it can often help you reduce unnecessary troubles.

#ifndef _DATA_H#define _DATA_H#endif

C) do not define global variables in the header file. If it is an external reference, you must add the extern

extern int g_Data;  

D) do not implement functions in the header file. To implement the functions, you must also add static

static int add(int a, int b){return a + b;}

E) if another header file needs to be embedded in the header file, it is only used to reference the data structure of another header file.

 

F) if the data type referenced in the header file is not described, you only need to ensure that the data type definition exists in other header files when being referenced by the source file.

 

G) when the source file references the header file, pay attention to the sequence of the header file. Sometimes the order changes, and compilation may fail. The reason is that the data type defined in the header file cannot be found.

 

H) Some projects do not bind the header file with the source file. to modify the header file, you must delete the project and re-compile it.

 

I) the header file exists only for the source file. Do not write the header file if necessary. To write data, the impact scope should be minimized.

 

J) If the header file defines the data structure, you need to embed the reference header file. If it is just a pointer, just declare it. For example

struct _Data;typedef struct _Data Data;

 

K) if it is possible to sort out all the header files and add them one by one, we will know what we need and what is not

 

L) for some macros, if you are not sure where the file is defined, you can define them again in the source file, so that the compiler will precisely prompt us that the macro was defined there.

Okay, that's almost all.

 

 

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.