The inclusion and layout of multiple ". c,. h" files in the lower computer,
I. background:
Since the contact with Single-Chip Microcomputer Programming, due to work needs, it is inevitable to often take over others' code, but often because the previous colleague's code is a little random, resulting in poor readability, sometimes you have to discard the original code and re-launch it. This increases the workload and wastes valuable development time. Therefore, we are increasingly aware that standard code is an important guarantee for improving work efficiency, not only for others, but also for future maintenance.
Therefore, this article chooses one aspect: make a record of the mutual inclusion and layout of multiple ". c,. h" files in the lower computer, and prompt yourself to serve as a reference for new friends.
Ii. Text:
Taking the power management project I am developing as an example, the project needs to communicate with a computer motherboard through I2C and manage the power supply of the motherboard as required. The previous colleague may have used only one "main. c "file to implement some functions, the consequence is that the delete and modify function is difficult to locate the corresponding position and difficult to clarify the relationship between related functions.
This project consists of several modules: the main cycle module, the IIC/UART module that communicates with the main board, the reading dial switch/button module, the storage flash module, and the battery management module. And power management module. Each module is a ". c,. h" file, and now focuses on Recording Multiple ". c. h" interrelated and mutually contained solutions.
For ease of management, I divided the project folder into the following folders: "PROJ" stores project files, "APP" stores "main. c "," aplication. c "files," Uart "stored by" BASEDRIVE. c "file," EXTIDRIVE "stores" Flash. c "," DOC "stores" Readme "files, corresponding ". h "files are stored in the corresponding folder.
When a header file is included, you can write an absolute path, for example, "# include XXX/EXTIDRIVE/'Flash. H'". However, this is too troublesome. You can also set the directory of the header file to the compilation software,
A) IAR setting method:
Right-click the project file name and choose "Option" --> "C/C ++ complier" --> "Proprocessor" --> "Additonal Include directories ".
B) how to set KEIL:
"Project" --> "Option for Target XXX" --> "C/C ++" --> "Include Paths ".
In this way, you can directly write the relative path "# include 'Flash. H'". The header file can also be found by the compiler.
So many preparations have been made in the early stage, and now the question is: "How should I deal with multiple. c files that contain. H files "?
In each ". c "file declares variables and functions, and some variables/functions will be" other ". c ". h "file extern this variable/function, if there is a file to use these variables/functions, just include its header file. Macro definition can be defined directly in the header file.
Take this power management project as an example: in the "main. c "file, I will declare some variables that will basically be used or changed by many modules, such as the status flag of the motherboard, whether it is boot, shutdown or sleep, this status is changed by the dial/button, IIC/UART, and other modules. h "extern this variable/function, and then let all files contain this header file;" main. h "macro definition is generally used by all modules. For example, "Uart. c "in this file, there is a flag" uartdone_flag ", a function" myprintf (* string); the former does not have other file calls, I only in the "Uart. c "file declaration, but the function" myprintf (* string) "will be called by other files, so this function is not just in the" Uart. c. h. h.
Let's talk about why the extern keyword is not directly declared in the header file, but is required. If it is declared directly in the header file, two ". the c file contains this header file. during compilation, a duplicate definition error occurs, and Conditional compilation with "# ifndef" is ineffective. To avoid this error, I used the keyword of extern. As for why, we have to wait for time to go into the Compiler Principles.
The following figure compares the actual results:
Location: Shenzhen WZ
Recorded on: July 15, May 6, 2016