Use C language to implement modularization, and C language to implement Modularization

Source: Internet
Author: User

Use C language to implement modularization, and C language to implement Modularization

In addition to C and C ++ programming languages, java, php, and jsp are currently very popular development languages. It is hard to imagine how terrible a standardized module management mechanism is. However, this is often determined by the design philosophy of the C language itself: leave as many possibilities as possible to developers. Then, you can customize what you need based on your actual needs.

For a slightly smaller system (that is, the system we usually access during development), we generally consider selecting a lightweight source code-level solution. If it is not a small system, but a giant system (such as an operating system like Windows), we generally consider using a binary modular solution. The module provides metadata by itself, or we can also use a unified management solution (such as registry and other tools ).

However, we often have to consider the initialization process and dependency of the module.

1. Dependency

In fact, the dependency can be solved by the loader or the linker. Especially when using the C language, simple dynamic or static libraries won't cause us a lot of trouble, so you can rest assured.

However, the C ++ programming language is not like this. In C ++, there are some features (such as the construction of static members of the template class) it is necessary to make some enhancements to the linker that was only used in C language in the early stage. Even some C ++ libraries carefully written may encounter unexpected bugs (program errors ). If you want to query these program errors, you must have a deep understanding of the links, loading processes, and compilation before you can find them. Here, you should pay attention to this point, that is, to write a small paragraph, not to say that you should use the C ++ programming language to develop programs.

2. module initialization process

We usually need to focus on management, which is the initialization process of the module.

For a library that is packaged together (for example, msvcrt or glibc), there is usually an initialization entry during loading and end code During uninstallation. But what I want to talk about here is not this issue, but the dependency between smaller modules internally split. The problem is: who first initializes and who then initializes?

A friend who uses the C ++ programming language should find that all of his language-level solutions often use a single module. The linker determines the order in which the initialization code is generated. However, this often leads to program errors due to different actual build order and dependency relationships (Note: I have seen several books on C ++ and will wait for verification. In fact, the small editor does not write C ++ for a long time and there are no actual Error Examples); or it is to use the inert initialization solution. However, you should also know that this inert Initialization is not a universal key, and there are some additional overhead. Therefore, you must pay special attention to this in a multi-threaded environment.

In the initial design of C language production, xiaobian often adopts a simple and convenient method. This simple method is to use the encoding specification to specify that each module must have an initialization function with a more standardized name. For example, the initialization entry of the foo module is: int foo_init ().

If you use a specific module, you must call the module initialization function. This is a rule, so everyone must follow it.

You must note that the initialization function is called indirectly rather than directly. To avoid repeated initialization of modules. It is similar to this: mod_using (foo_init );.

The main function of mod_using is to call the initialization function and ensure that the call is not repeated. In addition, it can also check the circular dependency for us.

Here, we also agree whether Initialization is successful depends on a returned value. (In general, in our system, if the return value is 1, it indicates failure. On the contrary, if the return value is 0, it indicates correctness) then we define a macro to make this use. :

Note: For individuals, small editors are particularly opposed to macro abuse. Therefore, you should try to avoid using macros. Here, the small Editor uses macros, which are determined after careful consideration. In order to determine whether or not I have missed module initialization, the editor hopes to have a code scanner to help me determine whether or not I have actually missed module initialization. (A module may be used by the editor, but forget to initialize it ). Macros can help the code scanning analyzer implement it more easily. In addition, using macros is more like a slight and necessary extension to programming languages.

In this case, at the end of the implementation code of the module in the mini-Editor System, there is often a function named init, which simply calls USING to reference other modules. For example, the following example shows the specific program code:

Because most of the requirements do not require module uninstallation, we will not discuss this point in this tutorial. If you want to learn more, you can read related books.

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.