Using C language for modularity

Source: Internet
Author: User

In addition to the C language and the C + + programming language, in other now very popular development languages, such as: java,php,jsp and so on. It's hard to imagine how horrible it is to lack a standardized module-management mechanism. But this is often determined by the design philosophy of the C language itself: leave as many possibilities as possible to the developer. Then according to the actual situation of the system, according to the practical needs of people to customize some of the things they need.

For some systems that are slightly smaller (that is, the system that we usually come into contact with during development), we will generally consider choosing a lightweight source-level solution. If it is not a small system, instead of some giant systems (for example, an operating system like Windows), then generally we will consider using a binary-level modular scheme. The module itself provides meta-information, or we can use a unified management scheme (e.g., a registry tool).

But the first thing we often have to consider is the initialization process of the module and the two parts of the dependency relationship.

1. Dependency relationship

In fact, the dependency of this part can be placed by the loader or the linker to solve. Especially when we use C language, simple dynamic library or static library, all are not very likely to cause great trouble for us, so we can be very relieved.

However, the C + + programming language is not the case, there are some features in C + + (such as the construction of static members of template class) must be used in the early days only for the C language linker to do some enhancements. Even some of the C + + libraries that are carefully written are likely to have some unexpected bugs (i.e., program errors). These program errors often if you want to query out, often need to link, loading process, compile a very deep understanding, before you can find out. Here you have to pay attention to a little bit, that is, small write this paragraph, do not say that it is against the use of the C + + programming language to develop the program.

2, the initialization of the module process

Then we often need to focus on the management, is the module initialization process this part.

For some of the libraries packaged together (say, MSVCRT or glibc, etc.), there will always be an initialization entry at the time of loading, and there will be an end code when unloading. But the little part here is not about this problem, but the interdependence of smaller modules that are split internally. The problem is: who is initialized first, who initializes it?

A friend who uses the C + + programming language usually finds that his language-level solution is often used in a single-piece module. Or it is up to the linker to decide in what order to generate the initialization code. However, this is often due to the fact that the actual construction sequence and dependencies, resulting in a program error occurred (note: Small in several books on C + + have been seen, to be verified. In fact, the small part of their own in a long time do not write C + + also has no actual error examples appear), or the use of lazy initialization scheme. But you should also know that this lazy initialization is not a master key, there are some additional overhead. So, if you are in a multithreaded environment, you must pay particular attention to this point.

Small series in the use of C language in the initial design, often adopt a simple and convenient method. This simple method is to use coded specifications to make provisions, each module must have an initialization function, to have a more canonical name. For example, the initialization entry for the Foo module is called: int foo_init ().

If you say that you are using a specific module, then you must call the module initialization function. This is a rule to yo, so we must follow.

It is important to note that the initialization function is called indirectly, not directly. This is done in order to avoid the problem of repetitive initialization of modules. Just like this: mod_using (foo_init);.

The main function of mod_using is that it is responsible for invoking the initialization function and is guaranteed to not repeat the call, and it can also check the cyclic dependencies for us.

In this case, we also agreed that the success of the initialization is due to its return value. (in general, in our system, if the return value is 1, it represents a failure, and if it is 0, then it represents the correct one) and then we define a macro to make this use. :

Note: For the individual, the small part is particularly against the abuse of macros. So everyone should avoid using the macro as much as possible. The use of a macro here, are carefully considered before the decision to use. In order to be able to determine whether or not you really missed the module initialization, but I hope you can have a code scanner to help me to judge (perhaps the small part will use a module, but forgot to initialize it). Macros can help the code-scanning parser make it easier to implement. In addition, the use of macros is more like a minor and necessary extension to the programming language.

In this case, the implementation code of the module module in the system of small series Finally, there will always be a function called init, in which simply call the using to refer to other modules. For example, the following example, the specific program code:

As most of the requirements are not required to unload the module, so in this tutorial small-scale is no longer to demonstrate this point. If you want to know, you can check the relevant books.

Using C language for modularity

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.