过大的可执行文件的大小
我创建一个简单的(世界,你好!)C++使用wxDev-C++ IDE用一个非常简单的GUI(仅适用于无元素的帧)应用程序(在Windows 7的)。
问题是输出.exe文件的该尺寸过大(2MB左右,当我编译使用MSVC10编译&更多的8MB,当我使用GCC编译&这些尺寸是最好的情况下!)。
我已经检查了很多可能性(例如排除调试信息或优化大小),但其中没有一个对于减小可执行文件大小没有帮助。
我强烈猜测存在链接问题,因为我看过一个demo application,它包含几乎所有的小部件,只有大约1MB的代码大小。
有什么想法?过大的可执行文件的大小
当你将整个内容打包到一个exe文件中时,你期望什么? (静态)
基于许多因素的编译.exe文件的大小。但是如果你将它编译为静态的,你创建的是最大的。
这里是例如一个wxWidgets应用程序。编译两次。一个用VC2010,另一个用minGW。
动态编译与VS2010的WinXP 127.488 KB
动态编译使用MinGW的gcc的WinXP 1.018.639 KB
你写道:about 2MB when I compile using MSVC10 compiler & more that 8MB when I use GCC compile & these sizes are best cases!.
只要我们可以看到任何make文件。或其他设置,这里很难提供帮助。
向上述结果,可以看出,有可能产生具有仅128.000 KB一个WX应用。
dtx02.cpp
#include "dtx02.h"
///////////////////////////////////////////////////////////////////////////
dxDialog::dxDialog(wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, long style) : wxDialog(parent, id, title, pos, size, style)
{
this->SetSizeHints(wxDefaultSize, wxDefaultSize);
wxBoxSizer* bSizer2;
bSizer2 = new wxBoxSizer(wxVERTICAL);
m_button1 = new wxButton(this, ID_SHOWMESSAGE, wxT("&Show a message"), wxDefaultPosition, wxDefaultSize, 0);
bSizer2->Add(m_button1, 0, wxALL, 5);
m_button2 = new wxButton(this, wxID_OK, wxT("&Close"), wxDefaultPosition, wxDefaultSize, 0);
bSizer2->Add(m_button2, 0, wxALL, 5);
m_notebook1 = new wxNotebook(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, 0);
m_panel1 = new wxPanel(m_notebook1, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL);
m_panel1->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE));
m_notebook1->AddPage(m_panel1, wxT("a page"), true);
m_panel2 = new wxPanel(m_notebook1, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL);
m_notebook1->AddPage(m_panel2, wxT("a page"), false);
bSizer2->Add(m_notebook1, 1, wxEXPAND | wxALL, 5);
this->SetSizer(bSizer2);
this->Layout();
this->Centre(wxBOTH);
// Connect Events
this->Connect(wxEVT_CLOSE_WINDOW, wxCloseEventHandler(dxDialog::OnClosex));
m_button1->Connect(wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(dxDialog::OnShowMessage), NULL, this);
m_button2->Connect(wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(dxDialog::OnClose), NULL, this);
}
dxDialog::~dxDialog()
{
// Disconnect Events
this->Disconnect(wxEVT_CLOSE_WINDOW, wxCloseEventHandler(dxDialog::OnClosex));
m_button1->Disconnect(wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(dxDialog::OnShowMessage), NULL, this);
m_button2->Disconnect(wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(dxDialog::OnClose), NULL, this);
}
dtx02App.cpp
#include "dtx02App.h"
#include "dtx02dxDialog.h"
IMPLEMENT_APP(dtx02App)
dtx02App::dtx02App()
{
}
dtx02App::~dtx02App()
{
}
bool dtx02App::OnInit()
{
dtx02dxDialog* dialog = new dtx02dxDialog((wxWindow*)NULL);
dialog ->Show();
SetTopWindow(dialog);
return true;
}
dtx02dxDialog.cpp
#include "dtx02dxDialog.h"
dtx02dxDialog::dtx02dxDialog(wxWindow* parent)
:
dxDialog(parent)
{
}
void dtx02dxDialog::OnClosex(wxCloseEvent& event)
{
wxTheApp->Exit();
}
void dtx02dxDialog::OnShowMessage(wxCommandEvent& event)
{
wxMessageBox(wxT("wxFormBuilder Tut"));
}
void dtx02dxDialog::OnClose(wxCommandEvent& event)
{
wxTheApp->Exit();
}
这里有您需要按照减少静态链接wxWidgets的大小的步骤使用mingw的应用程序:
1.Compile使用下列选项库:
-ffunction截面-fdata截面-Os
这将会把每个功能或数据项到自己的部分。
2。链接可执行文件到图书馆使用下列选项:
轮候册, - GC-部分-s
符号和未使用的功能和数据段将被删除。
使用这些选项,生成的可执行文件大小将约为3.3 MB。当然,这意味着你必须自己编译库,而不是使用预编译的二进制文件。我用下面的bat文件编译库:
cd C:\wxWidgets\build\msw
mingw32-make SHELL=CMD.exe -j4 -f makefile.gcc BUILD=release UNICODE=1 SHARED=0 clean
mingw32-make SHELL=CMD.exe -j4 -f makefile.gcc CPPFLAGS="-MD -MP -DHAVE_W32API_H -D__WXMSW__ -DNOPCH -DwxDEBUG_LEVEL=0 -DNDEBUG" CFLAGS="-mthreads -pipe -fmessage-length=0 -ffunction-sections -fdata-sections -fno-builtin -Os" CXXFLAGS="-mthreads -Wno-ctor-dtor-privacy -pipe -fmessage-length=0 -ffunction-sections -fdata-sections -fno-builtin -Os" LDFLAGS="-Wl,--subsystem,windows -Wl,--gc-sections -s -mthreads -mwindows" BUILD=release UNICODE=1 SHARED=0
此外,我建议你使用的代码::块或Qt Creator的替代wxDev。我个人使用Qt Creator。它有一个伟大的编辑器,QMake使事情变得更容易。
静态或动态链接? – 2013-04-06 17:33:05
当然是静态的 – 2013-04-06 17:35:28