先看一个单文件,直接写Makefile的例子minimal.cpp(《Cross-Platform GUI Programmingwith wxWidgets》上的)。
Makefile如下
其中 `wx-config --cxxflags`和 `wx-config --libs`是关键。它们产生编译的参数。Makefile写好后,直接make就产生了可执行的minimal。
接下来在看一个用auto*工具编译多文件。有四个文件MyApp.h,MyApp.cpp,MyFrame.h,MyFrame.cpp(就是把第一个例子拆成四个文件)。
首先需要修改configure.in。其中,粗体部分是特别需要注意的。结合Makefile.am,你也许会问:为什么WX_LIBS不能像 WX_CXXFLAGS那样写在Makefile.am中?因为如果放在Makefile.am中,automake会认为“--libs”是给编译器的参数。如果放在configure.in中,Makefile.am中的WX_LIBS就是`wx-config --libs`的输出了。automake会识别 AC_SUBST 将它的参数读到Makefile.in中。
MyApp_LDADD是连接时需要的参数。Makefile.am:
- #include <wx/wx.h>
- #include <wx/app.h>
- #include <wx/frame.h>
- #include <wx/string.h>
- // Declare the application class
- class MyApp : public wxApp
- {
- public:
- // Called on application startup
- virtual bool OnInit();
- };
- // Declare our main frame class
- class MyFrame : public wxFrame
- {
- public:
- // Constructor
- MyFrame(const wxString& title);
- // Event handlers
- void OnQuit(wxCommandEvent& event);
- void OnAbout(wxCommandEvent& event);
- private:
- // This class handles events
- DECLARE_EVENT_TABLE()
- };
- // Implements MyApp& GetApp()
- //DECLARE_APP(MyApp)
- // Give wxWidgets the means to create a MyApp object
- IMPLEMENT_APP(MyApp)
- // Initialize the application
- bool MyApp::OnInit()
- {
- // Create the main application window
- //MyFrame *frame = new MyFrame(wxT(“Minimal wxWidgets App”));
- MyFrame* frame = new MyFrame(_T(""));
- // Show it
- frame->Show(true);
- // Start the event loop
- return true;
- }
- // Event table for MyFrame
- BEGIN_EVENT_TABLE(MyFrame, wxFrame)
- EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
- EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
- END_EVENT_TABLE()
- void MyFrame::OnAbout(wxCommandEvent& event)
- {
- wxString msg;
- msg.Printf(_T("Hello and welcome to %s"),
- wxVERSION_STRING);
- wxMessageBox(msg, _T("About Minimal"), wxOK | wxICON_INFORMATION, this);
- }
- void MyFrame::OnQuit(wxCommandEvent& event)
- {
- // Destroy the frame
- Close();
- }
- //#include “mondrian.xpm”
- MyFrame::MyFrame(const wxString& title)
- : wxFrame(NULL, wxID_ANY, title)
- {
- // Set the frame icon
- //SetIcon(wxIcon(mondrian_xpm));
- // Create a menu bar
- wxMenu *fileMenu = new wxMenu;
- // The “About” item should be in the help menu
- wxMenu *helpMenu = new wxMenu;
- helpMenu->Append(wxID_ABOUT, _T("&About.../tF1"),
- _T("Show about dialog"));
- fileMenu->Append(wxID_EXIT, _T("E&xit/tAlt-X"),
- _T("Quit this program"));
- // Now append the freshly created menu to the menu bar...
- wxMenuBar *menuBar = new wxMenuBar();
- menuBar->Append(fileMenu, _T("&File"));
- menuBar->Append(helpMenu, _T("&Help"));
- // ... and attach this menu bar to the frame
- SetMenuBar(menuBar);
- // Create a status bar just for fun
- CreateStatusBar(2);
- SetStatusText(_T("Welcome to wxWidgets!"));
- }
Makefile如下
- CXX = $(shell wx-config --cxx)
- PROGRAM = minimal
- OBJECTS = $(PROGRAM).o
- # implementation
- .SUFFIXES: .o .cpp
- .cpp.o :
- $(CXX) -c `wx-config --cxxflags` -o $@ $<
- all: $(PROGRAM)
- $(PROGRAM): $(OBJECTS)
- $(CXX) -o $(PROGRAM) $(OBJECTS) `wx-config --libs`
- clean:
- rm -f *.o $(PROGRAM)
其中 `wx-config --cxxflags`和 `wx-config --libs`是关键。它们产生编译的参数。Makefile写好后,直接make就产生了可执行的minimal。
接下来在看一个用auto*工具编译多文件。有四个文件MyApp.h,MyApp.cpp,MyFrame.h,MyFrame.cpp(就是把第一个例子拆成四个文件)。
首先需要修改configure.in。其中,粗体部分是特别需要注意的。结合Makefile.am,你也许会问:为什么WX_LIBS不能像 WX_CXXFLAGS那样写在Makefile.am中?因为如果放在Makefile.am中,automake会认为“--libs”是给编译器的参数。如果放在configure.in中,Makefile.am中的WX_LIBS就是`wx-config --libs`的输出了。automake会识别 AC_SUBST 将它的参数读到Makefile.in中。
- # -*- Autoconf -*-
- # Process this file with autoconf to produce a configure script.
- AC_PREREQ(2.61)
- AC_INIT(MyApp, 1.0, cszzys@gmail.com)
- #AM_CONFIG_HEADER(config.h)
- AM_INIT_AUTOMAKE(MyApp,1.0)
- # Checks for programs.
- AC_PROG_CXX
- AC_PROG_CC
- # Checks for libraries.
- WXCONFIG=wx-config
- WX_LIBS=`$WXCONFIG --libs`
- AC_SUBST (WX_LIBS)
- # Checks for header files.
- # Checks for typedefs, structures, and compiler characteristics.
- AC_HEADER_STDBOOL
- AC_C_CONST
- # Checks for library functions.
- AC_CONFIG_FILES([Makefile])
- AC_OUTPUT
MyApp_LDADD是连接时需要的参数。Makefile.am:
- AUTOMAKE_OPTIONS=foreign
- bin_PROGRAMS = MyApp
- MyApp_SOURCES = MyApp.h MyApp.cpp /
- MyFrame.h MyFrame.cpp
- WX_CXXFLAGS = `wx-config --cxxflags`
- CXXFLAGS = $(WX_LIBS) $(WX_CXXFLAGS)
- MyApp_LDADD = $(WX_LIBS)