C header file organization

Source: Internet
Author: User

1. Write global variables separately (for reference ).
Generally, we are used to placing different functional modules in a header file and a C file.

For example, write some mathematical functions:
[Cpp]
// Mymath. h
# Ifndef _ mymath_H
# Define _ mymath_H
Extern int Global_A; // declare the necessary global variables
......
Extern void fun (); // declare necessary external functions
.....
# Endif

// Mymath. h
# Ifndef _ mymath_H
# Define _ mymath_H
Extern int Global_A; // declare the necessary global variables
......
Extern void fun (); // declare necessary external functions
.....
# Endif
[Cpp]
// Mymath. c
# Include "mymath. h"
# Include <some C library files to be used>
...
Int Global_A; // defines the necessary global variables and functions.
Void fun ();
...
Int a, B, c; // defines some internal global variables
Void somefun ();
 
// Function implementation body
Void fun ()
{
...
}
Void somefun ()
{
...
}

// Mymath. c
# Include "mymath. h"
# Include <some C library files to be used>
...
Int Global_A; // defines the necessary global variables and functions.
Void fun ();
...
Int a, B, c; // defines some internal global variables
Void somefun ();

// Function implementation body
Void fun ()
{
...
}
Void somefun ()
{
...
} Which C file must contain only the header file mymath. h.

However, I think the above method is good, but the method for defining global variables above causes inconvenience in a large project. It is best to transfer data between a module and other modules through proprietary functions, instead of directly passing through data units (this is the idea of VC ++), it is not recommended to declare global variables in the header file of the module; it is best to define global variables in a fixed file, so you can use the following method:

Define a Globel_Var.C file to put global variables, and declare global variables in the corresponding Globel_Var.H file.
For example:
----------------------------------
[Cpp]
// Globel_Var.c
/******* Define the global variables used in this project *******/
Int speed;
Int torque;
...
...
...

// Globel_Var.c
/******* Define the global variables used in this project *******/
Int speed;
Int torque;
...
...
... ----------------------------------
[Cpp]
// Globel_Var.H
/******* Declare the global variables used in this project *******/
Extern int speed;
Extern int torque;
...
...

// Globel_Var.H
/******* Declare the global variables used in this project *******/
Extern int speed;
Extern int torque;
...
... ----------------------------------
In this way, which file uses these two variables can write the file inclusion command at the beginning of the file; for example, aa. the speed and toque variables are used in the C file. the H file contains the Globel_Var.H file.
----------------------------------
[Cpp]
// Aa. h file
# Include "Globel_Var.H"
...
Extern void fun (); // declare necessary interface functions
...

// Aa. h file
# Include "Globel_Var.H"
...
Extern void fun (); // declare necessary interface functions
... [Cpp]
// Aa. c file
# Include "aa. H" // each program file contains its own header with the same name
Int a, B, c; // defines some local variables used in this file.
Void somefun ();
 
// Function implementation body
Void fun ()
{
Int d, e, f; // defines some local variables used in this function.
...
}
Void somefun ()
{
...
}
...

// Aa. c file
# Include "aa. H" // each program file contains its own header with the same name
Int a, B, c; // defines some local variables used in this file.
Void somefun ();

// Function implementation body
Void fun ()
{
Int d, e, f; // defines some local variables used in this function.
...
}
Void somefun ()
{
...
}
... ----------------------------------
The void fun () function or variable file in the aa. c file can be written in this way in the bb. c file.
[Cpp]
// Bb. h file
# Include "aa. H"
...
Extern int fun_1 (void); // declare the interface function of this file
...

// Bb. h file
# Include "aa. H"
...
Extern int fun_1 (void); // declare the interface function of this file
... [Cpp]
// Bb. c file
# Include "bb. H"
...
Int fun_1 (void)
{
...
Fun (); // call the fun () function in the aa. C file
...
}

// Bb. c file
# Include "bb. H"
...
Int fun_1 (void)
{
...
Fun (); // call the fun () function in the aa. C file
...
}----------------------------------
In the main function, you can write as follows: The main file does not have its own header file.
[Cpp]
// Main. c file
# Include <system library files>
# Include "Globle_Var.H"
# Include "aa. H"
# Include "bb. H"
...
 
Char fun_2 (); // declare the function defined in the main file
Int I, j; // defines some local variables used in this module.
Char k;
...
Void main ()
{
...
Fun ();
...
I = fun_1 ();
...
K = fun_2 ();
...
}
 
Char fun_2 ()
{
...
}

// Main. c file
# Include <system library files>
# Include "Globle_Var.H"
# Include "aa. H"
# Include "bb. H"
...

Char fun_2 (); // declare the function defined in the main file
Int I, j; // defines some local variables used in this module.
Char k;
...
Void main ()
{
...
Fun ();
...
I = fun_1 ();
...
K = fun_2 ();
...
}

Char fun_2 ()
{
...
}----------------------------------
In this way, no error is reported and global variables can be easily used.

 


