Lowering build size

左心房为你撑大大i 提交于 2019-12-02 17:52:53

问题


Is there any way I can lower my build size when I am using static linking for the C++ runtime libraries in VS2013?

My original file was just 15kb, but I couldn't run it on my other computers without a missing .dll message popping up. I decided to use static linking and now the size is ~100kb.

I am using the Windows.h header and WinAPI functions.


回答1:


If you're only going to display a MessageBox, you don't need to link to any static libraries.

#include <windows.h>

void entry(void) {    
    MessageBox(NULL, "Hello, World!", "", MB_OK);
    ExitProcess(0);
}

Compile and link using VC++2013, with the following command line:

cl /O1 /GS- hello32.c /link /nodefaultlib /entry:entry /subsystem:windows user32.lib kernel32.lib

At least for me (with VC++ 2013) that produces an executable of 2560 bytes (that depends only on core Windows DLLs, so it shouldn't require anything extra to run on even the most bare-bones system).




回答2:


As your main intention is to keep size in control, dll is way to go.

As far as your missing dll is concerned it can be addressed. If you are using standard libary missing error then its good idea to install redistributable on machine where you are running application.

Redistributable for VS2013




回答3:


If you don't use MFC etc. you can

#define WIN32_LEAN_AND_MEAN
#include <Windows.h>  

to reduce size of your code



来源:https://stackoverflow.com/questions/20943392/lowering-build-size

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!