A classic example.
The classic example of using preprocessing and macro definitions is to add a header file to prevent the header file from being compiled two times. Imagine this situation, there is a file headerfile.h it is included in the Headerfile1.h, and also included in the Headerfile2.h, there is now a CPP file, Implement.cpp contains Headerfile1.h and headerfile2.h:
#include “headerfile1.h”
#include “headerfile2.h”
Suppose a global variable iglobal is defined in the headerfile.h.
int iglobal;
At compile time the compiler compiles the Headerfile two times, also discovers Iglobal is defined two times, then will occur the variable redefinition compilation error.
The traditional solution is to use #ifdef and #endif to avoid repetitive compilation of header files, and in the example above, just add a few lines:
#ifndef smartnose_2002_6_21_headerfile_h
#define smartnose_2002_6_21_headerfile_h
int iglobal;
#endif
With careful consideration of the macro definition above, you will find that when the compiler compiles once headerfile.h, SMARTNOSE_2002_6_21_HEADERFILE_H this macro is defined, and subsequent compilations of headerfile.h will skip int Iglobal this line. Of course SMARTNOSE_2002_6_21_HEADERFILE_H This macro can be defined arbitrarily, but the macro itself cannot be duplicated with the macros defined in other files, so MFC always uses a very long, randomly generated macro in the auto-generated file, but I don't think it's necessary. , I suggest adding some meaningful information to this macro, such as author, filename, file creation time, and so on, because we sometimes forget to include this information in our comments.
We will not see these macro definitions again in Vc.net, because a preprocessing directive is commonly used here:
#pragma once
Adding this instruction at the beginning of the header file ensures that the header file is compiled once, and this instruction is actually available in the VC6, but there is not much use of it, given the compatibility.
Source Code version control
precompiling directives and macro definitions can also help us when we develop multiple versions for many platforms. Let's say we've developed a suite of software for Windows and Linux, and because of the differences between the two systems, we have to program the version of the source code. For example, we can use the malloc function of standard C on Linux, but we want to use the HeapAlloc API on Windows. The following code illustrates 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, then heapalloc this statement will work. This allows us to implement different versions of the code for different platforms in the same file, while maintaining the good structure of the program. In many cases, we can also use a different algorithm for a method, and then use a macro definition to select one of them to compile for different situations. This is the most used in MFC applications. The most obvious is that the files often exist
#ifdef _DEBUG
…………………….some code………..
#endif
Such code, which works in the debug version of the application (debug).