Understanding # pragma comment

Source: Internet
Author: User

# Pragma comment (comment-type [, "commentstring"])
This macro places a comment to the object file or executable file.

Comment-type is a predefined identifier that specifies the annotation type. It 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 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

The/defaultlib option adds a library to the list of libraries that link searches for when parsing references. Use/defalib lib
The specified library is searched after the specified library on the command line and before 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]

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

_ Declspec (dllexport) in source code)

Exports statement in the. Def File

/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. 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)/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.
# Pragma comment (linker, "/section: flag_data, RWS ")
You can share data in the dynamic link library, or use it to start an application only once. Note that there cannot be spaces between flag_data and RWS !!

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.