Creating wxWidgets Programs with Visual Studio 2017 – Part 1

This post is a replacement for the post of the same name for Visual Studio 2015 that I published approximately two years ago. A number of changes made during various updates to Visual Studio 2017 has invalidated some of the instructions in that earlier post.

I have already discussed building the wxWidgets libraries using Visual Studio 2017. I will assume that you have done that.

Prior to the First Program

Note: This section discusses setting up an environment variable that points to the wxWidgets directory. An alternative, and probably better, method to that given in this section is provided in the User-Wide Settings in Visual Studio 2015 post. If you choose to use the procedure in that post, only set up the User Macro for WXWIN. Do not follow the instructions for adding folders to the Include Directories and the Library Directories. If you do make the changes to the Include Directories and the Library Directories, you may not be able to build the wxWidgets libraries in the future.

Prior to the first time that you create a wxWidgets-based project, you must set up an environment variable. For this, you need to have Administrator rights on your PC. The fastest way to create an environment variable in Windows 10 is to right-click the Start menu and select Settings. In the search box, enter “environment variables”, and select Edit environment variables for your account. This opens the Environment Variables dialog. Click the New… button in the User variables panel to set up an environment variable for the logged in user. This opens the New User Variable dialog. Enter WXWIN as the the variable name, and the directory of the top-level wxWidgets directory as the variable name. On my computer, this is C:\wxWidgets.

If you created wxWidgets DLLs, you must add the directory containing the DLLs to the PATH variable.

Click the OK button on both dialogs and close Settings.

IIRC, for Windows 8.1, you can click the Start button, select Control Panel, and follow a path similar to that stated above for Windows 10.

For Every New wxWidgets Program

Once the WXWIN environment variable has been defined, you can start Visual Studio, or restart Visual Studio if it was already running.

Now we can create a wxWidgets project. The documentation on the wxWidgets website suggests that you start by creating a Visual C++ Win32 project or by copying one of the Samples projects and modifying that. I prefer to start with an empty project: On the Start Page tab under New project, click on Empty Project       Visual C++,  or select the File->New->Project… menu item to open the New Project dialog; select Empty Project       Visual C++. Change the project name near the bottom of the dialog. For the sake of this post, assume you have named the project MyProject. Select the Create directory for solution and Create new Git repository checkboxes as appropriate. Click the OK button to create the project. You now have a project with no source files.

The first step is to create a subclass of the wxApp class. Right-click on the project name in the Solution Explorer. Select the Add->New Item… menu item to open the Add New Item dialog. Select the Header File (.h) item and enter the name of header file for the application class (MyProjectApp.h). Click the Add button.

Repeat this process, but select the C++ File (.cpp) item and enter the name of the source file for the application class (MyProjectApp.cpp). Click the Add button.

Two files (e.g. MyProjectApp.h and MyProjectApp.cpp) have been created and opened. Modify them to contain the following:

#pragma once

class MyProjectApp :
    public wxApp
{
public:
    MyProjectApp();
    virtual ~MyProjectApp();
    virtual bool OnInit() override;
};
#include <wx/wxprec.h>
#include "MyProjectApp.h"

#ifdef _DEBUG
#pragma comment(lib, "wxbase30ud.lib")
#else
#pragma comment(lib, "wxbase30u.lib")
#endif
MyProjectApp::MyProjectApp()
{
}

MyProjectApp::~MyProjectApp()
{
}

bool MyProjectApp::OnInit()
{
	wxFrame* mainFrame = new wxFrame(nullptr, wxID_ANY, L"MyProject");
	mainFrame->Show(true);
	return true;
}

wxIMPLEMENT_APP(MyProjectApp);

In the pragma comment statements, note the 30 in the lib filenames. This is an indication of the version of wxWidgets that you are using. If necessary, change it to match as follows:

  • For wxWidgets version 3.0.x, the value should be 30.
  • For wxWidgets version 3.1.x, the value should be 31.
  • For wxWidgets version 3.2.x, the value should be 32,
  • etc.

Do not attempt to build this project yet. If you do, you will get the following error:

c:\users\\documents\visual studio 2015\myproject\myproject\myprojectapp.cpp(1): fatal error C1083: Cannot open file: ‘wxprec.h’: No such file or directory

The following instructions set up project specific include and library directories. Right-click on the project name in the Solution Explorer and select the Properties menu item. This opens the MyProject Property Pages dialog. In this dialog, select the Configuration Properties->General menu item. Change the Character Set setting to the appropriate value. Note that the default wxWidgets builds have the Use Unicode Character Set setting, so unless you modified this in your wxWidgets build, you should set this value. Click the Apply button.

Now select the Configuration Properties->VC++ Directories->General->Include Directories item. This changes the text on the right to a dropdown list:

MyProjectPropertyPagesIncludeDirectoriesSelected

Click to open the dropdown list, and select <Edit> to open the Include Directories dialog:

IncludeDirectoriesDialog1

Double-click in the empty scroll box at the top of the dialog (where the cursor is in the image) and enter $(WXWIN)\include. Double-click below this line and enter $(WXWIN)\include\msvc. Press Enter, and the dialog box will look like this:

IncludeDirectoriesDialog2

Click the OK button to close the Include Directories Dialog, and then the Apply button. Now select Library Directories and open the Library Directories dialog. Enter the path to the wxWidgets library files: $(WXWIN)\lib\<directory as noted below>:

  • For wxWidgets 32-bit Debug and Release builds, use vc_lib.
  • For wxWidgets 32-bit DLL Debug and DLL Release builds, use vc_dll.
  • For wxWidgets 64-bit Debug and Release builds, use vc_x64_lib.
  • For wxWidgets 64-bit DLL Debug and DLL Release builds, use vc_x64_dll.

