问题
I have written a minimal wxWidgets application:
stdafx.h
#define wxNO_REGEX_LIB
#define wxNO_XML_LIB
#define wxNO_NET_LIB
#define wxNO_EXPAT_LIB
#define wxNO_JPEG_LIB
#define wxNO_PNG_LIB
#define wxNO_TIFF_LIB
#define wxNO_ZLIB_LIB
#define wxNO_ADV_LIB
#define wxNO_HTML_LIB
#define wxNO_GL_LIB
#define wxNO_QA_LIB
#define wxNO_XRC_LIB
#define wxNO_AUI_LIB
#define wxNO_PROPGRID_LIB
#define wxNO_RIBBON_LIB
#define wxNO_RICHTEXT_LIB
#define wxNO_MEDIA_LIB
#define wxNO_STC_LIB
#include <wx/wxprec.h>
Minimal.cpp
#include "stdafx.h"
#include <memory>
#include <wx/wx.h>
class Minimal : public wxApp
{
public:
virtual bool OnInit();
};
IMPLEMENT_APP(Minimal)
DECLARE_APP(Minimal)
class MinimalFrame : public wxFrame
{
DECLARE_EVENT_TABLE()
public:
MinimalFrame(const wxString& title);
void OnQuit(wxCommandEvent& e);
void OnAbout(wxCommandEvent& e);
};
BEGIN_EVENT_TABLE(MinimalFrame, wxFrame)
EVT_MENU(wxID_ABOUT, MinimalFrame::OnAbout)
EVT_MENU(wxID_EXIT, MinimalFrame::OnQuit)
END_EVENT_TABLE()
MinimalFrame::MinimalFrame(const wxString& title)
: wxFrame(0, wxID_ANY, title)
{
std::auto_ptr<wxMenu> fileMenu(new wxMenu);
fileMenu->Append(wxID_EXIT, L"E&xit\tAlt-X",
L"Terminate the Minimal Example.");
std::auto_ptr<wxMenu> helpMenu(new wxMenu);
helpMenu->Append(wxID_ABOUT, L"&About\tF1",
L"Show the about dialog box.");
std::auto_ptr<wxMenuBar> bar(new wxMenuBar);
bar->Append(fileMenu.get(), L"&File");
fileMenu.release();
bar->Append(helpMenu.get(), L"&Help");
helpMenu.release();
SetMenuBar(bar.get());
bar.release();
CreateStatusBar(2);
SetStatusText(L"Welcome to wxWidgets!");
}
void MinimalFrame::OnAbout(wxCommandEvent& e)
{
wxMessageBox(L"Some text about me!", L"About", wxOK, this);
}
void MinimalFrame::OnQuit(wxCommandEvent& e)
{
Close();
}
bool Minimal::OnInit()
{
std::auto_ptr<MinimalFrame> mainFrame(
new MinimalFrame(L"Minimal wxWidgets Application"));
mainFrame->Show();
mainFrame.release();
return true;
}
This minimal program weighs in at 2.4MB! (Executable compression drops this to half a MB or so but that's still HUGE!) (I must statically link because this application needs to be single-binary-xcopy-deployed, so both the C runtime and wxWidgets itself are set for static linking)
Any tips on cutting this down? (I'm using Microsoft Visual Studio 2010)
回答1:
You can additionally set MSVC to compile for small binary size (but this might impact performance of course) by using the flag /01
. See here. Also remember that wxWidgets will need to be compiled as such for this to have any impact.
And I think that 2.4 MB for a statically linked app is pretty decent IMHO...
UPDATE: the wxWidgets wiki on executable size might bring some comfort...
回答2:
You should disable all the features you don't need by setting most of wxUSE_XXX
in include/wx/msw/setup.h
to 0. But even so, you still won't have anything near 90KB.
wxWidgets was simply never optimized for size so if this is your primary concern it's probably unsuitable for you (unless you want to do some work on this yourself, of course, which would be very welcome). In practice however it's never been a problem except, maybe, for a vanishingly few people.
回答3:
Try to add the linker switches as follows:
/FILEALIGN:512 /OPT:REF /OPT:ICF /INCREMENTAL:NO
These would reduce the size of your executable.
For a simple hello world program in C++, if you wish to compile from the command line, enter the following commands:
cl /MT /O1 main.cpp /link /FILEALIGN:512 /OPT:REF /OPT:ICF /INCREMENTAL:NO /OUT:test.exe
If you try to check the executable, you will notice that the size is now much smaller.
来源:https://stackoverflow.com/questions/4568364/any-tips-on-reducing-wxwidgets-application-code-size