如何用auto*工具编译wxWidgets程序

先看一个单文件,直接写Makefile的例子minimal.cpp(《Cross-Platform GUI Programmingwith wxWidgets》上的)。
  1. #include <wx/wx.h>
  2. #include <wx/app.h>
  3. #include <wx/frame.h>
  4. #include <wx/string.h>
  5. // Declare the application class
  6. class MyApp : public wxApp
  7. {
  8. public:
  9. // Called on application startup
  10. virtual bool OnInit();
  11. };
  12. // Declare our main frame class
  13. class MyFrame : public wxFrame
  14. {
  15. public:
  16. // Constructor
  17. MyFrame(const wxString& title);
  18. // Event handlers
  19. void OnQuit(wxCommandEvent& event);
  20. void OnAbout(wxCommandEvent& event);
  21. private:
  22. // This class handles events
  23. DECLARE_EVENT_TABLE()
  24. };
  25. // Implements MyApp& GetApp()
  26. //DECLARE_APP(MyApp)
  27. // Give wxWidgets the means to create a MyApp object
  28. IMPLEMENT_APP(MyApp)
  29. // Initialize the application
  30. bool MyApp::OnInit()
  31. {
  32. // Create the main application window
  33. //MyFrame *frame = new MyFrame(wxT(“Minimal wxWidgets App”));
  34. MyFrame* frame = new MyFrame(_T(""));
  35. // Show it
  36. frame->Show(true);
  37. // Start the event loop
  38. return true;
  39. }
  40. // Event table for MyFrame
  41. BEGIN_EVENT_TABLE(MyFrame, wxFrame)
  42. EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
  43. EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
  44. END_EVENT_TABLE()
  45. void MyFrame::OnAbout(wxCommandEvent& event)
  46. {
  47. wxString msg;
  48. msg.Printf(_T("Hello and welcome to %s"),
  49. wxVERSION_STRING);
  50. wxMessageBox(msg, _T("About Minimal"), wxOK | wxICON_INFORMATION, this);
  51. }
  52. void MyFrame::OnQuit(wxCommandEvent& event)
  53. {
  54. // Destroy the frame
  55. Close();
  56. }
  57. //#include “mondrian.xpm”
  58. MyFrame::MyFrame(const wxString& title)
  59. : wxFrame(NULL, wxID_ANY, title)
  60. {
  61. // Set the frame icon
  62. //SetIcon(wxIcon(mondrian_xpm));
  63. // Create a menu bar
  64. wxMenu *fileMenu = new wxMenu;
  65. // The “About” item should be in the help menu
  66. wxMenu *helpMenu = new wxMenu;
  67. helpMenu->Append(wxID_ABOUT, _T("&About.../tF1"),
  68. _T("Show about dialog"));
  69. fileMenu->Append(wxID_EXIT, _T("E&xit/tAlt-X"),
  70. _T("Quit this program"));
  71. // Now append the freshly created menu to the menu bar...
  72. wxMenuBar *menuBar = new wxMenuBar();
  73. menuBar->Append(fileMenu, _T("&File"));
  74. menuBar->Append(helpMenu, _T("&Help"));
  75. // ... and attach this menu bar to the frame
  76. SetMenuBar(menuBar);
  77. // Create a status bar just for fun
  78. CreateStatusBar(2);
  79. SetStatusText(_T("Welcome to wxWidgets!"));
  80. }

Makefile如下
  1. CXX = $(shell wx-config --cxx)
  2.  
  3. PROGRAM = minimal
  4.  
  5. OBJECTS = $(PROGRAM).o
  6.  
  7. # implementation
  8.  
  9. .SUFFIXES:      .o .cpp
  10.  
  11. .cpp.o :
  12.     $(CXX) -c `wx-config --cxxflags` -o $@ $<
  13.  
  14. all:    $(PROGRAM)
  15.  
  16. $(PROGRAM):     $(OBJECTS)
  17.     $(CXX) -o $(PROGRAM) $(OBJECTS) `wx-config --libs`
  18.  
  19. clean:
  20.     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中。

  1. #                                               -*- Autoconf -*-
  2. # Process this file with autoconf to produce a configure script.
  3. AC_PREREQ(2.61)
  4. AC_INIT(MyApp, 1.0, cszzys@gmail.com)
  5. #AM_CONFIG_HEADER(config.h)
  6. AM_INIT_AUTOMAKE(MyApp,1.0)
  7. # Checks for programs.
  8. AC_PROG_CXX
  9. AC_PROG_CC
  10. # Checks for libraries.
  11. WXCONFIG=wx-config
  12. WX_LIBS=`$WXCONFIG --libs`
  13. AC_SUBST (WX_LIBS)
  14. # Checks for header files.
  15. # Checks for typedefs, structures, and compiler characteristics.
  16. AC_HEADER_STDBOOL
  17. AC_C_CONST
  18. # Checks for library functions.
  19. AC_CONFIG_FILES([Makefile])
  20. AC_OUTPUT


MyApp_LDADD是连接时需要的参数。Makefile.am:
  1. AUTOMAKE_OPTIONS=foreign
  2. bin_PROGRAMS = MyApp
  3. MyApp_SOURCES = MyApp.h MyApp.cpp /
  4.           MyFrame.h MyFrame.cpp
  5. WX_CXXFLAGS = `wx-config --cxxflags`
  6. CXXFLAGS = $(WX_LIBS) $(WX_CXXFLAGS)
  7. MyApp_LDADD = $(WX_LIBS)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值