2. If static or const is added before a global variable (a variable of the static type is implicitly added)
As follows:
[Cpp]
// Xxxx. h
...
Const double PI = 3.1415926;
Static void * NULL = 0;
...
//

// Xxxx. h
...
Const double PI = 3.1415926;
Static void * NULL = 0;
...
// This header file can be contained in multiple compilation units.


Iii. refer to the following basic rules for compiling header files
Ideally, an executable module provides a public interface, that is, *. h file exposes the interface. However, sometimes a module must provide more than one interface. In this case, a public interface must be provided for each defined interface. In the C language, each C file is a module. The header file provides interfaces for users using this module. Users can use the exposed interfaces in this header file as long as they contain the corresponding header file. We recommend that you refer to the following rules for all header files:

 

1. the header file cannot contain executable code or data definitions. It can only contain macro, type (typedef, struct, union, menu), and data and function declarations.

For example, the following code can be included in the header file:

[Cpp]
# Define NAMESTRING "name"
Typedef unsigned long word;
Menu {flag1, flag2 };
 
Typedef struct
{
Int x;
Int y;
} Piont;
 
Extent Fun (void );
Extent int;

# Define NAMESTRING "name"
Typedef unsigned long word;
Menu {flag1, flag2 };

Typedef struct
{
Int x;
Int y;
} Piont;

Extent Fun (void );
Extent int a; the global variables and function definitions cannot appear in the *. h file. For example, the following code cannot be included in the header file:
[Cpp]
Int;
Void Fun1 (void)
{
A ++;
}

Int;
Void Fun1 (void)
{
A ++;
}


2. the header file cannot contain local data (the data or functions used by the module are not used by other modules ).

This is equivalent to the private member in the object-oriented programming, that is, only the functions used by the module, Data, do not use extern to declare in the header file, only the macro and constant used by the module, the type should not be declared in the header file. It should be in your own *. c file.

3. Include some declarations to be used. Declare external data, functions, macros, and types in the header file.

4. Prevent Repeated inclusion. Use the following macro to prevent a header file from being repeatedly contained.

[Cpp]
# Ifndef MY_INCLUDE_H
# Define MY_INCLUDE_H
<Header file content>
# Endif

# Ifndef MY_INCLUDE_H
# Define MY_INCLUDE_H
<Header file content>
# Endif


4. Refer to more rules for header file writing (only 1, 2, 3, and 4 can be understood for the time being)
Some header files provide the Calling Interface for users. This header file declares the functions and data that the module needs to use for other modules. In view of the software quality considerations, the processing should refer to the above rules, more rules are required for header files used to expose interfaces:

1. One module and one interface, which cannot be used by several modules.

2. The file name is the same as the c file of the implementation module. Abc. c -- abc. h

3. Try not to use extern to declare some shared data. This approach is insecure. Users of other external modules may not fully understand the meaning of these variables. It is best to provide functions to access these variables.

4. Avoid including other header files unless they exist independently. This means that, in the header file used as an interface, try not to include the exposure of other modules *. the header file in file C, but it can contain header files that are not used to expose interfaces.

5. Do not include header files that are only used in executable files. These header files should be included in *. c files. This is the same as above. In order to improve the independence and transparency of interfaces

6. The interface file should have user-oriented comments. Describe exposed content from the application perspective.

7. Do not modify the interface file after it is released. Make sure that the modification does not affect the user program.

 

5. Use one interface file for multiple code files (cannot be fully understood at the moment)
This header file is used when a module thinks that a file is too large. For this case, after referring to the above suggestions, you should also refer to the following suggestions.

1. A module composed of multiple code files has only one interface file. These files are a module.

2. Use the file name under the module <system name> <Module name>

3. Do not abuse such files.

4. sometimes there will be a few *. c file is used to share data *. h file, which is characteristic of *. the c file defines global variables, while the other *. c files are used in different scenarios.

5. If a module has several sub-modules, you can use a *. h file to expose the interface. In this file, use # include to include the interface file of each sub-module.

 

There is also a header file, descriptive header file, which does not need to have a corresponding code file. Most of these files contain a large number of macro definitions without exposing data variables and functions. These files provide the following suggestions:

1. Include some conceptual things.

2. naming method, defined function. h

3. does not contain any other header files.

4. Do not define any types.

5. does not contain any data or function declaration.


This section describes some suggestions for the C header file. The following describes the C code file *. some suggestions for file c ,*. the c file is the content of the assembly code and machine code generated in the c language. Pay attention to the following suggestions:


1. naming method module name. c

2. Use static to modify local data and functions.

3. Do not use externa. This is used in *. h and can be included.

4. When to define internal objects, make sure they are independent from other execution files.

5. This file must contain the corresponding function.


Conclusion: I have introduced some suggestions for C file organizations to improve the quality of C language projects. In future C project organizations, I will learn the object-oriented and COM ideas, add these ideas to the C program to write higher quality code. The above suggestions should be used flexibly in specific projects. In addition, there are often some assembly code files in the C project. These files also need to expose the data and functions in the *. h header file so that other *. c files can be used.

 

Related Article

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.