Eclipse and wxWidgets

Setup Eclipse for wxWidgets with automatic debug/release build support and standalone Makefile

This guide is made for systems with Windows, TCD-GDM Compiler (MinGW GCC), wxWidgets 3.1 (SHARED=0), wxConfig and MSYS. But the procedure should be similar if not identical on other setups. Also make sure when working with wxWidgets in Eclipse, check that file encoding is set to utf8 for files (Preferences -> General -> Workspace).

Create a new project and add files

  1. Go to File -> New -> C++ Project -> Project type: Makefile project -> Empty Project -> Choose your toolchain (MinGW GCC).
  2. Make files and folders to match this folder structure:
    /include               <- headers
    /src/main.cpp          <- cpp source files
    /obj/{release,debug}   <- object files
    /bin/{release,debug}   <- where our .exe will be
    /resource/resource.rc  <- icons, images
    /Makefile              <- our makefile

    Your structure should now look something like this:

Note: the two last include folders under includes is added later in the guide

Configuring your project for wxWidgets and auto debug/release compiling

Right click your project -> Properties -> C/C++ Build.
Click the Mange Configurations button and add a new called Release and rename Default to Debug. It should look like this when your are done:

Now from the Configuration dropdown choose Debug. Click the Behavior tab. Leave everything default, except:
Build field, where you write: all DEBUG=${DEBUG}.
And in the clean field: cleanobj DEBUG=${DEBUG}.

Then do the same thing with the Release configuration. You may also check the parallell jobs option for faster compiling.

Go to the Build Variables (C/C++ Build -> Build Variables), make a new string variable called DEBUG in both configurations. Give it the value 1 in debug and value 0 in release configuration.

Adding include directories

  1. Right click your project -> Properties
  2. C/C++ General -> Paths and Symbols -> Includes
  3. Add project include folder (just write "include" and check all 3 checkboxes) and the wxWidgets include folder (check the two top checkboxes only)

The makefile

Paste the following into your makefile:

###################################
## MAKEFILE                      ##
##-------------------------------##
## wxWidgets Project             ##
###################################

DIRBIN = release
VER    = mswu
DEBUG ?= 1

ifeq ($(DEBUG), 1)
    VER    = mswud
    DIRBIN = debug
endif

EXES                 = bin/$(DIRBIN)/test

## wxConfig          ##########################################################

WX_CXXFLAGS          = `wx-config --cxxflags --wxcfg=gcc_lib/$(VER)`
WX_LIBS              = `wx-config --libs     --wxcfg=gcc_lib/$(VER)`
WX_RC_FLAGS          = `wx-config --rcflags  --wxcfg=gcc_lib/$(VER)`

## Compiling options ##########################################################

COMP_ADD             = -w
COMP                 = $(WX_CXXFLAGS) $(COMP_ADD) \
                       -Iinclude

## Libs/link         ##########################################################

LINK_ADD             = -Wall
LIBS                 = $(LINK_ADD) $(WX_LIBS) \
                       -lshlwapi -lversion \
                       -mwindows

## Clean             ##########################################################

.PHONY: cleanobj
cleanobj : 
    $(shell rm -rf obj/$(DIRBIN)/*.o resource/*.o)

## Build             ##########################################################

all : $(EXES)

CPP_FILES := $(wildcard src/*.cpp)
OBJ_FILES := $(addprefix obj/$(DIRBIN)/, $(notdir $(CPP_FILES:.cpp=.o))) \
             resource/resource.o

$(EXES): $(OBJ_FILES)
    g++ -o $@ $^ $(LIBS)

obj/$(DIRBIN)/%.o: src/%.cpp
    g++ $(COMP) -c -o $@ $<

resource/resource.o: resource/resource.rc
    windres -i resource/resource.rc -o resource/resource.o $(WX_RC_FLAGS)

Explanation:
To build test.exe it need the object files in OBJ_FILES. To build the obj files it takes every .cpp file from the src folder and makes each one into a separate .o file. Then uses thoose to build the .exe. You can add your own compiler flags to COMP_ADD and linker flags to LINK_ADD. At the top it detects if it is a release or debug build. A debug .exe will be a LOT bigger than an release .exe.

Filling the other files

main.cpp simple Hello World example:

#include "wx/wxprec.h"

#ifndef WX_PRECOMP
#   include "wx/wx.h"
#endif

class HelloWorldApp : public wxApp
{
public:
    virtual bool OnInit();
};

DECLARE_APP(HelloWorldApp)

IMPLEMENT_APP(HelloWorldApp)

bool HelloWorldApp::OnInit()
{
    wxFrame *frame = new wxFrame((wxFrame*) NULL, -1, _T("Hello wxWidgets World"));
    frame->CreateStatusBar();
    frame->SetStatusText(_T("Hello World"));
    frame->Show(true);
    SetTopWindow(frame);
    return true;
}

resource.rc:

#include "wx/msw/wx.rc"

Test your application

Remember to save all files before compiling. Choose what configuration you want, and build.

It should build without problems. Try also clean (Project -> Clean...) on both configurations.

Navigate to the .exe in windows explorer and run the program.