subsystem

用來指定程序的入口函數

subsystem,連接器參數,用來指定程序的入口函數

簡介


Windows操作系統在裝載應用程序並且做完初始化工作后,就轉到程序的入口點開始執行你編寫的程序。程序的默認入口點實際上是由連接程序設置的,不同的連接器選擇的入口函數也大都不同。在VC++ 6.0下,連接器對控制台程序所設置的入口函數是 mainCRTStartup,也就是說Windows操作系統在裝載完程序並且初始化完成後,就進入入口函數mainCRTStartup ,然後由它調用你自己編寫的 main 函數,開始執行具體的代碼;對於圖形用戶界面(GUI)程序設置的入口函數是 WinMainCRTStartup,和上面一樣,WinMainCRTStartup再調用你寫的WinMain 函數執行具體功能。
那麼,設置哪個入口點可以由連接器的“/subsystem:”參數確定的,它告訴操作系統如何運行編譯連接所生成的.exe可執行文件。這裡我們可以指定四種方 式:“CONSOLE | WINDOWS | NATIVE | POSIX”如果這個選項參數的值為“WINDOWS”,則表示該應用程序運行時不需要控制台。

詳細參數


該選項的語法如下:
/SUBSYSTEM:{ CONSOLE | WINDOWS | NATIVE | WINDOWSCE | POSIX |} [,left[,right]]

CONSOLE

win32 字元模式應用程序,此種類型的應用程序在運行的時候會產生一個類似DOS
窗口的控制台窗口,如果在應用程序的主函數為main()或者wmain()時,在默認情況下
該應用程序就是一個控制台應用程序。

WINDOWS

該類型的應用程序不產生console窗口,該類型的應用程序的窗口由用戶自己創建,簡而言之
就是一個標準的Win32 application,其入口地址為WinMain()函數或者wWinMain()函數的地址,
如果你在應用程序中定義的主函數為WinMain或者wWinMain,在默認情況下該應用程序就是一個
Win32 Application !

NATIVE

設備驅動器選項,如果/DRIVER:WDM選項被設定的話,該鏈接選項(NATIVE)就為默認選項。

WINDOWSCE

運行在windows CE上的應用程序。

POSIX

在windows NT 種運行在POSIX子系統上的應用程序。
[,left[,right]]:
主版本號和次版本號,該選項為可選,該選項為0~65535之間的十進位整數,CONSOLE, WINDOWS, and NATIVE 默認的版本號為4.00;
POSIX默認的版本號為19.90。
以下是Microsoft MSDN原文:
This option edits the image to indicate which subsystem the operating system must invoke for execution. /SUBSYSTEM:{CONSOLE|WINDOWS|NATIVE|WINDOWSCE|POSIX|}[, left[, right]]
You can specify any of the following subsystems:
The CONSOLE subsystem handles a Win32 character-mode application that use a console supplied by the operating system.
The WINDOWS subsystem handles an application that does not require a console and creates its own windows, if required.
The NATIVE subsystem handles a Windows NT device driver.
The WINDOWSCE subsystem handles Windows CE consumer electronics applications.
The POSIX subsystem handles a POSIX application in Windows NT.
The optional left and right values specify the minimum required version of the specified subsystem. The whole number part of the version number (the portion to the left of the decimal point) is represented by left. The fractional part of the version number (the portion to the right of the decimal point) is represented by right. The values of left and right must be from 0 through 65,535. The default is version 4.00 for CONSOLE, WINDOWS, and NATIVE; and version 19.90 for POSIX.

應用舉例


例一:
#include
#include
#pragma comment(linker,"/subsystem:windows /entry:mainCRTStartup") //設置連接器選項參數,即設置入口
int main()
int n;
char b,a="字元串長度為:";
char *s="ABCDEFG12345";
n=strlen(s);//測量字元串長度
itoa(n,b,10);//將整數轉換為字元串,以十進位方式
strcat(a,b);//連接字元串
MessageBoxA(NULL,TEXT(a),TEXT("提示信息:"),MB_OK);//調用windows API
return 0;
例二:
#include
#include
#pragma comment(lib,"winmm.lib") // 告訴連接器與這個庫連接,因為我們要播放多媒體聲音
#pragma comment(linker, "/subsystem:windows /entry:mainCRTStartup")// 設置連接器選項
int main()
PlaySound("SystemStart",NULL, SND_ALIAS|SND_SYNC);//播放聲音
Sleep(50);//執行暫停50毫秒,windows下參數為毫秒,想要暫停一秒:Sleep(1000);linux下參數為秒!
PlaySound("SystemStart",NULL, SND_ALIAS|SND_SYNC);
Sleep(50);
PlaySound("SystemStart",NULL, SND_ALIAS|SND_SYNC);
Sleep(50);
return 0;