Modular Programming Summary-Ultimate Chapter __ Programming

Source: Internet
Author: User
Tags data structures
Always thought that I completely understand the modular programming, but occasionally see some conditional compilation feeling inexplicable, today together to make a summary.
1, a single function module to establish source files. C, and header files. h, note that the. c suffix must be lowercase for C languages, and some compilers (such as GCC) would consider uppercase C suffixes to be C + + files.
2, for the function module internal use of global variables, function do not put in the header file, you should use the static declaration in the front section of the source file module, so as to prevent multiple file links (no error at compile time) the name of the function, the name of the variable names conflict error (even if the case is different than int a and int) A will also).
3, for the header file plus #ifndef xxx #define XXX What is to prevent in the same source file a header file is repeatedly referenced (also known as included), otherwise the file repeat contains errors, rather than function duplicate definition error.
4, as far as possible in the header file to define variables, the definition of variables should be placed in the source file, the header file plus extern declaration can, although the C language function declaration header file default plus extern, but in order to facilitate understanding plus extern.
5. What is appropriate in the header file: macro definition, type redefinition, structure weight definition and the function interface, global variable interface that the source file module needs to be accessed externally.
6. In principle, the shared variables between multiple files are not defined in a header file, but rather the 5th rule, which is declared in the header file in the source file, but there is one method in the UCOSII real-time operating system that is very good, It is ingenious to define global data structures in the UCOSII.h header file to share these data structures with other kernel files of the system. The practice is: at the beginning of the header file
#ifdef os_globals #define Os_ext #else #define OS_EXT extern #endif
Os_ext OS_TCB *ostcbcur when defining variables;
And in the mailbox, Semaphore source file at the beginning have such a piece of code
#ifndef os_master_file #include #endif
Except in the core.c file.
#ifndef os_master_file #define Os_globals #include #endif
Such a dozen has the data structure in the header file compiled into a defined form when compiling the core.c file, and other source files are compiled into the form of declaration, which can be used for reference later.
7, the head file for a long while, each source file module One add header file method is unreasonable, should define a header file Config.h, and then put all the header files are placed in this
In the file, do not include the duplication of the processing of the main, but all the headers are included in the file. For ease of management, you can create a new folder in the engineering directory to store headers specifically, so that the header file is written #include. \user\task.h ", said that first exit the current level of the file directory, and then enter the USER folder and then find Task.h header files, all the source files, header files are added #include" Config.h "on the line, very convenient.

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.