Macro definitions and the conflicts they bring

Source: Internet
Author: User

In C language programming, for a relatively large project, the use of the library will be more, if the design of a slight negligence, it may create a macro-defined conflict. An example of a project is that the two header file defines "isspace (CH)", and the two macro definitions are inconsistent, causing the entire project to fail to compile.

The solution to this problem is to undefine the macro by undefine the conflicting macros immediately after the include statement. For example, the library liba.h and header file app_pub.h have different definitions of isspace (CH), and if you use these two files, you will create a macro-defined conflict, and you can have one undefine (a prerequisite is that a macro that is not a two-header file definition will be used): # Include "App_pub.h"
#ifdef isspace
#undef isspace
#endif

This approach is a frustrating way to resolve a macro-definition conflict. In fact, the scope of the macro definition has long been criticized, especially in the header file, was included several times, inadvertently covered, and then reused, perhaps at this time is not the original meaning, and we do not know when used.

For this reason, there is a simple principle to reduce the conflict of macro definitions, that is, try not to place the macro definition in the header file unless you have a reason not to put it in the header file. In addition, there is a strategy that if a file/library function is not used elsewhere, then do not put it in the header file. The header file should be an interface, not a hodgepodge of malatang.

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.