# Use of Pragma

Source: Internet
Author: User
# Pragma is used. Although both C and C ++ have standards, almost every Compiler (in a broad sense, including connectors) extends some C/C ++ keywords. The rational application of these keywords can sometimes make our work very convenient. The # pragma indicator in Visual C ++ is described below. 1. The traditional method for exporting DLL functions using # pragma is to use the module definition file (. def), Visual C ++ provides a more concise and convenient method, that is, the "_ declspec ()" keyword is followed by "dllexport", telling the connection to export this function, for example: _ declspec (dllexport) int _ stdcall myexportfunction (INT itest); Put "_ declspec (dllexport)" at the beginning of the function declaration, the DLL generated by the connection will export the function "[email protected]". The name of the export function above may not be what I want. We want to export the original "myexportfunction ". Fortunately, VC provides a pre-processing indicator "# pragma" to specify the connection options (not only this function, but also many indication functions), as follows: # pragma comment (linker, "/export: [email protected]") This is just a matter of days :). If you want to specify the export sequence, or export the function as the serial number without entryname, this preprocessing indicator (specifically the connector) can be implemented. Let's take a look at the msdn syntax description: /export: entryname [, @ ordinal [, noname] [, data] @ ordinal specifies the sequence. noname indicates that only the function is exported as the sequence number. The data keyword specifies that the export item is a data item. 2. indicates that the file is contained only once in the header file. Generally, we only need to include it once in the entire project, but if I have multiple. c /. CPP files must contain header files, such as Windows. h, isn't there two declarations? The traditional method to solve this problem is to use # define to define a macro in the header file, such as Windows. in H: # ifndef _ WINDOWS _ # DEFINE _ WINDOWS _ p> and then add # endif to the file to avoid being contained multiple times. But the consequence is that the Code is less readable (personal opinion). VC provides us with another way, that is, adding the following before the file: # Is Pragma once very convenient? 3. Invalid warnings sometimes we have to forcibly convert the variables, which leads to a warning from the compiler. Especially in C ++, the type check is stricter than C. Although this does not affect anything, it seems so uncomfortable-I mean to do this, what do you warn me! :) At this time, you will see the Warning type, for example, "Warning c4311:" type conversion ":" hhook "to" bool "pointer truncation", and add: # pragma warning (Disable: 4311) the compiler has nothing to say :). 4. Specify the library to be used for connection. For example, wsock32.lib is used for connection. You can add it to your project without any effort. However, I think it is more convenient to use the # pragma indicator to specify the library to be connected: # pragma comment (Lib, "wsock32.lib"). 5. It is of little use to display the compilation message, for example: # ifdef _ debug # pragma message ("compile the connection to debug mode... ") # endif // _ debug is among all the preprocessing commands. # The Pragma command may be the most complex, it is used to set the status of the compiler or to instruct the compiler to complete some specific actions. # The Pragma command provides a method for each compiler to provide the unique features of the host or operating system while maintaining full compatibility with C and C ++ languages. According to the definition, the compilation instructions are proprietary to machines or operating systems and are different for each compiler. The format is generally: # pragma para, where para is a parameter. Below are some common parameters. (1) 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 method is as follows: # pragma message ("message text") prints the message text in the compilation output window when the compiler encounters this instruction. 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 if we have defined the _ x86 macro in the source code. We can use the following method # ifdef _ x86 # pragma message ("_ x86 macro activated !") # Endif when 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. (2) The other Pragma parameter that is used more frequently is code_seg. The format is as follows: # pragma code_seg (["section-name" [, "section-class"]). It can set the code segment for storing function code in the program, we use the driver when developing it. (3) # pragma once (commonly used) as long as this command is added at the beginning of the header file, the header file can be compiled once. This command is actually available in vc6, however, considering the compatibility, It is not used much. (4) # pragma hdrstop indicates that the pre-compiled header file ends here, and the subsequent header files are not pre-compiled. BCB can pre-compile the header file to speed up the link, but if all header files are pre-compiled, it may occupy too much disk space. Therefore, this option is used to exclude some header files. Sometimes there is a dependency between units. For example, unit a depends on unit B. Therefore, Unit B must be compiled before unit. You can use # pragma startup to specify the compilation priority. If # pragma package (smart_init) is used, BCB will be compiled based on the priority. (5) # pragma resource "*. DFM" indicates adding resources in the *. DFM file to the project. *. DFM defines the appearance of a form. (6) # pragma warning (Disable: 4507 34; once: 4385; error: 164) is equivalent to: # pragma warning (Disable: 4507 34) // no warning messages 4507 and 34 are displayed # pragma warning (once: 4385) // only one warning message is reported # pragma warning (error: 4385) // use the 164 warning as an error. At the same time, This pragma warning also supports the following format: # pragma warning (push [, N]) # pragma warning (POP) Where n represents a warning level (1---4 ). # Pragma warning (push) saves the existing warning status of all warning information. # Pragma warning (push, n) saves the existing warning status of all warning information and sets the global warning level to n. # Pragma warning (POP) pops up the last warning message to the stack, and all changes made between the inbound and outbound stacks are canceled. Example: # pragma warning (push) # pragma warning (Disable: 4705) # pragma warning (Disable: 4706) # pragma warning (Disable: 4707) //....... # pragma warning (POP) resaves all warning information (including 4707, and) at the end of this Code ). (7) Pragma comment (...) This command puts a comment record into an object file or executable file. Common lib keywords can help us to connect to a library file. # Pragma comment (comment-type [, "commentstring"]) comment-type is a predefined identifier that specifies the annotation type, which should be one of compiler, exestr, Lib, and linker. Commentstring is a string that provides additional information for comment-type. Remarks: 1. Compiler: place the version or name of the compiler to an object file. This option is ignored by linker. 2. exestr: It will be canceled in a later version. 3. Lib: place a library search record to the object file. This type should be commentstring (specify the name and path of the Lib you want to search for by liner) the name of this library is placed behind the default library search record of the object file. linker searches for this library just like you enter this command on the command line. You can set multiple database records in a source file. The order of these records in the object file is the same as that in the source file. If the order of the default Library and the additional library needs to be different, use the Z compilation switch to prevent the default library from being placed in the object module. 4. linker: specify a connection option so that you do not need to enter the connection in the command line or in the development environment. Only the following Linker options can be passed to linker. /defaultlib/export/include/manifestdependency/merge/Section (1)/defaultlib: Library/defaultlib option Add a library to the list of libraries that link searches for when parsing references. Use the library specified by/defaultlib to search for the specified library on the command line and the default library specified in the. OBJ file. Ignore all default libraries (/nodefalib Lib) option to override/defaultlib: Library. If the same library name is specified in both, the ignore Library (/nodefaultlib: Library) option will overwrite/defaultlib: Library. (2)/export: entryname [, @ ordinal [, noname] [, data] use this option to export functions from the program so that other programs can call this function. You can also export data. It is usually defined and exported in DLL. Entryname is the name of the function or data item to be used by the caller. Ordinal specifies an index ranging from 1 to 65,535 in the export table. If ordinal is not specified, link will allocate one. The noname keyword only exports the function as the serial number without entryname. The data keyword specifies that the export item is a data item. The data items in the customer program must be declared using extern _ declspec (dllimport. There are three methods to export the definition. The recommended sequence is: _ declspec (dllexport) in the source code ). the/export specification in the link command of the exports statement in the def file can be used in the same program. Link also creates Import and Export files when generating programs that contain export, unless the. Exp file is used in the generation. Link uses the identifier modifier. The identifier is modified by the compiler when the. OBJ file is created. If entryname is assigned to the linker in its unmodified form (the same as in the source code), the link tries to match the name. If a unique match name cannot be found, link sends an error message. When you need to specify an identifier to the linker, use dumpbin to obtain the modifier form of the identifier. (3) The/include: Symbol/include option notifies the linker to add the specified symbol to the symbol table. To specify multiple symbols, enter a comma (,), semicolon (;), or space between symbol names. On the command line, specify/include: symbol for each symbol. The linker parses the symbol by adding an object containing the symbol definition to the program. This function is useful for adding library objects that do not link to the program. You can use this option to specify the symbol to remove it from/OPT: ref. We often use the # pragma comment (Lib, "*. lib") class. # Pragma comment (Lib, "ws2_32.lib") indicates link to the library ws2_32.lib. It works the same as writing ws2_32.lib into the project settings. However, when using this method to write programs, other people do not need to set the project settings when using your code.

# Use of Pragma

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.