Search code examples
c++mingwwxwidgetslinker-errorscodeblocks

Building a wxWidgets program in Code::Blocks


I'm trying to build this wxWidgets sample program using Code::Blocks 12.11 but the build fails no matter what I try.

For example, the build log shows the following (the project is under X:\programming\c++\HelloWxWidgets):

mingw32-g++.exe -Wall -fexceptions -g -D_GNUWIN32_ -D_WXMSW_ -DwxUSE_UNICODE -D_WXDEBUG_ -IC:\libraries\wxWidgets-2.8.12.01\include -IC:\libraries\wxWidgets-2.8.12.01\contrib\include -IC:\libraries\wxWidgets-2.8.12.01\lib\gcc_lib\mswud -c X:\programming\c++\HelloWxWidgets\hworld2.cpp -o obj\Debug2\hworld2.o mingw32-g++.exe -LC:\libraries\wxWidgets-2.8.12.01\lib\gcc_lib -o bin\Debug2\HelloWxWidgets.exe obj\Debug2\hworld2.o -static-libgcc C:\libraries\wxWidgets-2.8.12.01\lib\gcc_lib\libwxmsw28ud.a C:\libraries\wxWidgets-2.8.12.01\lib\gcc_lib\libwxmsw28ud.a(monolib_window.o): In function ZN8wxWindow6UpdateEv': C:\BuildAgent\work\wxPack\wxwidgets\build\msw/../../src/msw/window.cpp:1643: undefined reference toGdiFlush@0' C:\libraries\wxWidgets-2.8.12.01\lib\gcc_lib\libwxmsw28ud.a(monolib_window.o): In function ZNK8wxWindow13GetTextExtentERK8wxStringPiS3_S3_S3_PK6wxFont': C:\BuildAgent\work\wxPack\wxwidgets\build\msw/../../src/msw/window.cpp:2132: undefined reference toGetTextExtentPoint32W@16' C:\BuildAgent\work\wxPack\wxwidgets\build\msw/../../src/msw/window.cpp:2133: undefined reference to `GetTextMetricsW@8'

And these are some of the build messages that I get

C:\libraries\wxWidgets-2.8.12.01\lib\gcc_lib\libwxmsw28ud.a(monolib_window.o)||In function ZN8wxWindow6UpdateEv':| C:\BuildAgent\work\wxPack\wxwidgets\build\msw\..\..\src\msw\window.cpp|1643|undefined reference toGdiFlush@0'| C:\libraries\wxWidgets-2.8.12.01\lib\gcc_lib\libwxmsw28ud.a(monolib_window.o)||In function ZNK8wxWindow13GetTextExtentERK8wxStringPiS3_S3_S3_PK6wxFont':| C:\BuildAgent\work\wxPack\wxwidgets\build\msw\..\..\src\msw\window.cpp|2132|undefined reference toGetTextExtentPoint32W@16'| C:\BuildAgent\work\wxPack\wxwidgets\build\msw....\src\msw\window.cpp|2133|undefined reference to `GetTextMetricsW@8'|

Here are the full build log and all of the build messages (by the way, I don't have a C:\BuildAgent directory)

I am using wxPack 2.8.12 (installed in C:\libraries\wxWidgets-2.8.12.01) and GCC 4.7.2 (installed separately from Code::Blocks in C:\mingw using mingw-get )

Here are the build options in Code::Blocks

Thank you in advance of your help :)


Solution

  • Alright, I finally managed to get it working :)

    I'm posting here the procedure to follow if, like me, someone wanted to use wxWidgets in an (initially) console project under Code::Blocks. With this, anyone should be able to build/run at least this sample code in a Wind0ws environment:

    1. I'll assume that Code::Blocks and MinGW are correctly setup.
    2. Download/Install wxMSW from http://www.wxwidgets.org/downloads/ (in my case, I downloaded wxMSW-2.8.12-Setup.exe and installed it in C:\libraries\wxWidgets-2.8.12)
    3. Open a command prompt, wxWidgets needs to be built, I'll take the exemple of a statically-linked, monolithic, debug build that uses unicode (more infos here):

      cd /D C:\libraries\wxWidgets-2.8.12\build\msw

      mingw32-make -f makefile.gcc BUILD=debug SHARED=0 MONOLITHIC=1 UNICODE=1

      :(mingw32-make will take some time to build the library)

    4. Open Code::Blocks

    5. Create a new console project, or if you already have a console project (and you plan to add a wxWidgets GUI to it) open it
    6. Right-click on the project > Build options
    7. Here are the build options that I used for the Debug build. Code::Blocks build options for wxWidgets
    8. Build & Run

    [EDIT] wx/app.h should be included in addition to wx/wx.h

    #include <wx/wx.h>
    #include <wx/app.h>