Getting smart-completion for wxWidgets using CEDET

This post shows how CEDET can be configured to show code-completion for classes in wxWidgets library. Since wxWidgets uses preprocessor macros heavily, configuring smart-completion is bit tricky. I haven’t got successful completion on my first attempt and got everything worked nicely after having few discussions on the CEDET mailing list. Thanks to all who helped.

This post assumes that you have a CVS version of CEDET installed and configured correctly.

Consider the following “Hello world” sample from wxWidgets.

 * hworld.cpp
 * Hello world sample by Robert Roebling

#include "wx/wx.h" 

class MyApp: public wxApp
    virtual bool OnInit();

class MyFrame: public wxFrame

    MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);

    void OnQuit(wxCommandEvent& event);
    void OnAbout(wxCommandEvent& event);


    ID_Quit = 1,

    EVT_MENU(ID_Quit, MyFrame::OnQuit)
    EVT_MENU(ID_About, MyFrame::OnAbout)


bool MyApp::OnInit()
    MyFrame *frame = new MyFrame( _T("Hello World"), wxPoint(50,50), wxSize(450,340) );

    return TRUE;

MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
: wxFrame((wxFrame *)NULL, -1, title, pos, size)
    wxMenu *menuFile = new wxMenu;
    menuFile->Append( ID_About, _T("&About...") );
    menuFile->Append( ID_Quit, _T("E&xit") );

    wxMenuBar *menuBar = new wxMenuBar;
    menuBar->Append( menuFile, _T("&File") );

    SetMenuBar( menuBar );

    SetStatusText( _T("Welcome to wxWindows!") );

void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))

void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
    wxMessageBox(_T("This is a wxWindows Hello world sample"),
        _T("About Hello World"), wxOK | wxICON_INFORMATION, this);

Here is the CEDET initialization file looks like:

;; Handles the CEDET configuration
(load-file "~/emacs/cedet/common/cedet.el")


(require 'semantic-ia)
(require 'semantic-gcc)

;; custom key bindings for smart completion
(global-set-key "\C-x\C-m" 'semantic-ia-complete-symbol-menu)

;; Loading EDE package
(require 'ede)
(global-ede-mode t)

;; Function to load all include files in the specified directory
(defun DE-imply-includes-in-directory (dir)
  "Add all header files in DIR to `semanticdb-implied-include-tags'."
  (let ((files (directory-files dir t "^.+\\.h[hp]*$" t)))
    (defvar-mode-local c++-mode semanticdb-implied-include-tags
      (mapcar (lambda (header)
                 :filename header))

initialization file for wxWidgets:

;; include files for wxwidgets
(semantic-add-system-include "/YourWxWidgetsPath/include" 'c++-mode)

;; preprocessor macro
(add-to-list 'semantic-lex-c-preprocessor-symbol-map '("WXDLLEXPORT" . ""))
(add-to-list 'semantic-lex-c-preprocessor-symbol-map '("WXDLLIMPEXP_CORE" . ""))
(add-to-list 'semantic-lex-c-preprocessor-symbol-map '("WXDLLIMPEXP_FWD_CORE" . ""))
(add-to-list 'semantic-lex-c-preprocessor-symbol-map '("WXDLLIMPEXP_BASE" . ""))
(add-to-list 'semantic-lex-c-preprocessor-symbol-map '("WXDLLIMPEXP_FWD_BASE" . ""))
(add-to-list 'semantic-lex-c-preprocessor-symbol-map '("WXDLLIMPEXP_FWD_XML" . ""))
(add-to-list 'semantic-lex-c-preprocessor-symbol-map '("WXDLLIMPEXP_ADV" . ""))

;; Implicitly adding the header files to get completion working
;; Adding all include files in gtk directory
(DE-imply-includes-in-directory "/YourWxWidgetsPath/include/wx/gtk")

DE-imply-includes-in-directory finds all the include files in the specified directory and add to semanticdb-implied-include-tags variable. Thanks to David Engster for the function.

Load the above files in your emacs initialization file and you will get code-completion for wxWidgets classes. Here is a screen-shot from my system.

Smart completion works for wxWidgets with CEDET

Smart completion works for wxWidgets with CEDET

If you haven’t tried CEDET yet, give it a try. It is way cool!

About these ads

3 thoughts on “Getting smart-completion for wxWidgets using CEDET

  1. Good Example I have been looking for it. I was almost ready to try etags when I found your blog post
    I tried your example it works fine.
    Then I tried my own code and I got “Possible metatype recursion for ‘wxStaticText’”

    • Thanks for reading my blog. I haven’t faced any issues with wxStaticText so far. Can you email your code to navaneethkn[at]gmail? I will try to reproduce the issue.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s