Chapter 19 Pre-Administration

xiaoxiao2021-03-06  20

Chapter 19 Pre-Administration

The preprocessor is a text processor for processing source file text, which is an integral part of the translation initiation phase.

The preprocessor is not analyzed on the syntax to analyze the source text, but it is separated from the source text in the language symbol for the purpose of the positioning macro. Although the compiler is typically initiated at the initial compilation, the pre-processor can also process text without compilation.

Microsoft Special Office

After prerequisites with / e or / EP compiler option, you can get a list of source code. In most cases, start the prefrocessor and output result text to the output device. Both options are console instructions. The difference between the two options is that / e includes #LINE instructions, / EP does not have these instructions.

Microsoft End

Special term

In this book, the term "parameters" refers to an entity that is transmitted to a function. Sometimes it is modified with "actual" or "formal", which is used to indicate the parameter expression during the function call and the parameter instructions in the function definition. Noun "variable" refers to a simple C-type data object, noun "object" refers to C objects and variables; it is a meaningful noun.

Translational stage

The C and C programs consist of one or more source files, which contain some text of the program, a source file that does not contain the code section and its containment file (files included with the #Indude preprocessor instruction), Conditional compilation instructions (such as #IF) call, called a "conversion unit".

Source files can be translated multiple times, translated past files in fact is normal. The translated translation unit can be saved in a separate object file or object code base, which can be connected to form an executable file or a dynamic link library (DLL).

The conversion unit can communicate in the following form:

* Call a function with an external connection.

* Call the class member function with external connections.

* Directly change objects with external connections.

* Direct changes to the file.

* Internal proxy communication (only Microsoft Windows-based applications). The following is the various stages of the compiler translation file:

Character mapping

The characters in the source file are mapped to the form of internal source code. This phase of the three-letter sequence is converted into a single-character internal representation.

Row stitching

At this stage, all of the source files are ending with the backslash (/) and followed by a changing line, and will be connected to the next row to generate logical rows by physical row. All non-empty source files ended in a newline in front without a backslash.

Language symbolization

This phase source file is divided into preprocessing language symbols and blank characters. Each annotation in the source file is replaced with a blank character. The wrap is retained.

Pretreatment

This phase performs a pre-processing instruction and extends the macro to the source file, and # include statement calls the translation process that includes the three translation steps including the text started.

Character set map

All source character set members and escape sequences will be converted to an equivalent form in which Microsoft C and C , the source character set and the execution character set are ASCII code.

String merge

All adjacent strings and wide character text will be merged. For example: "String" "concatenation" merges to "stringconcatenation".

translation

All language symbols will be analyzed according to the syntax and semantic rules; these language symbols are converted to target code.

link

All external references are decomposed in this stage to generate an executable or a dynamic link library.

When the compiler encounters a syntax error during the translation process, a warning or error message will be issued.

The linker decomposes all external references and connects one or more switching units and standard libraries to generate an executable program or dynamic link library (DLL).

Preprocessor instruction

Pre-regulator instructions such as #define and #ifdef are typically used in different running environments, allowing the source program to be easier to change and compile. The instructions in the source file indicate that the preprocessor performs a unique behavior. For example, the preprocessor can replace the language symbols in the text, insert other file content into the source file, or remove a part of the text to suppress a part of a part of the file. The preprocessor row is identified and executed before the macro extension. However, if the macro extension looks like a preprocessor instruction, the command will not be recognized by the pre-processor.

In addition to the escape sequence, the preparation instrument statement uses the same character set as the source file statement. The character set and the character set of the executable program in the preparation genre statement are the same. The preprocessor can also identify the negative character value. The preprocessor can identify the following instructions:

#define #Error #import #undef

#elif #if #include

#lse #ifdef #Line

#ndif #ifndef #pragma

The number symbol (#) is the first non-empty character in the row of the pre-processor instruction. Blank characters can appear between the first letters of the digital symbols and instructions. Some instructions contain parameters and values. Any text after the instruction (except for a parameter or value as part of the instruction) must be placed between the single line annotation detail (//) or between the comment distinceance (/ * * /). The preprocessor instruction can appear anywhere in the source file, but they are only used for the remainder of the source file.

#define instruction

You can use the #define instruction to give a meaningful name to the constant in the program, and the two forms of their syntax are as follows:

grammar

#define identifier language symbol string

Opt # define identifier [(identifier OPT, ..., identifier OPT)] Language symbol string OPT

#DEfine instructions Replace all the appearances of an identifier in the source file with language symbol strings, the identifier is replaced only when it forms a language symbol (see "Microsoft Visual C 6.0 language" of the Microsoft Visual C 6.0 Reference. Reference Manual "Language Symbol" in Chapter 1 "Speech Provisions" in Volumes. For example, if the identifier is now in one comment, a string or as part of a long identifier is not replaced.

A #define instruction without a language symbol string will remove the appearance of each identifier in the source file. The identifier retains its definition and can be tested with #defined and #ifdef.

Language symbol strings consists of a series of language symbols such as keywords, constants, or complete statements. One or more blank characters can separate the language symbol strings and identifiers. Blank characters will not be considered part of the replaced text, and the blank after the last language symbol is not considered part of the replacement text.

The formal parameter name appears in the language symbol string to mark the actual value, each parameter name can occur multiple times in the language symbol string, and can appear in any order. The number of parameters when the call is called must match the number of parameters defined. The free use of parentheses ensures that complex actual parameters are correctly illustrated.

Use the second grammatical form to create a macro of similar functions. This form accepts an optional parameter table enclosed with parentheses. The identifier is referenced after the initial definition, and the actual parameter replaces the language symbol string parameter of the formal parameter, replaces each appearance of the identifier (identifier OPT, ..., identifier OPT).

The formal parameters in the table must be separated by commas. Each name in this table must be unique, and this parameter table must be included in the parentheses, between the identifier and the parentheses on the left cannot have spaces. For long-line long instructions that occupy multiple lines, the reverse slash (/) is placed before the charm. The range of form parameter names extends to the newline character of the end language symbol string.

When a macro is defined in the second grammatical form, the text instance after the parameter table constitutes a macro. In the source file, the actual parameters after an identifier instance must match the corresponding form parameters defined by the macro. Each language symbol string is not characterized (#), characterization (# @) or language symbol paste (##) operator, or thereafter without ## operator form parameters, it is correspondingly active. Replace. Any macro in the actual parameters will be expanded before the instruction replacement form parameter (these operators) will be described in this chapter.

Example of the following parameter macro illustrates the second form of #define syntax:

/ / Define the macro of the light mark

#define cursor (TOP, BOTTOM) ((TOP) << 8) | BOTTOM))

/ / Get a macro of a random integer in the specified range

#define getrandom (Min, Max) /

((() (((%) ((max) 1) - (min))) (min))

Passages with side effects may sometimes result in macros to produce undesirable results. A given form parameter may have multiple times in the language symbol string. If the form parameter is replaced by an expression of side effects, the expression and its side effects may be evaluated multiple times (see example in the "Language Symbol Paste Operator ##" later in this chapter).

#undef instruction allows the pre-processes of an identifier to fail. See the #undef instructions for more information. If a defined macro name appears in the language symbol string (even if another macro extension), it will not be expanded.

Refine a defined macro will generate an error message unless the second definition is exactly the same as the original definition.

Microsoft Special Office

Microsoft C / C allows a macro to redefine, but a warning message is generated, indicating that the new definition is the same as the original definition. ANSI C considers that the macro's redefine is wrong. For example, the following macros are the same, but will generate a warning message: #define test (F1, F2) (F1 * F2)

#define test (A1, A2) (A1 * A2

) End of Microsoft Special Office

This example is used to illustrate the #define directive:

#define width 80

#define length (Width 10)

The first description defines the identifier Width as a shaped constant 80, and defines the length of the length 10 with Width and the shaping constant 10. Every appearance of Length is replaced with (Width 10), then, each appearance of Width 10 is replaced with an expression (80 10). Width 10 parentheses is very important because they determine the explanation of the following statement.

VAR = Length * 20;

This statement becomes:

VAR = (80 10) * 20;

The value is 1800, if there is no parentheses, the result is:

VAR = 80 10 * 20 Its value is 280.

Microsoft Special Office

Define macros and constants with / d compiler options, and use a #define pre-processed instruction effect. The macro that can be defined with / D option can reach 30.

Microsoft End

#Error instruction

Compiling error messages can be generated using an Error instruction.

grammar

#error language symbol string

Error messages include language symbol string parameters, and belong to macro extensions. These instructions are very useful for violations of violations when the programs of the detecting procedure and the pretreatment are very useful. The following example illustrates the error handling of pre-processed:

#if! defined (__ cplusplus)

#Error C Complier Required.

#ENDIF

When you encounter #Error instructions, compile end.

# IF, # Elif, # else and #endif instruction

# IF, # Elif, # Else and #endif instructions Control the compilation of a part in the source file. If there is a non-zero value after the expression (#IF), the rows followed by the #IF instruction will remain in the conversion unit.

grammar

Condition:

IF section ELIF section OPT ELSE section OPT Endif

IF section:

IF line

This IF line:

#IF constant expression

#ifDef identifier

#ifndef identifier

ELIF section:

Elif line text

Elif section Elif Text

Elif line:

#ELIF constant expression

ELSE section:

ELSE line text

ELSE line:

#ELSE

Endif line:

#ENDIF

Each #IF instruction in the source file must match the nearest #ENDIF. The number of #elif instructions before #IF and #ENDIF instructions is not limited, but only one #else instruction can be available.

#ELSE must be the last instruction before #ENDIF. # IF, # Elif, # Else and #endif instructions can nested in the text section of other #IF instructions. Each nested # else, # Elif or #Endif instruction should belong to a nearest #IF instruction in front.

All conditional compile instructions, such as #IF and #ifdef, must match the nearest #ENDIF command before the file; otherwise, an error message will be generated. When the conditional compilation instructions are included in the included file, they must meet the same conditions: there is no mismatch instruction in the end of the containing file.

The macro replacement is performed in the command line section of the #elif command, so a macro is available in a constant expression.

The preprocessor selection one of the appearances of a given text makes further processing. A block specified in the text can be any sequence of text. It may take up one or more. Usually this text is a program text that makes sense for compilation and preprocessors.

Pre-regulators handle the text and transfer it to the compiler. If the text contains a pre-processor instruction, the preprocessor will execute these instructions. The compiler only compiles the text block selected by the pre-processor.

The preprocessor selects a single text item by evaluating a constant expression after each #IF or #elif instruction until a constant expression is found for true (non-0). Pre-regulators Select all texts (including other preprocessor instructions beginning with # outgoing) until it associates # Elif, # else or #ENDIF. If all the appearances of constant expressions are false, or if there is no #LIF instruction, the preprocessor will select the text block after #ELSE. If #else is ignored, and the constant expression in all #IF blocks is false, no text block is selected.

A constant expression is an integral constant expression with additional limitations:

* The expression must be integer and can include integer, character constants, and defined operators.

* Expression cannot use SIZEOF or a type of styling operator.

* The target environment cannot represent all the range of integers.

* In the translation representation, the int type and long types, and the unsigned int type and the unsigned long type are the same.

* The translator can translate the character constant into a group of code values ​​that are different from the target environment. In order to determine the attributes of the target environment, the macro value of Limits.h should be detected in the application established for the target environment.

* Expressions do not need to perform all environment queries, but need to be separated from the execution process of the target computer.

Pre-regulator operator Defined can be used for special constant expressions, the syntax is as follows:

grammar

Defined (identifier)

Defined identifier

If this identifier is currently defined, the constant expression is considered true (non-0); otherwise, the condition is false (0). A identifier defined as empty text can be considered defined. The Defined instruction can only be used for #IF and #endif instructions.

In the following example, # if and #endif instructions control the compilation of one of three functions:

#if Defined (Credit)

Credit ();

#elif defined (Debit)

DEBIT ();

#ELSE

Printerror ();

#ENDIF

If the identifier CREDIT is defined, the function call to the CRedit is compiled. If the identifier DEBIT is defined, the function call to the debit is compiled. If no identifier is defined, the function call for Printerror will be compiled.

Note that in C and C , Credit and CRedit are different identifiers because their case is different.

The conditional compile statement in the following example gives a defined symbol constant named DLEVEL:

#if Dlevel> 5

#define signal 1

#if stackuse == 1

#define stack 200

#ELSE

#define stack 100

# Endif # Else

#define signal 0

#if stackuse == 1

#define stack 100

#ELSE

#define stack 50

#ENDIF

#ENDIF

#if Dlevel == 0

#define stack 0

#elif Dlevel == 1

#define stack 100

#ELF DLEVEL> 5

Display (Debugptr)

; #else

#define stack 200

#ENDIF

There are two groups of nested # IF, # else, and #endif instructions in the first # IF block. The first set of instructions is only executed when Dlevell> 5 is true; otherwise, the statement after #ELSE is executed.

The #elif and #else instructions in the second group select one of the four options based on a DLEVEL value. Constant Stack is defined as 0, 100 or 200 based on DLEVEL. If DLEVEL is greater than 5, the compile statement:

#ELF DLEVEL> 5

Display (Debugptr);

And the Stack is not defined at this time.

Conditional compilation is generally used to prevent multiple inclusion of the same header file. The location of the class is often defined in the header file in C , and the following structure can be used to prevent multiple definitions.

//Example.h example header file

#if! defined (Example_H)

#define examplee_h

Class Example

{

...

}

#ndif //! defined (Example_H) The code above is used to check if the symbol constant eXample_h is defined. If the file has been defined, the file has been included and does not need to be processed; if it is not defined, the constant eXample_h will be defined to mark the example.h as already processed.

Microsoft Special Office

The conditional compilation expression is considered as a Signed long value, and these expressions are evaluated with the same rule with expressions in C . For example, expression:

#if 0xffffffffl> 1 ul

It is true.

Microsoft End

#ifDef and IFNDEF Directive

#ifdef and #ifndef instructions are the same as using the Defined (identifier) ​​operator.

grammar

#ifDef identifier

#ifndef identifier

Equivalent to

#if Defined Identifier

#if! defined identifier

#IF instructions can be used anywhere to use #ifDef and #ifndef instructions. When the identifier has been defined, the # IFDEF identifier statement is equivalent to #if 1; and when the identifier is not defined or reversed with the #undef instruction, the statement is equivalent to #IF 0. These instructions are only used to check whether the identifier in the C or C source code is instead of instead of checking the identifier in the C or C source program.

Providing these instructions is only compatible with old versions of the language. The current trend is to biased the #IF instruction to define a constant expression using a defined (identifier).

#ifndef instruction Test the conditions opposite to #ifDef. If the identifier is not defined (or has been reversed), the condition is true (non-0); contrary, the condition is false (0).

Microsoft Special Office

You can use the / d option to transfer the identifier from the command line, and use / d option to specify 30 macros. Check if a definition is very useful because the definition can be transmitted from the command line. E.g:

//prog.cpp

#ifndef test // These three statements are placed in your code

#define final

#ENDIF

CL / DTEST Prog.cpp // This is a compiled command

Microsoft End

#import instruction

C special place

#import instructions are used to combine information from a type library. The content of this type of library is converted to a C class, primarily used to describe the COM interface.

grammar

#import "File Name" [Properties]

#meport [Properties]

Attributes:

Property 1, attribute 2, ...

Attribute 1 attribute 2 ...

File name is a name of a file containing type library information. A file can be one of the following types:

* A type library (.tlb or .odl) file.

* An an executable (.exe) file.

* A library file (.dll) containing type library resources (such as .ocx).

* A composite document containing the type library.

* Other file formats that can be supported by the LoadTypelib API.

There can be a directory specification before the file name. The file name must be a name that already exists. The difference between the two formats is that the order of the preparallert retrieval type library file is different when the path is not fully described.

action

Grammatical format

Quotation marks format This format allows the preparation unit to first search the type library file with the file containing the #import statement, and then searched in all directories including the file (#include), and search in the following path.

Ridge brackets This format indicates that the pre-processor is searching the type library file along the following path.

The compiler searches for named files in the following directory:

1. Path environment variable path table.

2. LIB environment variable path table.

3. Use / i (additional directory) compiler option to specify the path. #import can optionally contain one or more properties. These properties make the compiler change the content of the type library header file. A backslash (/) is available in a single #import statement contains additional rows, for example:

#import "Test.lib" NO_NAMESPACE /

Rename ("OldName", "NewName")

#import attribute is listed below:

excludehigh_method_prefixhigh_property_prefixesimplementation_onlyinclude (...) inject_statementnamed_guidsno_auto_excludeno_implementationno_namespaceraw_dispinterfacesraw_interfaces_onlyraw_method_prefixraw_native_typesraw_property_prefixesrenamerename_namespace # import directive to create two reconstructed content type library header files in C source code, header files, and with the first Microsoft Interface Definition Language (the MIDL) compiler generated similar header files, But there is an additional compiler to generate code and data. The first header file has the same basic name as the type library, its extension is .tlh. The second header file also has the same basic name as the type library, its extension is .tli. It includes the implementation of the compiler to generate member functions and is included in the first header file of (#include).

Both headers are in the output directory specified by / fo (Name Object File) option. They are then read and compiled, just like the first header file is named by the #include command.

The following is an optimization of compiler accompanying #import instructions:

* When the header file is created, the same time flag will be assigned to the class library.

* When processing #import, the compiler first tests whether the header file exists and has expired. If the condition is true, you don't need to recreate it.

* The compiler delay for the initialization of the OLE subsystem until the first #import command is encountered.

#import instructions can also be involved in minimal reconstruction and can be placed in a precompiled header file.

Basic type library head file

The basic type library header file consists of seven parts:

1. Head fixed body: consist of a #include statement and other complicated installation information for Comdef.h (define some standard macros in the head).

2. Forward reference and type definition: Descriptions of structural IMYINTERFACE and the type definitions for some Tkind_alias items.

3. Sensitive pointer description: Module class _com_ptr_t is a package interface pointer and eliminating the sensitivity pointer implementation of addref, release, and queryinterface functions. In addition, it hides CocreateInstance calls in a new COM object. This section uses the macro statement _com_smartptr_typedef to create the type definition of the COM interface as the template specialization of the _COM_PTR_T template class. For example, for interface IFOO, .tlh files contain:

_COM_SMARTPTR_TYPEDEF (ifoo, _UUIDOF (iFoo));

The compiler expands its: Type DEF _COM_PTR_T <_com_iiid > iFooptr;

Type Ifooptr can be used in places where the original interface pointer IFOO *. As a result, there is no need to call various IUNKNOWN member functions.

4. TypeInfo Description: Mainly composed of class definitions and other items, these item descriptions are filed by iTyptlib: GetTypeInfo. In this section, each information type from the type library is reflected in the header in a format that relies on Typekind information.

5. Optional Old GUID Definition: The initialization process of the named GUID constant is included, which is the name of the format CLSID_COCLASS and IID_INTERFACE, similar to those generated by the MIDL compiler.

6. Use the #include statement for the second type of library header.

7. End fixed body: Currently, #pragma pack (POP).

The above parts are all in addition to the head fixed body and end fixed body parts, which are included in the original IDL file to specify the namespaces of their name with the library statement in the original IDL file. You can use this name from the type library header by explicitly defining or comprising the following statement.

USING NAMESPACE MYLIB

Immediately after the #import statement of the source code

The namespace can be blocked with the NO_NAMESPACE attribute of the #import instruction. But the namespace that blocks may cause a name conflict. The namespace can also be re-renamed with the rename_namespace property.

The compiler provides a full path to any type library that needs to rely on the type library currently processed. The path is written to the type library header (.tlh) generated by the compiler for each processing by the compiler. If a type library contains type references to other type library definitions, the .tlh file will include the following comments:

//

// Cross-Reference Type Libraries:

//

// # Import "c: /path/typelib0.tlb"

//

The actual file name in the #import comment is the full path to the type library stored in the crossed reference. If you encounter an error due to the omitted type definition, check the comment of the .tlh header, see which dependency type library needs to be entered first. Map with errors (for example, C2143, C2146, C2321), C2501 (missing description indicator), or C2433 (, in data descriptions), or C2433 (, in data instructions).

You must determine which dependency comments are not given by the system head, but give a #import instruction before relying on the #import instruction of the type library to eliminate these errors.

Exclude attribute Exclude ("said 1" [, "Name 2", ...])

Name 1

Exclusive first item

Name 2

The second item excluded (if necessary)

Type libraries may be included in the definition of items defined within the system head or other type library. This property can be used to exclude these items from the generated type library header file. This property can take any number of parameters, each parameter is a rejected advanced type library project:

High_method_prefix attribute

High_method_prefix ("prefix")

Prefix

The prefix used

In the default, advanced error handling properties and methods are displayed with a unfashionable member function. This name comes from the type library. High_method_prefix attribute Description A prefix for naming these advanced properties and methods.

High_property_prefixes attribute

High_property_prefixes ("GetPrefix," Putprefix, "PutRefprefix")

GetPrefix

Prefix for the PropGet method

PUTPREFIX

Prefix for the PropPut method

PutRefprefix

Prefix for the PropPutref method

In the default, advanced error handling methods, such as PROPGET, PROPPUT, and PROPPUTREF, which are used in a member function named for prefix Get, PUT, and PUTREF, respectively. The high_property_prefixes property is used to indicate the prefix of these three attribute methods, respectively.

Implementation_only attribute

The importation_only property is forbidden .TLH header file (basic header file) generated. This file includes all instructions for displaying the content of the type library. The implementation of the .tli header file and the Wrapper member function will be generated and included in the compilation process.

When this property is specified, the content of the .tli header will be placed in the same namespace for the content of the normal .tlh header. In addition, the member function will not be used as a connection. The importation_only property is generally paired with the NO_IMPLEMentation property to track the implementation other than the pre-translated header file (PCH). A #import statement with a NO_IMPLEMENTATION property is placed in the source area used to create a PCH, and the PCH will be used by some source files. A #IMPORT statement with an Implementation_only property is then used outside the PCH area. Only one of this statement in one source file. This will generate all the necessary Wrapper members functions that do not require additional recoilateration for each source file.

Note: Use the importation_ only attribute in a #import statement. You must use another #import statement of the no_implementation attribute in the same type of library. Otherwise, a compilation error will be generated. This is because the Wrapper class definition generated by the No_Implementation property is required to compile the statement generated by the implementation_only property.

Include (...) attribute

Include (Name 1 [, Name 2, ...])

Name 1

The first item is forced

Name 2 Second of the item (if necessary)

Type libraries may be included in the definition of items defined in the system head or other type library. #import instructions try to use automatic exclusion to avoid multiple definition errors. If these items have been rejected, as indicated by warning C4192, and they should not be rejected, this property can be used to disable automatic rejection. This property can take any number of parameters, each parameter, should be the name of the type library item included.

INJECT_STATEMENT attribute

INJECT_STATEMENT ("Source_Text")

Source_text

The source text that is inserted into the type library header file.

The Inject_Statement property is inserted into the type library header as the source text. This text is placed in the start of the namespace description of the type library content in the header file.

Named_guids attribute

The named_guids property allows the compiler to define and initialize the Template libid_mylib, clsid_mycoclass, iid_myinterface, and DiID_MyDispinterface's old format of GUID variables.

NO_IMPLEMENTATION attribute

This attribute blocks the generation of the .tli header file, this file contains the implementation of the Wrapper member function. If this property is specified, the .tlh head will generate without a #include statement including the .tli header file.

This attribute is used with the importation_only property.

NO_AUTO_EXCLUDE attribute

Type libraries may include definitions defined in the system head or other type library. #import Attempts to avoid multiple definition errors by automatically rejecting these items. When doing this, each exclusive item will generate a C4192 warning message. You can prohibit this property from using automatic exclusion.

NO_NAMESPACE attribute

The type library content in the #import header is generally defined in a namespace. The name of the namespace is specified in the library statement of the original IDL file. If you specify the NO_NAMESPACE property, the compiler will not generate this namespace.

If you want to use a different namespace, you should use the rename_namespace property instead.

Raw_dispinterfaces attribute

Raw_dispinterfaces properties allow the compiler to generate a low-level Wrapper function. This function is used to call IDispatch :: Invoke and return the Dispinterface method and properties of the HRESULT error code. If this property is not specified, only advanced Wrapper is generated, which discards the C exception when fails.

Raw_Interfaces_only property

The Raw_Interfaces_Only property is disabled from generating an error handling Wrapper function and an__declspec (attribute) using these Wrapper functions.

The Raw_Interfaces_Only property also causes the default prefix in the nameless non__property function. Usually the prefix is ​​RAW_. If this property is specified, the function name will be generated directly from the type library. This attribute only allows the low level of the type library to be displayed.

Raw_method_prefix attribute

Raw_method_prefix ("prefix")

Prefix

The prefix used

Use RAW_ as a member function that default prefix to exhibit low-level properties and methods to avoid conflicts with advanced error handling member functions. The Raw_Method_PREFIX property is used to specify a different prefix. Note: The effect of the RAW_METHOD_PREFIX property does not change due to the existence of the RAW_METHOD_PREFIX attribute. When an prefix is ​​explained, Raw_Method_PREFIX always takes precedence over Raw_Interfaces_only. If two properties are used in the same #import statement, the prefix specified by Raw_Method_PREFIX is used.

Raw_native_types attribute

By default, advanced error handling methods use COM support class_bctr_t and _variant_t in BSTR and Variant data types and original COM interface pointers. These classes encapsulate the details of memory storage allocation and unassigning these data types, and greatly simplifying type modeling and conversion operations. Raw_native_types properties are forbidden to use these COM support classes in the advanced Wrapper function, and enforce replacement using low-level data types.

Raw_property_prefix attribute

Raw_property_prefix ("GetPrefix", "Putprefix", "PutRefprefix" getPrefix

Prefix for the PropGet method

PUTPREFIX

Prefix for the PropPut method

PutRefprefix

Prefix for the PropPutref method

By default, low-level methods Propget, PropPut, and PropPutref are displayed by members of GET_, PUT_, and PUTREF_, respectively. These prefixes are compatible with the names in the header file generated by MIDL. The Raw_Property_PREFixes property is used to illustrate the prefix of these three attribute methods, respectively.

Rename properties

Rename ("OldName" "" newname ")

OldName

The old name in the type library

Newname

Name used to replace the old name

The rename property is used to solve the problem of the name conflict. If this property is specified, the compiler will replace the NEWNAME supplied in the result header file in all of the OldName in the type library.

This property is used in one name in the type library and the macro definition in the system header file. If this situation is not resolved, a large number of grammar errors, such as C2059 and C2061.

Note: This replacement is used for the name of the type library instead of the name in the result header file.

Here is an example: assuming that there is a name that is name myParent in the type library, and a header file defines a macro getMyParent before #import. Since getMyParent is the default name for a Wrapper function for error handling properties Get, a name conflict will be generated. To solve this problem, use the following properties in the #import statement:

Rename ("MyParent", "MyParentx")

This statement will rename the name myparent in the type library, and try to rename the getMyParentWrapper name.

Rename ("getmyParent", "getmyParentx")

This is because the name getMyparent only appears in the result type library header file.

Rename_namespace properties

Rename_namespace ("newname")

Newname

New Name of Name Space

The rename_namespace property is used to rename the namespace containing the type library content. It has a parameter with a new name space for new names.

Eliminate namespaces can use the NO_NAMESPACE attribute.

C special place

#include instruction

#include instruction tells the pre-processor to process the content of a specified file, just before these contents appear in the source program that appears in this instruction. You can define constants and macros in the included file, then use the #include instruction to add these definitions to any source file. An instructions containing files for external variables and complex data types are also useful.

You only need to define and name these types once in an inclusion file created for this purpose.

grammar

#include "patH-spec"

#include

Path_spec is an optional file name with a directory instructions. This file name must be named an existing file.

The syntax of PATH_SPEC relies on compiling the operating system.

Both grammar formats lead to replace the instruction with all the contents of the included files. The difference between the two formats is the order of the pre-processor search header files when the path is not completely specified.

Grammar format action

Quotation marks format This format indicates that the pre-processor searches in the same directory of the file containing the #include statement, and then searches in any directory including the file. Subsequent, the prequisition searches specified along the / i compiler option, and finally the path search in the include environment variable.

Angbuble format This format indicates that the preprocessor first searches for a file in the path specified by the / i compiler option. Then search in the path illustrated in the include environment variable

Once the preprocessor finds the specified file, it stops searching. If a dual quotation is given a clear and complete path to the file, the preprocessor will only search the path specification and ignore the standard directory.

If the file name between the double quotes is not a complete path specification, the preprocessor will first search the "parent" directory. The parent file is a file containing a #include instruction. For example, if you include a file named file2 in a file named File1, File1 is a parent file. The included file can be nested; this means a #include instruction appears in a file named after another #include command. For example, the above file file2 can include file file3, in which case File1 is the parent file of File2, and is a grandfather file of File3.

When the file is included, the directory search first starts by the directory of the parent file, and then search for the directory of the grandfather file.

Therefore, search starts from the directory containing the current processing source file. If the file is not found, the search will go to the directory specified by the / i compiler option, and finally search for the directory specified by the include environment variable.

The following example gives the files that use angle brackets include:

#include

This example adds the file content named stdio.h to the source program. Aggregate indicates that the pre-processor searches for the directory specified by the environment variable of stdio.h after searching the / i compile option description. The following example gives the files with quotation marks include: #include "defs.h"

This example adds the file content specified by DEFS.H to the source. Double Quotes Mark means that the preprocessor first searches for directorys that contain the parent file.

The nesting of the containing file can be up to 10 floors, as long as the preprocessor will continue to add the included file to the original source file when the nesting #include command is processed.

Microsoft Special Office

For positioning, the source file can be included, the preparation is first searched for the directory specified / i compiler option. If the / I option is not given or has failed, the preprocessor uses the include environment variable to search for the included files in yournestal brackets. The include environment variable and / i compiler options can contain multiple paths separated by semicolons. The preprocessor will search for them in the order in which the / i option is more than one directory in the / I option or in the include environment variable.

For example, the command:

CL / ID: / MSVC / INCLUDE MyProg.c

Search for the pretreatment in the directory D: / MSVC / Include, you can search for an included file such as stdio.h. Command: set include = d: / msvc / incn

CL myprog.c

Have the same role. If all search failed, a fatal compilation error will be generated.

If a file name containing a file is integrated with a path including a colon (for example, f: /msvc/special/incl/test.h), the preprocessor will be searched along this path.

For included files specified as #include "path_spec", directory search will start from the directory of the parent file, then search for the directory of the grandfather file. Therefore, the search will start from the directory of the source file containing the currently processed #include instruction, and if no grandfather file or file is not found, the search will continue, just like the file name includes in a plurality of brackets.

Microsoft End

#LINE Directive

#LINE instruction tells the pre-processor to convert the line numbers and file names stored in the compiler into a given line number and file name. The compiler uses the line number and the file name to point out the errors found during the compilation process. The line number generally refers to the current input line, the file name refers to the current input file. Each line is handled, the line number is increased.

grammar

#LINE

Digital sequence "file name" OPT

The value of the digital sequence can be any constant constant. Macro replacement can be performed in the pre-processing language symbol, but the result must be evaluated as the correct syntax. The file name can be a combination of arbitrary characters, and should be built between double quotes (""). If the file name is omitted, the previous file name remains unchanged.

You can change the source row and file name by writing a #LINE instruction. The translator uses the line number and file name to determine the value of the predefined macro __file_ _ and _ _line_ _. You can use these macros to add self-description error messages into program text. For more information on these macros, see a predefined macro.

__File_ _ macro extension into a string of the file name enclosed in double quotes ("" ").

If you change the line number and file name, the compiler will ignore the original value and continue processing with the new value. #LINE instructions are typically used by the programs generator to generate an error message to the initial source program, not the generator. The following example is used to illustrate #LINE and _ _line_ _ and _ _file_ _ macro. In this statement, the line number of internal storage is set to 151, and the file name is changed to Copy.c.

#Line 151 "Copy.c"

In this example, if a given "assertion" is not true, the macro Assert uses a predefined macro __line_ _ and _ _file_ _ _ printed an error message about the source file. #define assert (COND)

IF (! (! (!) /

{Printf ("Assertion Error Line% D, File (% s) / N", /

__Line_ _, _ _file__);

NULL instruction

The air pre-processor instruction is a single number of numbers (#), no effect.

grammar

#

#undef instruction

As its name is impossible, # undef command cancels (reverse defined) A name created by the #define instruction.

grammar

#undef

Identifier

#undef instructions Cancel the current definition of the identifier. As a result, each appearance of the identifier will be ignored by the preprocessor. To cancel a macro definition with #undef, only the macro identifier is given, and the parameter table is not required.

You can also use the #undef instruction for an identifier for an undefined identifier. This will confirm that this identifier is undefined. Macro replacement cannot be executed in the #undef statement.

#undef instructions are usually matched with a #define instruction to create a zone in the source program, and one identifier in this area has its specific meaning. For example, a unique function of the source program can use explicit constant definitions that do not affect the environment specific value of the lower part of the program. #undef instructions can also be paired with the #IF instruction to compile the condition of the source program. See "# IF, # Elif, # else, and #endif instructions" for more information.

In the following example, the # UNDEF command cancels a symbol constant and a macro definition, and note that the instruction only gives the macro identifier.

#define width 80

#define add (x, y) (x) (y)

... # undef width

#undef add

Microsoft Special Office

The macro can definition by using the / u option command line definition, this command follows the macro name of the definition. This command is equal to the role of the #undef macro name statement at the beginning of the file.

Microsoft End

Preprocessor operator

The text of the #define instruction has four pre-regulatory operators (see table below).

Characterization, character string, and language symbol paste operators will be discussed in the following three chapters. See "# IF, # Elif, # else, and #endif instructions" in the DEFINED operator.

Operator action

Character string operator (#) will place the corresponding reference within Double quotes

Characterization operator (# @) places the corresponding parameters in single quotes and treats them as characters (Microsoft Special place)

Language symbol paste operators (##) can use the language symbol as the unintended, and combine it into other language symbols.

Renewal

The defined operator simplifies the writing of composite expressions in a particular macro

Character string operator (#)

The digital symbol or "character string" operator (#) converts the macro parameter (after extending) into a string constant. It is only used for macros with parameters. If it is enclosed in brackets before one of the form parameters in the macro definition, the actual parameters of macro are enclosed in brackets and is seen as a string text. The string text will then replace each appearance of operators and metall parameters in the macro definition.

The blank after the first language symbol of the arguments and the blank after the last language symbol is ignored. All blank between the language symbols in the field is seen as a space in the resulting string semantic. Therefore, if an annotation in the factor is between two language symbols, it will be considered as a space. Result String text is automatically connected to any neighboring string of only spaced separated.

In addition, if a character contained in the parameter is used as a string text (for example, a dual quotation mark (") or backslash (/) character), a essential escape backslash Before being automatically inserted into characters. The following example gives a macro definition containing a character string operator and a main function that calls the macro:

#define stringer (x) Printf (#x "/ n")

void main ()

{

STRINGER (in quotes in the printf function call / n);

STRINGER ("in quotes when printed to the screen" / n);

STRINGER ("this: /" print ");}

This call will be expanded during pre-processing, generating as follows:

void main ()

{

Printf ("in quotes in the sharpf function call / n" "/ n");

Printf ("/" in quotes when printed to the screen / "/ n" "/ n");

Printf ("/" this; /// "prints an escaped double quote /" "/ n");

}

When running the program, the screen output of each line is as follows:

In Quotes in the Printf Function Call

"In quotes when printed to the screen"

"This; /" Prints an escaped double quotation mark "

Microsoft Special Office

Microsoft C (Version 6.0 and earlier) Extended ANSI C standard, ANSI C extension The macro-shaped parameters that appear in string text and character constants are no longer supported. The code that relies on this extension should be rewritten using a character string operator (#).

Microsoft End

Characterization operator (# @)

Microsoft Special Office

Characterization operators can only be used for macrocence, if the macro definitions are in front of one, the reference is placed in single quotes, and the macro extension is used as a character processing. E.g:

#define makechar (x) # @ x

Put the statement:

A = Makechar (b);

Expansion is:

A = 'b';

Single number characters cannot be used for characterized operators.

Microsoft Special Interface

Symbol paste operator (##)

Dual Digital Language Symbol or "Language Symbol Paste" operator (##), sometimes referred to as a "merge" operator, used for class object macro and class function macro. It allows the separated language symbol to add a single language symbol, so it cannot be the first language symbol or the last language symbol of the macro.

If the shape in a macro definition is before and after the language symbol paste operator, the shape will be replaced immediately unlasting. Do not perform macro extensions before replacing.

Then, the language symbol string will be deleted each appearance in the language symbol string, and the language symbols before and after it will be merged. The resulting language symbol must be a valid language symbol. If it is valid, if the language symbol represents a macro name, scan it to discover possible replacement. This identifier represents the name of the language symbols that know the merged language in the replacement program. Each language symbol represents a language symbol defined in the program or in the compiler command line.

The blank before and after the operator is arbitrary.

The following example illustrates the usage of the character string operator and language symbol paste operator in the program output: #define paster (n) Printf ("token" #n "=% D", Taken ## n)

INT token9 = 9;

If a macro is called similar to the numerical parameter:

PASTER (9);

Macro will be generated:

Printf ("token" "9" "=% D", token9);

It becomes:

Printf ("token9 =% D", token9);

Macro

The pretreatment of macro expansion is performed in a line that is not a row that is not a pretreatment instruction (the first non-empty character is not a #), and its instructions are not ignored as part of the conditional compilation. The "Condition Compilation" instruction allows which text block is removed from the source file by detecting a constant expression or identifier to determine which text block is sent to the compiler during the pre-processing process, which text block is removed from the source file and controls a source file in this way. Partial compilation.

#define instructions typically use meaningful identifiers with constants, keywords, common statements, and expressions. The identifier indicating constant is sometimes referred to as "symbol constant" or "explicit" constant. The constant indicating the statement or expression is called "macro". In this pre-processor document, only the term "macro" is used.

When the macrology is identified in the program source text or in the parameters of some other preprocessor commands, it is processed to call the macro. The macro name is replaced by a copy of the macro body. If the macro is adjacent to the parameters, the ginseng after the macro name will replace the ginseng in the macro. The copy of the macro body is replaced with a macro, called the "extension" of the macro. There are two types of macros in the actual term. "Class object" macro does not take parameters, and "class function" macro can be defined as a belt parameter. Therefore, their forms and functions are called function calls. Since the macro does not generate actual function calls, sometimes the macro replacement function call makes the program run faster, (in C , the inline function is usually a good way), however, If you accidentally define and use macros, you may also cause trouble. When a parameter macro definition, you must use parentheses to keep the normal priority in an expression, and the macro does not correctly process an expression with side effects. See the example Getrandom in the #define instructions for more information.

Once you have defined a macro, you can't cancel the macro in the original definition, and redefine it as a different value. However, the macro can be redefined with a prior identical definition, so the same definition of the macro in a program can occur multiple times.

#undef instructions are used to cancel the definition of macros. Once the macro definition is canceled, the macro can be redefined into a different value. #define and #undef two sections discussed in detail #define and #undef instructions.

Macro and C

C provides some new features. Some of these functions replace the functions provided by ANSI C. These new features enhance type security and predictability of the language:

* In C , the object illustrated in Const can be used in a constant expression, which enables the program to explain the constant of type and value information, and the constant of enumeration values ​​that can be checked by the debugger one by one. Using preprocessor instruction #define defines constant is not accurate. A Const object will not be assigned any storage unless a expression of an address is found in the program.

* C connection function replaces the function type macro, the advantage of using the connection function relative to the macro is:

* Type security. The same type of type detection is required as the wocker function and the general function, and the macro has no type of security detection.

* Correct a parameter processing with side effects. The connection function evaluates the expression of the parameters before entering the function. Therefore, an expression with side effects will be safe.

For more information on the connection function, see Inline, _ _inline section. In order to be compatible, Microsoft C retains all preprocessor features in ANSI C and earlier C specifications.

Predefined macro

The compiler recognizes six predefined ANSI C macros (see Table 1.1), while Microsoft C implementation provides more predefined macros (see Table 1.2). These macros do not have to be paramented, but they cannot be redefined. Their value (except __line_ _ and _ _ _ _) must be compiled constant. Some predefined macroses listed below are defined by multiple values, and their values ​​can select the corresponding menu option in the Visual C development environment to set or use the command line switch. See the table below for more information.

Table 1.1 ANSI predefined macro

Macro Description __date_ _ _ The compilation date of the current source file. The date is a string text that format is MMM DD YYYY. Month Name MMM is the same as the date generated in the library function asctime described in Time.h __file_ _ _ _ current source file name. __File_ _ extension is the line number of the current source file with a string __line_ _ current source file enclosed with a double quotation number. The line number is a decimal constant constant. You can use a #LINE directive to modify __stdc_ _ to point out full consistency with the ANSI C standard. Defined when the / za compiler option is given and does not compile C code 1; otherwise it is uncertainty __time_ _ current compilation time of the current file. This time is the format of HH: MM: SS string text __timestamp_ _ _TimeStamp_ _ current source files recently modified. The date is the format of DDD MMM Datehh: MM: SS YYYY string text, here DDD is the story of the day of the week, Date is an integer table from 1 to 31

Table 1.2 Microsoft Special predefined macro

Macro Description __char_unsigned default char type is unsigned, when the __cplusplus defined when specified / J is only the C program definition __cpprtti defines the code compiled with / GR (allow runtime type information) __ cppunwind defined as used / gx Compiled code (Allow exception handling) __ DLL Specify / MD or / MDD (multithreaded DLL) __m_alpha is defined when the __m_alpha is defined when using the alpha compiler, if you use another compiler, do not define _ _M_ix86 is the X86 processor definition, see Table 1.3__m_mppc is defined for the Power Macintosh platform, default is 601 (/ qp601) See Table 1.4__m_mrx000 is defined by the MIPS platform, default is 4000 (/ qmr4000), see Table 1.5__m_ppc is PowerPC Platform definition, default is 604 (/ QP604), see Table 1.6__mfc_ver is the MFC version definition, define 0x0421 for Microsoft FounNDation class library 4.21, which is always defined __msc_extensions This macro is using / ZE compilation option (default When defined, it is defined as the value is always 1__msc_ver definition compiler version, for Microsoft Visual C 6.0 is defined as 1200, which is always defined __mt When specified / md or / mdd (multi-thread DLL) or / MT or / MTD (Multi-thread) Options Define __win32 to be defined for Win32 applications. It is always defined as shown in the following table, the compiler generates a value for the preprocessor identifier reflects the processor option.

Table 1.3 _m_ix86 value

Developer's Options Command Line Option Return Value BLEND / GB_M_IX86 = 500 (Default. For the future compiler, a different value will affect the main processor) Pentium / g5_m_ix86 = 500 pentiumPro / g6_m_ix86 = 60080386 / g3_m_ix86 = 30080486 / G4_M_IX86 = 400

Table 1.4 _M_MPPC value

Developer's Options Command Route Option Return Value PowerPC 601 / QP601_M_MPPC = 601 (Default) PowerPC 603 / QP603_M_MPPC = 603PowerPC 604 / QP604_M_MPPC = 604PowerPC 620 / QP620_M_MPPC = 620

Table 1.5 _m_mrx000 value

Command line options options developer return value R4000 / QMR4000_M_MRX000 = 4000 (Default) R4100 / QMR4100_M_MRX000 = 4100R4200 / QMR4200_M_MRX000 = 4200R4400 / QMR4400_M_MRX000 = 4400R4600 / QMR4600_M_MRX000 = 4600R10000 / QMR10000_M_MRX000 = 10000

Table 1.6 _m_ppc value

Command line options options developer return value R4000 / QMR4000_M_MRX000 = 4000 (Default) R4100 / QMR4100_M_MRX000 = 4100R4200 / QMR4200_M_MRX000 = 4200R4400 / QMR4400_M_MRX000 = 4400R4600 / QMR4600_M_MRX000 = 4600R10000 / QMR10000_M_MRX000 = 10000

转载请注明原文地址:https://www.9cbs.com/read-46709.html

New Post(0)