windows下編寫綠色軟體的設想

2021-10-08 23:01:46 字數 4600 閱讀 3429

可以看出它依賴的都是windows系統庫。我們再看看一些需要安裝包的軟體如adobe audition 3.0的庫依賴情況,如下圖:

可以看到除了依賴windows系統庫,還依賴於vc的執行時庫。我們知道vc的執行時庫是分不同版本的,如上圖的audition 3.0依賴的就是vc 2003的執行時庫。

比較二者我們可以得出乙個結論:是否依賴於vc執行時庫是綠色軟體和非綠色軟體的一大區別。

那麼問題來了:如何開發綠色軟體。這個問題其實應該是如何在編譯軟體時去除軟體對vc執行時庫的依賴。我曾經試驗過使用vs編譯vc工程是無法去除vc執行時庫的依賴的。我摸索了一種辦法,就是使用makefile編譯c/c++**是可以去除對vc執行時庫的依賴。下面是一段示例makefile指令碼:

#microsoft sdks因個人安裝的vs路徑的不同而不同

!include #設定編譯標記,初始化為false

cfgset = false

run_clean = false

!if "$(option)"== "1"

cfgset = true

configuration = release

platform = x64

#定義release版本的預處理器

cc_option = -d_win64 -d_ndebug -d_windows

target_machine = x64

!else if "$(option)"== "2"

cfgset = true

configuration = debug

platform = x64

#定義debug版本的編譯選項

cc_option = -d_win64 -d_debug -d_windows /mdd

target_machine = x64

!else if "$(option)"== "3"

cfgset = true

configuration = release

platform = win32

cc_option = -d_win32 -d_ndebug -d_windows

target_machine = x86

!else if "$(option)"== "4"

cfgset = true

configuration = debug

platform = win32

cc_option = -d_win32 -d_debug -d_windows /mdd

target_machine = x86

!else if "$(option)"=="5"

cfgset=true

run_clean=true

!endif

cc = $(cc_option)

# 提示用法

!if "$(cfgset)"== "false"

!message usage: nmake /f makefile.vc option=1 or 2 or 3 or 4 or 5

!message

!message where is one of:

!message - option=1 - build x64 release version

!message - option=2 - build x64 debug version

!message - option=3 - build x86 release version

!message - option=4 - build x86 debug version

!message - option=5 - clear output file

!message

!message

!error please choose a valid configuration instead"

!endif

#假如執行清理命令

!if "$(run_clean)" == "true"

clean:

if exist "..\..\intdir\debug_x64\filesplitandmerge" (del "..\..\intdir\debug_x64\filesplitandmerge\*.obj") else echo debug_x64 not exist

if exist "..\..\intdir\debug_win32\filesplitandmerge" (del "..\..\intdir\debug_win32\filesplitandmerge\*.obj") else echo debug_win32 not exist

if exist "..\..\intdir\release_x64\filesplitandmerge" (del "..\..\intdir\release_x64\filesplitandmerge\*.obj") else echo release_x64 not exist

if exist "..\..\intdir\release_win32\filesplitandmerge" (del "..\..\intdir\release_win32\filesplitandmerge\*.obj") else echo release_win32 not exist

!else

#設定輸出資料夾和臨時目錄資料夾

outdir =..\..\outdir\$(configuration)_$(platform)

indir =..\..\intdir\$(configuration)_$(platform)\filesplitandmerge

#設定所有輸出

all: $(indir) $(outdir) $(outdir)\filesplitandmerge.exe

#假如不存在$(indir)資料夾,就建立它

$(indir) :

if not exist "$(indir)" mkdir $(indir)

#假如不存在$(outdir)資料夾,就建立它

$(outdir) :

if not exist "$(outdir)" mkdir $(outdir)

#編譯資源指令碼

$(indir)\resource.res:.\resource.rc

rc.exe /n /v /fo $(indir)\resource.res .\resource.rc

#編譯原始碼

$(indir)\splitfile.obj:.\splitfile.c

cl -c $(cc) /fo"$(indir)\\" /fd"$(indir)\\" .\splitfile.c

$(indir)\mergefile.obj:.\mergefile.c

cl -c $(cc) /fo"$(indir)\\" /fd"$(indir)\\" .\mergefile.c

$(indir)\progress.obj:.\progress.c

cl -c $(cc) /fo"$(indir)\\" /fd"$(indir)\\" .\progress.c

$(indir)\maindlg.obj:.\maindlg.c $(indir)\splitfile.obj $(indir)\mergefile.obj $(indir)\resource.res $(indir)\progress.obj

cl -c $(cc) /fo"$(indir)\\" /fd"$(indir)\\" .\maindlg.c

#鏈結$(outdir)\filesplitandmerge.exe: $(indir)\maindlg.obj

link /machine:$(target_machine) /incremental:yes /nologo /subsystem:windows /out:$(outdir)\filesplitandmerge.exe $(indir)\splitfile.obj $(indir)\mergefile.obj $(indir)\progress.obj $(indir)\maindlg.obj $(indir)\resource.res kernel32.lib user32.lib gdi32.lib comdlg32.lib shell32.lib

!endif

如果我們再將思路擴充套件一下,我們可以組裝乙個綠色版的vs。乙個精簡版的vs實際上是由windows sdk、**編輯器、資源指令碼編輯器、編譯器、鏈結器和偵錯程式組成的。windows sdk實際上是一堆標頭檔案和庫檔案,可以單獨抽取出來,**編輯器可以用文字編輯器,資源指令碼編輯器可以用resed (resource editor)代替,編譯器、鏈結器可以從安裝版的vs中抽取出來,偵錯程式可以用windbg代替vs的偵錯程式。上面提到的各種替代品都是綠色軟體。文字編輯器用來編寫**,使用makefile編譯**,使用windbg除錯**,這樣就不用安裝龐大的visualstudio,每次編寫**時也不用啟動龐大的visualstudio。

Linux中安裝綠色軟體的方法

一 簡介 我們平時安裝軟體時,想要把乙個可直接執行的軟體及其依賴庫copy到linux中的某個資料夾下。但是為了快速方便地執行它,不想每次都進入此目錄中執行。解決的方法是向path中相關的路徑下投放軟鏈結檔案。二 相關命令 ln s 目標可執行程式 目標地點 如 ln s usr local dot...

PDF轉換成word的綠色軟體推薦

隨著pdf格式文件的盛行,使用它辦公的人也越來越多,pdf如何轉換成word文件,如何在word文件中進行編輯 修改已經成為大家的需要。眾所周知,word有著良好的編輯效果,適合列印 加密等特點,因此很多的大型企業和單位都願意將公司的pdf檔案轉成為word檔案,這樣我們不僅可以方便閱讀檔案內容。轉...

綠色軟體聯盟 防毒軟體已經進入空前弱智與混沌狀態

原文 關鍵字 綠盟,軟體,安全,防毒,病毒,報毒,誤報,木馬 根據的反饋,現在很多軟體有 病毒 綠盟相信沒有乙個把關和軟體安全做的比綠盟更用心,反饋比綠盟更多,更熱烈的。根據反饋,現在真正 安全 的軟體基本上沒幾個了。1,防毒軟體和病毒警報系統敏感化 卡巴,誤報能力相當強,敏感過度了,瑞星也有跟風的...