Search code examples
wxwidgetseclipse-cdt

How to get Eclipse CDT working with WxWidgets under windows?


  • wxWidgets-3.1.3
  • Eclipse IDE 4.14.0
  • Eclipse CDT 9.10.0.2019x (the latest packages as of 2/2/2020)
  • MinGW compiler, installed via MSYS2
  • Windows 10 Pro

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:

  • added the MinGW compiler path to %PATH%
  • successfully compiled wxWidgets 3.1.3 using SHARED=0 UNICODE=1 MONOLITHIC=0 BUILD=release, these changes made in %WXDIR%\build\msw\config.gcc. The various tutorials I have found wildly disagree on these parameters, but the various responses to people with my problem here and on other forums have all been generally in agreement on them, and with the exception of BUILD, they're the defaults. So.
  • successfully compiled a test program from samples/minimal. The resulting executable runs without needing any other DLLs in the same directory.

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:

  • Not newbie accessible. "Add a link to your wxWidgets directory" but okay, how do I do that, and do you mean the main %WXDIR% code directory or %WXDIR%\lib or what?
  • Don't work. "Just File->Import->File System->%WXDIR%" and nope. Did, in fact, get rid of the "not resolved" for SOME references in code pasted from "minimal.cpp", but not all.
  • Explicitly refer to versions of the IDE or Code from, oh, say, ten years ago, and/or contain instructions that cannot be followed in the current version.

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

    • For GCC C++ Compiler:

      • For Preprocessor,
        • for all configurations add __WXMSW__, _UNICODE, HAVE_W32API_H, and NOPCH.
        • for the release configuration add NDEBUG enter image description here
      • For Includes
        • for all configurations, add ${WXWIN}\include
        • for the debug configuration, add ${WXWIN}\lib\gcc_lib\mswud
        • for the release configuration add ${WXWIN}\lib\gcc_lib\mswu
      • For Optimization
        • for the release configuration select Optimize more (-O2)
      • For Debugging
        • for the debug configuration select Debug level Default (-g)
      • For Miscellaneous
        • for all configurations, add -mthreads -W -Wno-ctor-dtor-privacy at the end of the "Other flags" box.
    • For MinGW C++ Linker:

      • For Libraries,
        • for the debug configuration:
          • for Libraries, add wxmsw31ud_core, wxbase31ud, wxtiffd, wxjpegd, wxpngd, wxzlibd, wxregexud, and wxexpatd
        • for the release configuration:
          • for Libraries, add all of the following: wxmsw31u_core, wxbase31u, wxtiff, wxjpeg, wxpng, wxzlib, wxregexu, and wxexpat
        • for all configurations
          • for Libraries, add all of the following: kernel32, user32, gdi32, comdlg32, winspool, winmm, shell32, shlwapi, comctl32, ole32, oleaut32, uuid, rpcrt4, advapi32, version, wsock32, wininet, oleacc, and uxtheme.
          • for Library search path, add ${WXWIN}\lib\gcc_lib
        • note: with MinGW the order of libraries is sometimes important and the libraries with names starting with "wx" used with the debug and release configurations should be listed first. You can use the up and down arrows to rearrange the order if necessary. enter image description here
      • For Miscelanious,
        • for all configurations
          • for liker flags, add -mthreads -mwindows
          • for Other options (-Xlinker), add --subsystem=windows.
        • for the debug configuration,
          • for liker flags, add -g to the existing contents.

    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.