In C language applications, such as communications and embedded systems, a software project usually contains a lot of complex functions, to achieve this project is not a programmer alone can be competent, often need a team of effective cooperation, in addition, in a C-code-oriented complete project, It is often necessary to include code in other languages, such as a mix of C code and assembly code, C files and C + +. All this adds to the complexity of a software project, and the various code and files that are reasonably organized to improve the quality of the software are important.
The purpose of organizing code and files is to make the team work more effectively, so that software projects have good scalability, maintainability, portability, reduction, testability, prevent errors and improve the stability of the software. Usually, the software project adopts the hierarchical structure and the modular development method, for example, an embedded software project might have a drive layer, an operating system layer, a functional layer, an application layer, each layer using its underlying interface, and providing the calling interface for its upper layer, which is the unit that completes a function in each tier, For example, the drive layer of each device driver is a module, the application layer of each application is a module, the module uses the interface provided by the lower level and other modules provided by the interface, to complete the specific functions, for the upper and the other modules of the same layer to provide the calling interface.
The interface here refers to the way a functional module exposes, providing access to the specific functionality of other modules. According to the characteristics of C language, use the *.c file to implement the module's function, using the interface of the *.h file exposure unit, to declare in the *.h file the functions, data types, global variables, type definitions, macro definitions, and constant definitions that may be used by other modules in the external module. The external module simply contains the *.h file to use the corresponding function. Of course, modules can be subdivided into sub modules. Although the interfaces we say here are different from those defined in COM (Common component model), according to COM's discussion of interfaces, in order for the software to be modified, The modification of one module does not affect the modification of one module of the other module, so the modification of the *.h file cannot result in the other modules needing to be rewritten when the interface is first released.
According to the characteristics of C language, and learn some mature software project code, summed up the code file in C Project organization of the basic recommendations:
Use a hierarchical and modular software development model. Each module can only use the interface provided by the layer and the next layer of module.
The packages for each module exist in a separate folder. Typically, more than one file is implemented for a module, and the related files should be saved in a folder.
Conditional compilation macros for module cuts are saved in a separate file for easy software reduction.
Hardware-related code and operating system related code and pure C code relative to save, in order to facilitate the software porting.
Declaration and definition separate, using *.h file exposure module needs to provide external functions, macros, types, constants, global variables, as far as possible to make the module on the external transparency, users in the use of module functions do not need to understand the specific implementation, file once released, to modify must be very cautious, folder and file naming should be able to reflect the functionality of the module.
Both the official version and the test version use the consolidated file, and the macro controls whether or not the test output is generated.
necessary annotations are indispensable.
Ideally, an executable module provides a public interface, even if the interface is exposed with a *.h file, but sometimes a module needs to provide more than one interface, at which point a public interface is provided for each defined interface. In C, each C file is a module, the header file provides an interface to the user using the module, and the user can use the interface exposed in the header file as long as it contains the corresponding header file. The following rules are recommended for all header files:
1. header file can not have executable code, and can not have the definition of data, can only have macros, types (typedef,struct,union,menu), data and function declarations. For example, the following code can be included in the header file:
#define NAMESTRING “name”
typedef unsign long word;
menu{
flag1;
flag2;
};
typedef struct{
int x;
int y;
}
Piont;
extent Fun(void);
extent int a;
The definition of global variables and functions cannot appear in the *.h file. For example, the following code cannot be included in the header file:
int a;
void Fun1(void)
{
a++;
}