wxwidgetseclipse-cdt

How to get Eclipse CDT working with WxWidgets under windows?


I have been using Eclipse for years for PHP, Python, JavaScript, and lua. I am, however, new to C++ and Eclipse CDT. I've got a reasonable enough grip on C++ syntax and convention that I'm ready to move on to the reason I came to C++, which is GUI. At first I tried Code::Blocks, which seemed simpler (I like wizards!), but I really would prefer an IDE with git integration, and I realised C::B didn't have that before I managed to get compilation working. So, back to Eclipse.

So far, I have done the following:

Unfortunately, this is where I'm stuck. There are plenty of tutorials and forum posts out there, but I run into one or more of the following problems:

Alternately, I would take a recommendation for another GUI toolkit that has accessible instructions for getting the current version of itself working with the current version of Eclipse.


Solution

  • I'll show how to compile the wxWidgets minimal sample with MinGW and eclipse. First of all, I highly recommend that you build both a debug and a release version of the wxWidgets library. These directions will assume that is the case. I'm not an expert with eclipse, so I can't guarantee these are the best directions. These directions do work for me, but corrections and improvements are welcome.

    There are many, many steps here. But if you get a working project with the minimal sample, you can copy the project and change the code files to use it for further projects.

    Before we do anything else, define the WXWIN environment variable in eclipse if it not already defined. From the menu select Window->Preferences->C/C++->Build->Environment, and the press the add button to add the variable.

    enter image description here

    The easiest way to build the minimal sample that comes with the library is with the command line. To do this, simply change to the WXWIN\samples\minimal folder and enter the exact same command you used to build the library. Since the command given above was mingw32-make -f makefile.gcc SHARED=0 UNICODE=1 MONOLITHIC=0 BUILD=release, this will result if the following commands being executed in the shell:

    windres --use-temp-file -i../../samples/sample.rc -ogcc_mswu\minimal_sample_rc.o    --define __WXMSW__   --define NDEBUG    --define _UNICODE --include-dir .\..\..\lib\gcc_lib\mswu --include-dir ./../../include  --include-dir .  --include-dir ./../../samples --define NOPCH
    g++ -c -o gcc_mswu\minimal_minimal.o  -O2 -mthreads  -DHAVE_W32API_H -D__WXMSW__   -DNDEBUG    -D_UNICODE -I.\..\..\lib\gcc_lib\mswu -I.\..\..\include  -W -Wall -I.  -I.\..\..\samples -DNOPCH   -Wno-ctor-dtor-privacy   -MTgcc_mswu\minimal_minimal.o -MFgcc_mswu\minimal_minimal.o.d -MD -MP minimal.cpp
    g++ -o gcc_mswu\minimal.exe @gcc_mswu\minimal.exe.rsp   -mthreads -L.\..\..\lib\gcc_lib -Wl,--subsystem,windows -mwindows    -lwxmsw31u_core  -lwxbase31u    -lwxtiff -lwxjpeg -lwxpng   -lwxzlib -lwxregexu -lwxexpat   -lkernel32 -luser32 -lgdi32 -lcomdlg32 -lwinspool -lwinmm -lshell32 -lshlwapi -lcomctl32 -lole32 -loleaut32 -luuid -lrpcrt4 -ladvapi32 -lversion -lwsock32 -lwininet -loleacc -luxtheme
    

    If we do the same command with build=debug instead we get similar commands with just a few differences:

    windres --use-temp-file -i../../samples/sample.rc -ogcc_mswud\minimal_sample_rc.o    --define __WXMSW__       --define _UNICODE --include-dir .\..\..\lib\gcc_lib\mswud --include-dir ./../../include  --include-dir .  --include-dir ./../../samples --define NOPCH
    g++ -c -o gcc_mswud\minimal_minimal.o -g -O0 -mthreads  -DHAVE_W32API_H -D__WXMSW__       -D_UNICODE -I.\..\..\lib\gcc_lib\mswud -I.\..\..\include  -W -Wall -I.  -I.\..\..\samples -DNOPCH   -Wno-ctor-dtor-privacy   -MTgcc_mswud\minimal_minimal.o -MFgcc_mswud\minimal_minimal.o.d -MD -MP minimal.cpp
    g++ -o gcc_mswud\minimal.exe @gcc_mswud\minimal.exe.rsp  -g -mthreads -L.\..\..\lib\gcc_lib -Wl,--subsystem,windows -mwindows    -lwxmsw31ud_core  -lwxbase31ud    -lwxtiffd -lwxjpegd -lwxpngd   -lwxzlibd -lwxregexud -lwxexpatd   -lkernel32 -luser32 -lgdi32 -lcomdlg32 -lwinspool -lwinmm -lshell32 -lshlwapi -lcomctl32 -lole32 -loleaut32 -luuid -lrpcrt4 -ladvapi32 -lversion -lwsock32 -lwininet -loleacc -luxtheme
    

    To build the sample with eclipse, we want to make it execute roughly the same commands accounting for the slight differences between the debug and release configuratins. Select File->New->C/C++ project->C++ Managed Build. Enter a project name, select MinGW GCC, make sure the project type is Executable/Empty project, and click the finish button.

    enter image description here

    Now add a file to the project. You can either

    1. Selecting File->New->Source File, enter a name for the file such as "minimal.cpp" or whatever you want to call it, and hit finish. The new file will automatically open in eclipse. Select and delete the entire contents. In a text editor, open the file WXWIN\samples\minimal\minimal.cpp, select and copy the entire contents, paste into the file just created in eclipse, and save the file.
    2. File->Import->General->File System. Click the next button. Then select the \samples\minimal for the directory. Then select the file minimal.cpp from the list of files this brings up. Finally hit the finish button.

    Either way, there should now be a file named minimal.cpp in the project. To build this file, several settings need to be changed. From the menu, select Project->Properties->C/C++ Build->Settings

    Both the debug and release configurations will now build, but the application isn't complete quite yet. The first thing done building the minimal application in the command prompt was

    windres --use-temp-file ...
    

    According to this link, eclipse just doesn't support building resource files, so we need to handle this manually.

    1. Copy the files WXWIN\samples\sample.rc and WXWIN\samples\sample.ico into the project folder. (The project folder is the folder containing the minimal.cpp file created earlier). Alternately, you can use File->Import-> ... to import the files into the project.
    2. Now go back Project->Properties->C/C++ Build->Settings->Build Steps.

      • For Pre-build steps
        • for the debug configuration, enter windres --use-temp-file -i"${ProjDirPath}/sample.rc" -o"${CWD}\minimal_sample_rc.o" --define __WXMSW__ --define _UNICODE --include-dir ${WXWIN}\lib\gcc_lib\mswud --include-dir ${WXWIN}\include --define NOPCH
        • for the release configuration enter windres --use-temp-file -i"${ProjDirPath}\sample.rc" -o${CWD}\minimal_sample_rc.o --define __WXMSW__ --define NDEBUG --define _UNICODE --include-dir ${WXWIN}\lib\gcc_lib\mswu --include-dir ${WXWIN}\include --define NOPCH enter image description here
    3. Next switch back to the Tool Settings tab:

      • For MinGW C++ Linker:
        • For Miscellaneous,
          • for all configurations
            • for Other objects, add ${CWD}\minimal_sample_rc.o enter image description here

    These 2 extra steps will make eclipse compile the resource file and link the resources into the final executable.