One of the advantages of preprocessing commands and macro definitions in VC

Source: Internet
Author: User
One of the advantages of preprocessing commands and macro definitions in VC

Author: Liu Wei Source: blog responsibility Editor: Ark

 

People who are new to MFC programming are often intimidated by various macro definitions and preprocessing commands generated by the MFC wizard. However, preprocessing and macro definition are a powerful tool in C language. They can be used for simpleSource codeControl, version control, warning, or complete some special functions.

A classic example

The most classic example of using preprocessing and macro definition is to add a header file to avoid the header file being compiled twice. In this case, there is a file headerfile. h. It is included in headerfile1.h and also included in headerfile2.h. Now there is a CPP file, implement. CPP contains headerfile1.h and headerfile2.h:

# Include "headerfile1.h"
# Include "headerfile2.h"

Assume that headerfile. h defines a global variable iglobal.

Int iglobal;

During compilation, the compiler compiles the headerfile twice and finds that iglobal is defined twice. In this case, a variable redefinition compilation error occurs.

The traditional solution is to use # ifdef and # endif to avoid repeated compilation of header files. In the above example, you only need to add the following lines:

# Ifndef smartnose_2002_6_21_headerfile_h
# Define smartnose_2002_6_21_headerfile_h

Int iglobal;

# Endif

After careful consideration of the macro definition above, we will find that when the compiler compiles the headerfile once. H and later, the macro smartnose_2002_6_21_headerfile_h is defined, and headerfile will be used later. the Int iglobal line will be skipped during compilation of H. Of course, the macro smartnose_2002_6_21_headerfile_h can be defined at will, but the macro itself cannot be the same as the macro defined in other files, therefore, MFC always uses a random macro with a very long length in an automatically generated file, but I don't think this is necessary. I suggest you add some meaningful information to this macro, for example, the author, the file name, the file creation time, and so on, because we sometimes forget to add this information to the comment.

In VC. net, we will not see these macro definitions any more, because a preprocessing command is generally used here:

# Pragma once

You only need to add this command at the beginning of the header file to ensure that the header file is compiled once. This command is actually available in vc6, but it is not widely used in consideration of compatibility.

SourceCodeVersion Control

When we develop multiple versions for many platforms, pre-compilation commands and macro definitions can also help us. Assume that we have developed a set of software for Windows and Linux.ProgramControl the source code version. For example, for memory allocation, we can use the standard C malloc function on Linux, but we want to use the heapalloc API on Windows. The following code demonstrates this situation:

Main ()
{
....................
# Ifdef _ windows_platform
Heapalloc (5 );
# Else
Malloc (5 );
# Endif
....................
}

When we compile this program on the Windows platform, we only need to define the _ windows_platform macro, so the heapalloc statement can work. In this way, we can implement different versions of code for different platforms in the same file, while maintaining a good program structure. In many cases, we can also use differentAlgorithmAnd then use the macro definition to select one of them for compilation based on different situations. This is the most widely used in MFC applications. The most obvious is the frequent

# Ifdef _ debug

......................... Some code ...........

# Endif

Such code plays a role in debugging of the application.

# Pragma command

Among all the pre-processing commands, the # pragma command may be the most complex. It is used to set the compiler status or to instruct the compiler to complete some specific actions. The format is generally

# Pragma para

Here, para is a parameter. Below are some common parameters.

Message parameter. The message parameter is one of my favorite parameters. It can output relevant information in the compilation information output window, which is very important for controlling source code information. The usage is as follows:

# Pragma message ("message text ")

When the compiler encounters this instruction, it prints the message text in the compilation output window.

When we define many Macros in the program to control the source code version, we may forget whether these macros are correctly set, in this case, we can use this command to check it during compilation. Suppose we want to determine whether we have defined the _ x86 macro in the source code. The following method can be used:

# Ifdef _ x86

# Pragma message ("_ x86 macro activated !")

# Endif

After we define the _ x86 macro, the application will display "_ x86 macro activated!" in the compilation output window during compilation !". We won't be scratching our heads because we don't remember some specific macros we defined.

The other Pragma parameter that is used more frequently is code_seg. Format:

# Pragma code_seg (["section-name" [, "section-class"])

It can set the code segment where function code is stored in the program. It is used when we develop the driver.

The last commonly used command is the # pragma once Command mentioned above.

Pre-defined macros of VC

In VC, a type of macros are not defined by the # define statement, but can be recognized by the compiler itself. These macros are also very useful. Let's take a look at the first one, which is the most frequently used in MFC: __file __.

When the compiler encounters this macro, it expands it into the file name of the currently compiled file. Now, we can immediately think of what we can do with it. When an application error occurs, we can report the file in which the program code of this error occurs, for example, in the file test. CPP has the following code:

Try
{
Char * P = new (char [10]);
}
Catch (cexception * E)
{
Trace ("There is an error in file: % s" N ",__ file __);
}

When the program runs, if an error occurs in memory allocation, there is an error in file: test. CPP, of course, we can also display this error message elsewhere.

If we can record the row in which the error occurs, we are lucky that, like the _ file _ macro definition, there is also a macro that records the number of rows of the current Code, this macro is _ line __. Using the two macros above, we can write an Assert statement similar to that provided by VC. The method is as follows:

# Define myassert (x )"
If (! (X ))"
MessageBox (_ file __,__ line __, null, mb_ OK );

In an application, we can use it like an Assert statement. When an error occurs, a dialog box is displayed. Its title and content tell us the file and code line number of the error, this facilitates debugging. This is very useful for projects that cannot use the assert statement.

In addition to these two macros, the _ time __, _ date __, and _ timestamp _ macro that records the Compilation Time.

With these predefined macros, we can generate almost the same complete source code information report as that generated by VC.

Conclusion

Open the source code of MFC and Linux, and macro definition occupies almost half of the sky. Message ing, queue operations, platform porting, version management, and even kernel module disassembly and installation are all completed with macro definition. It is no exaggeration to say that some files can only see macro definitions. Therefore, learning macro definition and skillful use of macro definition are critical to learning C language and even VC.

 

From: http://dev.yesky.com/201/2559701.shtml

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.