算是備忘吧。。轉幾篇,寫的都差不多,還行。。
pragma comment的使用
該宏放置一個注釋到對象文件或者可執行文件。
#pragma comment( comment-type [,"commentstring"] )
comment-type是一個預定義的標識符,指定注釋的類型,應該是compiler,exestr,lib,linker之一。
commentstring是一個提供為comment-type提供附加信息的字符串,
Remarks:
1、compiler:放置編譯器的版本或者名字到一個對象文件,該選項是被linker忽略的。
2、exestr:在以后的版本將被取消。
3、lib:放置一個庫搜索記錄到對象文件中,這個類型應該是和commentstring(指定你要Liner搜索的lib的名稱和路徑)
這個庫的名字放在Object文件的默認庫搜索記錄的后面,linker搜索這個這個庫就像你在命令行輸入這個命令一樣。你可以
在一個源文件中設置多個庫記錄,它們在object文件中的順序和在源文件中的順序一樣。如果默認庫和附加庫的次序是需要
區別的,使用Z編譯開關是防止默認庫放到object模塊。
4、linker:指定一個連接選項,這樣就不用在命令行輸入或者在開發環境中設置了。
只有下面的linker選項能被傳給Linker.
-
/DEFAULTLIB
-
/EXPORT
-
/INCLUDE
-
/MANIFESTDEPENDENCY
-
/MERGE
-
/SECTION
(1)/DEFAULTLIB:library
/DEFAULTLIB 選項將一個 library 添加到 LINK 在解析引用時搜索的庫列表。用 /DEFAULTLIB
指定的庫在命令行上指定的庫之后和 .obj 文件中指定的默認庫之前被搜索。
忽略所有默認庫 (/NODEFAULTLIB) 選項重寫 /DEFAULTLIB:library。如果在兩者中指定了相同的 library 名稱,忽略庫 (/NODEFAULTLIB:library) 選項將重寫 /DEFAULTLIB:library。
(2)/EXPORT:entryname[,@ordinal[,NONAME]][,DATA]
使用該選項,可以從程序導出函數,以便其他程序可以調用該函數。也可以導出數據。通常在 DLL 中定義導出。entryname 是調用程序要使用的函數或數據項的名稱。ordinal 在導出表中指定范圍在 1 至 65,535 的索引;如果沒有指定 ordinal,則 LINK 將分配一個。NONAME 關鍵字只將函數導出為序號,沒有 entryname。
DATA 關鍵字指定導出項為數據項。客戶程序中的數據項必須用 extern __declspec(dllimport) 來聲明。
有三種導出定義的方法,按照建議的使用順序依次為:
-
源代碼中的 __declspec(dllexport)
-
.def 文件中的 EXPORTS 語句
-
LINK 命令中的 /EXPORT 規范
所有這三種方法可以用在同一個程序中。LINK 在生成包含導出的程序時還創建導入庫,除非生成中使用了 .exp 文件。
LINK 使用標識符的修飾形式。編譯器在創建 .obj 文件時修飾標識符。如果 entryname 以其未修飾的形式指定給鏈接器(與其在源代碼中一樣),則 LINK 將試圖匹配該名稱。如果無法找到唯一的匹配名稱,則 LINK 發出錯誤信息。當需要將標識符指定給鏈接器時,請使用 Dumpbin 工具獲取該標識符的修飾名形式。
(3)/INCLUDE:symbol
/INCLUDE 選項通知鏈接器將指定的符號添加到符號表。
若要指定多個符號,請在符號名稱之間鍵入逗號 (,)、分號 (;) 或空格。在命令行上,對每個符號指定一次 /INCLUDE:symbol。
鏈接器通過將包含符號定義的對象添加到程序來解析 symbol。該功能對于添包含不會鏈接到程序的庫對象非常有用。用該選項指定符號將通過 /OPT:REF 重寫該符號的移除。
我們經常用到的是#pragma comment(lib,"*.lib")這類的。
#pragma comment(lib,"Ws2_32.lib")表示鏈接Ws2_32.lib這個庫。
和在工程設置里寫上鏈入Ws2_32.lib的效果一樣,不過這種方法寫的
程序別人在使用你的代碼的時候就不用再設置工程settings了
comment
Visual Studio 6.0
#pragma comment(comment-type [,commentstring] )
Places a comment record into an object file or executable file. The comment-type is one of five predefined identifiers, described below, that specify the type of comment record. The optional commentstring is a string literal that provides additional information for some comment types. Because commentstring is a string literal, it obeys all the rules for string literals with respect to escape characters, embedded quotation marks ("), and concatenation.
compiler
Places the name and version number of the compiler in the object file. This comment record is ignored by the linker. If you supply a commentstring parameter for this record type, the compiler generates a warning.
exestr
Places commentstring in the object file. At link time, this string is placed in the executable file. The string is not loaded into memory when the executable file is loaded; however, it can be found with a program that finds printable strings in files. One use for this comment-record type is to embed a version number or similar information in an executable file.
lib
Places a library-search record in the object file. This comment type must be accompanied by a commentstring parameter containing the name (and possibly the path) of the library that you want the linker to search. Since the library name precedes the default library-search records in the object file, the linker searches for this library just as if you had named it on the command line. You can place multiple library-search records in the same source file; each record appears in the object file in the same order in which it is encountered in the source file.
linker
Places a linker option in the object file. You can use this comment-type to specify a linker option instead placing the option on the Link tab of the Project Settings dialog box. For example, you can specity the /include option to force the inclusion of a symbol:
#pragma comment(linker, "/include:__mySymbol")
user
Places a general comment in the object file. The commentstring parameter contains the text of the comment. This comment record is ignored by the linker.
The following pragma causes the linker to search for the EMAPI.LIB library while linking. The linker searches first in the current working directory and then in the path specified in the LIB environment variable.
#pragma comment( lib, "emapi" )
The following pragma causes the compiler to place the name and version number of the compiler in the object file:
#pragma comment( compiler )
Note For comments that take a commentstring parameter, you can use a macro in any place where you would use a string literal, provided that the macro expands to a string literal. You can also concatenate any combination of string literals and macros that expand to string literals. For example, the following statement is acceptable:
#pragma comment( user, "Compiled on " __DATE__ " at " __TIME__ )