Use of Pragma comment

Source: Internet
Author: User
For snail bait, it doesn't mean that the speed is fast, but it shows a complete gesture ........

# Use of Pragma

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. In Visual C ++ # pragma

The usage of the indicator.

1. Use # pragma to export DLL Functions

The traditional way to export DLL functions is to use the module definition file (. Def). Visual C ++ provides a simpler and more convenient method,

The keyword "_ declspec ()" is followed by "dllexport" to tell 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, and connect the generated DLL to 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 like the hope of the day :). 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 look at the msdn syntax description:


/Export: entryname [, @ ordinal [, noname] [, Data]

@ Ordinal specifies the order. noname indicates that only the function is exported as the serial number. The data keyword specifies that the export item is a data item.

2. indicates that the file is only contained once.

In the header file, we generally only need to include it once in the entire project, but if I want to package in multiple. c/. cpp files

There are headers, such as Windows. H, and so many statements. Isn't it twice? The traditional method to solve this problem is in the header file

Start out

# Define defines a macro, for example, in windows. h:



# Ifndef _ WINDOWS _ # DEFINE _ WINDOWS _


P> 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: # pragma once before the file is very convenient?

3. Invalid Warning sometimes we have to forcibly convert the variables, which leads to a warning from the compiler, especially in C ++.

, Type check is stricter than C. Although this does not affect anything, it seems so uncomfortable-I deliberately want this,

What are you warning about! :) Now you will see the Warning type,

For example, "Warning c4311:" type conversion ": From" hhook "to" bool "pointer truncation", add:

# The Pragma warning (Disable: 4311) compiler has nothing to say :).

4. Specify the library to be used for connection. For example, wsock32.lib is used for connection. Of course you can

Add it to your project. However, I think it is more convenient to use the # pragma indicator to specify the database to be connected:

# Pragma comment (Lib, "wsock32.lib") 5. It is of little use to display the compiled message. For example:

# Ifdef _ debug # pragma message

("Build connection to debug mode...") # endif // _ debug

 

 

Among all the pre-processing commands, the # pragma command may be the most complex. It is used to set the compiler status or indication.

The compiler performs some specific actions. # Pragma
The instruction provides a method for each compiler to provide the host or operating system proprietary when it is fully compatible 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
Here, para is a parameter. Below are some common parameters.

(1) Message parameter. The message parameter is one of my favorite parameters. It can be used in the compilation information output window.
Output the corresponding information, which is very important for controlling the 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 there are correct settings.

Set these macros. At this time, we can use this command to check during compilation. Suppose we want to determine whether we are in the source

Where is the code defined?

_ The following method can be used for the x86 macro:
# Ifdef _ x86
# Pragma message ("_ x86 macro activated !")
# Endif
After we define the _ x86 macro, the application will display "_
X86 macro activated !". 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. 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.

(3) # pragma once (commonly used)
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,

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 add

Fast Link speed, but if all header files are pre-compiled, it may occupy too much disk space, so use this option 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 specifies the compilation priority. If # pragma package (smart_init) is used, BCB will

Size is compiled successively.

(5) # pragma resource "*. DFM" indicates adding resources in the *. DFM file to the project. * DFM includes the form
The definition of the appearance.

(6) # pragma warning (Disable: 4507 34; once: 4385; error: 164)
It is equivalent:
# Pragma warning (Disable: 4507 34) // do not display the 4507 and 34 Warnings
# Pragma warning (once: 4385) // only one warning message is reported once
# Pragma warning (error: 164) // the error message 164 is used as an error.
This pragma warning also supports the following formats:
# Pragma warning (push [, N])
# Pragma warning (POP)
Here 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 global warning
The level is set to n.
# Pragma warning (POP) pops up the last warning message to the stack.
All changes are canceled. For example:
# Pragma warning (push)
# Pragma warning (Disable: 4705)
# Pragma warning (Disable: 4706)
# Pragma warning (Disable: 4707)
//.......
# Pragma warning (POP)
At the end of the Code, save all warning information (including 4707, and ).
(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-typeIt is a predefined identifier and specifies the annotation type. It should be one of compiler, exestr, Lib, and linker.
Commentstring isComment-typeA string that provides additional information,
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 consistent
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 and additional libraries is required
The difference is that the use of the Z compiler switch prevents default libraries 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 OptionLibraryAdd 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, ignore the Library (/nodefaultlib:Library) Option will override/defalib Lib:Library.

(2)/export:Entryname[,@Ordinal[, Noname] [, Data]

With this option, you can 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.EntrynameIs the name of the function or data item to be used by the calling program. 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. NoEntryname.

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 for exporting definitions, which are recommended in sequence:

  1. _ Declspec (dllexport) in source code)

  2. Exports statement in the. Def File

  3. /Export specification in link command

All three methods 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. IfEntrynameIf the name is specified to the linker as it is not modified (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)/INCLUDE:symbol

The/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.
This method is the same as writing ws2_32.lib to the Project Settings.
When someone else uses your code, there is no need to set the project settings.

Posted on snail Scan also coding read (1925) Comments (1) EDIT favorites reference

Use of Pragma comment

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.