Click the OK button to close the Library Directories dialog, and then the Apply button on the MyProject Property Pages dialog.

If you built wxWidgets as a set of DLLs, open Configuration Properties->C/C++->Preprocessor. Open the Preprocessor Definitions dialog and add the following preprocessor definition: WXUSINGDLL.

Click the OK button to close the Preprocessor Definitions dialog, and the OK button in the MyProject Property Pages dialog to apply the changes to the project and to close the dialog.

Select a configuration that matches a configuration you built wxWidgets with. Select Debug if you built wxWidgets with either Debug or  DLL Debug. Similarly, select Release if you built wxWidgets with either Release or DLL Release. Make sure that you set the Library Directories to the correct lib directory.

Try to build MyProject. If you performed the setup described above correctly, there will be no build errors. If you get the following compiler error:

c:\wxwidgets\include\msvc\wx\setup.h(123): fatal error C1083: Cannot open include file: '../../../lib/vc_x64_lib/mswud/wx/setup.h': No such file or directory

then you have selected a configuration for which you did not build the corresponding wxWidgets library. Either build the wxWidgets library to match, or change the configuration for this program.

Once you have built the program, run it; the following window will display:

MyProject

You have created your first wxWidgets program. Admittedly, it doesn’t do much, but it shows that you can create a wxWidgets program. Part 2 of this post will look at modifying this program to do something useful. For now, just close this window to terminate the program.

Advertisements

15 thoughts on “Creating wxWidgets Programs with Visual Studio 2017 – Part 1

  1. Pingback: Creating wxWidgets Programs with Visual Studio 2017 – Part 2 | Using C++

  2. Pingback: Creating wxWidgets Programs with Visual Studio 2015 – Part 1 | Using C++

  3. Hello,

    i followed your tutorial and i got to the point where i get your example running. The problem is that visual studio cant find the dll’s in the directory i provided via $(WXWIN). I made a directory just as u did and put the include and lib folders in there after i compiled for DEBUG x64 with DLL usage and Unicode support. I followed your instructions about the user macro and the include and library directories. If i put the dlls in the root directory of the solution, it works fine. If i have them only in the designated lib/vc_x64_dll directory, i get a missing dll error. Do you have any idea where this can come from. I checked your tutorial at least 3 times now and ive been programming with java and eclipse for 15+ years so i know my way around in IDE’s., etc. but i cant find the problem here. Maybe you have an idea where this comes from.

    Thx alot 🙂

    Like

  4. nvm the comment, i got it to work by adding the dll directory to the environment under Project Properties->Debugging->Environment = PATH=D:\wxWidgets\lib\vc_x64_dll;%PATH%
    $(LocalDebuggerEnvironment).

    thx anyway for your help

    Like

  5. Jim O
    Many thanks for your excellent wiki. For the first time I’ve been able to build a wx project using VS(2017).
    Everything you describe it easily followed and correct and in Debug mode everything works fine. There was some build errors in Release mode though. Not your fault. I think there’s a bug in wxWidgets (first) setup.h (C:\wxWidgets-3.1.1-rc\include\msvc\wx\setup.h) To get the Release to build I had to hack it like this:
    Row 118:
    wxCONCAT6(../../../lib/, wxLIB_SUBDIR, /, wxTOOLKIT_PREFIX, u, /wx/setup.h) // Bug:Added u and changed to 6 fields
    Row 131:
    #define wxSUFFIX_STR “u” // Bug: Added u
    This solution works for me here using MSVC but is not at all a general fix. Comments?
    Best
    Håkan

    Like

    • Håkan.

      From looking at the changes you made, I assume you built a UNICODE version of the wxWidgets libraries. Again looking at the code around the changes you made, you get to those lines because wxSUFFIX is empty. Look at lines 22-40 of setup.h where wxSUFFIX is defined. wxSUFFIX is set to “u” if _UNICODE is defined. _UNICODE gets defined when you set the character set for your project to Unicode as stated in my post:

      “Change the Character Set setting to the appropriate value. Note that the default wxWidgets builds have the Use Unicode Character Set setting, so unless you modified this in your wxWidgets build, you should set this value.”

      That should fix your problem.
      Jim

      Like

      • Jim.
        Very good! It must have been as you describe although it was now set to Unicode. There must have been a temporary mistake for the release mode. And my “fix” worked, of course, for both character settings.
        We’ll all learn by time. Hopefully!
        Thanks for your lesson here and again for your excellent wiki.
        Håkan

        Liked by 1 person

  6. My error is: “1>c:\users\wilso\documents\wxwidgets\include\wx\wxcrt.h(496): note: consulte a referência à instanciação da função modelo ‘int wxStrcoll_String(const wxString &,const T &)’ que está sendo compilada”

    Like

    • Did you build the wxWidgets libraries for Unicode, and then attempt to build the program as Multibyte character set, or vice-versa?

      That seems like the most likely cause of your error, although I have not had a chance to look at the problem in detail. It will be 2 weeks or more before I can.

      Like

  7. Thank you for your tutorial.It helped me a lot but finally , after trying to build , I got these

    messages :

    Error C3861 ‘wxStrcoll’: identifier not found
    and :

    Error (active) E1256 __w64 can only be specified on int, long, and pointer types .

    Please , help me !

    Like

  8. Pingback: VS2017与wxWidgets的开发环境配置(仅静态链接) | 沉默的猫

Leave a Reply

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

WordPress.com Logo

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

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s