wxWidgets 学习笔记 之 1编译
转载请注明出处:http://www.cnblogs.com/cgblogs/archive/2013/01/14/2860132.html
准备学习wxWidgets,之前也有过一些接触,但是仅有少许的一些了解。
现在认真学习一下,把学习过程记录下来。
对于wxWidgets的介绍这里就不说了,可以到网上搜索下。
我使用版本:v2.9.4
wxWidgets在windows下最常用的两种编译方式:使用gcc或者vc。为了方便我使用了vs2010。用vc的话也有两种方式可以编译,一是通过wxWidgets中给的工程文件在vs2010中编译,二是通过wxWidgets中的makefile文件在命令行下编译。
由于使用makefile编译灵活性更好,所以我是通过makefile来进行的。
进入目录 .\wxWidgets-2.9.4\build\msw 可以看到这两个文件makefile.vc、config.vc
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# ========================================================================= # This configuration file was generated by # Bakefile 0.2.9 (http://www.bakefile.org) # Beware that all changes made to this file will be overwritten next # time you run Bakefile! # ========================================================================= # ------------------------------------------------------------------------- # These are configurable options: # ------------------------------------------------------------------------- # C compiler CC = cl # C++ compiler CXX = cl # Standard flags for CC CFLAGS = # Standard flags for C++ CXXFLAGS = # Standard preprocessor flags (common for CC and CXX) CPPFLAGS = # Standard linker flags LDFLAGS = # The C preprocessor CPP = $(CC) /EP /nologo # What type of library to build? [0,1] SHARED = 0 # Build wxUniversal instead of native port? [0,1] WXUNIV = 0 # Compile Unicode build of wxWidgets? [0,1] UNICODE = 1 # Use MSLU library when building Unicode version. [0,1] MSLU = 0 # Type of compiled binaries [debug,release] BUILD = debug # The target processor architecture must be specified when it is not X86. # This does not affect the compiler output, so you still need to make sure # your environment is set up appropriately with the correct compiler in the # PATH. Rather it affects some options passed to some of the common build # utilities such as the resource compiler and the linker. # # Accepted values: IA64, X64 # (AMD64 accepted as synonym for X64 but should not be used any more). TARGET_CPU = $(CPU) # Should debugging info be included in the executables? The default value # "default" means that debug info will be included if BUILD=debug # and not included if BUILD=release. [0,1,default] DEBUG_INFO = 1 # Value of wxDEBUG_LEVEL. The default value is the same as 1 and means that all # but expensive assert checks are enabled, use 0 to completely remove debugging # code. [0,1,default] DEBUG_FLAG = 1 # Link against debug (e.g. msvcrtd.dll) or release (msvcrt.dll) RTL? # Default is to use debug CRT if and only if BUILD==debug. [0,1,default] DEBUG_RUNTIME_LIBS = default # Multiple libraries or single huge monolithic one? [0,1] MONOLITHIC = 0 # Build GUI libraries? [0,1] USE_GUI = 1 # Build wxHTML library (USE_GUI must be 1)? [0,1] USE_HTML = 1 # Build wxWebView library (USE_GUI must be 1)? [0,1] USE_WEBVIEW = 1 # Build multimedia library (USE_GUI must be 1)? [0,1] USE_MEDIA = 1 # Build wxXRC library (USE_GUI must be 1)? [0,1] USE_XRC = 1 # Build wxAUI library (USE_GUI must be 1)? [0,1] USE_AUI = 1 # Build wxRibbon library (USE_GUI must be 1)? [0,1] USE_RIBBON = 1 # Build wxPropertyGrid library (USE_GUI must be 1)? [0,1] USE_PROPGRID = 1 # Build wxRichTextCtrl library (USE_GUI must be 1)? [0,1] USE_RICHTEXT = 1 # Build wxStyledTextCtrl library (USE_GUI must be 1)? [0,1] USE_STC = 1 # Build OpenGL canvas library (USE_GUI must be 1)? [0,1] USE_OPENGL = 1 # Build quality assurance classes library (USE_GUI must be 1)? [0,1] USE_QA = 1 # Enable exceptions in compiled code. [0,1] USE_EXCEPTIONS = 1 # Enable run-time type information (RTTI) in compiled code. [0,1] USE_RTTI = 1 # Enable threading in compiled code. [0,1] USE_THREADS = 1 # Enable wxCairoContext for platforms other than Linux/GTK. [0,1] USE_CAIRO = 0 # Is this official build by wxWidgets developers? [0,1] OFFICIAL_BUILD = 0 # Use this to name your customized DLLs differently VENDOR = custom # WX_FLAVOUR = # WX_LIB_FLAVOUR = # Name of your custom configuration. This affects directory # where object files are stored as well as the location of # compiled .lib files and setup.h under the lib/ toplevel directory. CFG = # Compiler flags needed to compile test suite in tests directory. If you want # to run the tests, set it so that the compiler can find CppUnit headers. CPPUNIT_CFLAGS = # Linker flags needed to link test suite in tests directory. If you want # to run the tests, include CppUnit library here. CPPUNIT_LIBS = # Version of C runtime library to use. You can change this to # static if SHARED=0, but it is highly recommended to not do # it if SHARED=1 unless you know what you are doing. [dynamic,static] RUNTIME_LIBS = dynamic
这个文件是关于编译有关的一些配置,可以仔细看看,都有解释,这里介绍几个我用到的:
#使用哪一种 lib库 编译,0:动态链接库,1:静态链接库
# What type of library to build? [0,1]
SHARED = 0
#是否使用Uincode,0:不使用,1使用
# Compile Unicode build of wxWidgets? [0,1]
UNICODE = 1
#发布版本还是调试版本,debug:调试版本,release:发布版本
# Type of compiled binaries [debug,release]
BUILD = debug
#生成一个文件还是多个,0:一个文件,1:多个
# Multiple libraries or single huge monolithic one? [0,1]
MONOLITHIC = 0
makefile.vc 是编译文件。
命令行的编译命令:nmake -f makefile.vc UNICODE=? MONOLITHIC=? BUILD=? SHARED=?
把?替换为你想要的编译方式就可以了。
编译的结果会在 .\wxWidgets-2.9.4\lib 目录下出现,文件名中Unicode版本会有一个字母"u",debug版本会有字母"d"。
到此,编译就完成了,下面来一个测试的例子,我使用的是"使用wxWidgets进行跨平台程序开发"一书的第一个程序。
新建一个空的Win32 Application程序,添加一个cpp文件:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include "wx/wx.h" 2 3 class MyApp : public wxApp 4 { 5 public: 6 virtual bool OnInit(); 7 }; 8 9 class MyFrame : public wxFrame 10 { 11 public: 12 MyFrame(const wxString& title); 13 14 void OnQuit(wxCommandEvent& event); 15 void OnAbout(wxCommandEvent& event); 16 17 private: 18 DECLARE_EVENT_TABLE() 19 }; 20 21 DECLARE_APP(MyApp); 22 23 IMPLEMENT_APP(MyApp); 24 25 bool MyApp::OnInit() 26 { 27 MyFrame * frame = new MyFrame(wxT("Minimal wxWidgets App")); 28 frame->Show(true); 29 30 return true; 31 } 32 33 BEGIN_EVENT_TABLE(MyFrame, wxFrame) 34 EVT_MENU(wxID_ABOUT, MyFrame::OnAbout) 35 EVT_MENU(wxID_EXIT, MyFrame::OnQuit) 36 END_EVENT_TABLE() 37 38 void MyFrame::OnAbout(wxCommandEvent& event) 39 { 40 wxString msg; 41 msg.Printf(wxT("Hello and welcome to %s"), wxVERSION_STRING); 42 43 wxMessageBox(msg, wxT("About Minimal"), wxOK|wxICON_INFORMATION, this); 44 } 45 46 void MyFrame::OnQuit(wxCommandEvent& event) 47 { 48 Close(); 49 } 50 51 MyFrame::MyFrame(const wxString& title) 52 : wxFrame(NULL, wxID_ANY, title) 53 { 54 wxMenu *fileMenu = new wxMenu; 55 56 wxMenu *helpMenu = new wxMenu; 57 helpMenu->Append(wxID_ABOUT, wxT("&About...\tF1"), 58 wxT("Show about dialog")); 59 fileMenu->Append(wxID_EXIT, wxT("E&xitAlt-X"), 60 wxT("Quit this program")); 61 62 wxMenuBar *menuBar = new wxMenuBar(); 63 menuBar->Append(fileMenu, wxT("&File")); 64 menuBar->Append(helpMenu, wxT("&Help")); 65 66 SetMenuBar(menuBar); 67 }
接下来是环境的配置,首先需要引入wxWidgets的头文件包含目录,
1. .\wxWidgets-2.9.4\include; //这个文件目录包含了wxWidgets所需的所有头文件。
2. .\wxWidgets-2.9.4\lib\vc_dll\mswu; //由于我使用的是动态链接库,并且现在配置的是release版本
".\wxWidgets-2.9.4\lib\vc_dll\mswu"这个目录是和使用版本不同而不同的,如debug就是".\wxWidgets-2.9.4\lib\vc_dll\mswud"。这个目录有个头文件"wx/setup.h",里面描述了你所使用的组件,如是否使用了GUI,由wxUSE_GUI来控制。
接下来,如果是使用动态链接库版本的话,需要在"预处理器定义"添加一个宏定义"WXUSINGDLL"
接着添加附加库目录,也就是使用的lib库的目录,".\wxWidgets-2.9.4\lib\vc_dll"。
还需要加上你使用到的lib,我使用的是单文件Unicode的debug版本,所以加上“wxmsw29ud.lib”就可以了。当然release版本就加上"wxmsw29u.lib"。
没什么问题的话编译就可以通过了。运行时需要将“wxmsw294u_vc_custom.dll”和“wxmsw294ud_vc_custom.dll”复制到相应的输出目录。
来看看运行效果: