Creating wxWidgets Programs with Visual Studio 2015 – Part 1

The information provided in this post is actually available elsewhere; I have included it here so that I can find it quickly without searching for it. (Hey, these posts are at least as much for me as they are for you!)

The previous post on this site discussed building the wxWidgets libraries using Visual Studio 2015. Now that we have the libraries available, how do we go about using wxWidgets in a program built using Visual Studio?

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.

Prior to the first time that you create a wxWidgets-based project, you must set up two environment variables. 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 System. Now click on Advanced system settings. This opens the System Properties dialog in the Advanced tab. Click on the Environment Variables... button to open the Environment Variables dialog. Click the New... button in the User variables panel to set up an environment variable for the logged in user, or in the System variables panel for all users of the PC. This opens the New User Variable dialog or the New System 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 also add the directory containing the DLLs to the PATH variable.

Click the OK button on all three dialogs and close the System dialog.

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, click on New Project... or select the File->New->Project... menu item to open the New Project dialog. Select Empty Project       Visual C++ and 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->Class... menu item to open the Add Class dialog. Double-click C++ Class Visual C++ to open the Generic C++ Class Wizard. Enter a name for the application class (e.g. MyProjectApp) in Class Name, and wxApp as the Base class name. Check the Virtual destructor checkbox and click the Finish button. At this point a message box is displayed containing the message: “Base class ‘wxApp’ not found in the project. Continue adding the class?” Click the Yes button.

Two files (e.g. MyProjectApp.h and MyProjectApp.cpp) are 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 _UNICODE
#ifdef _DEBUG
#pragma comment(lib, "wxbase31ud.lib")
#else
#pragma comment(lib, "wxbase31u.lib")
#endif
#else
#ifdef _DEBUG
#pragma comment(lib, "wxbase31d.lib")
#else
#pragma comment(lib, "wxbase31.lib")
#endif
#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);

Do not attempt to build this project yet. If you do, you will get the following error:
“c:\users\<username>\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. If you will be creating a large number of wxWidgets projects, you may prefer to follow the instructions provided in the User-Wide Settings in Visual Studio 2015 post. Once you have followed those instructions, jump to the instructions for building the project given at the end of this post.

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 as appropriate. 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 (e.g. $(WXWIN)/lib/vc_x64_dll if you created the wxWidgets x64 DLL configuration). 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.

Try to build MyProject. If you performed the setup described above correctly, there will be no build errors. Now run the program; 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

16 thoughts on “Creating wxWidgets Programs with Visual Studio 2015 – Part 1

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

  2. Pingback: Visual Studio, wxWidgets, and OpenGL | Using C++

  3. Pingback: Points, Lines, Triangles, Quadrilaterals, and Polygons | Using C++

  4. I believe you can avoid the environment variable setting by adding a user macro to the property sheet Microsoft.Cpp.Win32.user. This property sheet is added to all projects by default and let’s you make per-user changes to things like include search paths, library search paths, etc. It will be located under the %APPDATA% folder which is per-user, so you don’t need administrative rights to modify it.

    Like

    • Thanks. I have added a new post to describe doing exactly that as well as adding the include and lib source directories. I have also linked to that post from this post.

      Like

  5. Pingback: User-Wide Settings in Visual Studio 2015 | Using C++

  6. Pingback: Drawing Circles With OpenGL | Using C++

  7. When I choose `Empty Project Visual C++` as you suggest, I do not have the NDEBUG flag set and hence the `#ifdef NDEBUG` part in the cpp-file does not work.

    Like

  8. Matthias,

    NDEBUG is automatically defined when you select one of the Debug configurations for your project, and is not defined when you select one of the Release configurations. So, if you built a wxWidgets Debug X86 library, you must select the Debug x86 configuration for your application. Also, the same character set must be chosen for both, or the library will not be found.

    Liked by 1 person

  9. I understand that this is the idea, but it does not work. The idea of the code is that depending on whether a Debug configuration is selected or not, NDEBUG is defined or not, and thus the correct version of the library is chosen. The problem is the part “NDEBUG is automatically defined when you select one of the Debug configurations” is not correct if you before chose “Empty Project”. Try it out 🙂
    (I can speak only for VS2015.) You would have to add to your tutorial to go to the preprocessor settings and add NDEBUG manually for the Debug configuration for it to work out of the box. Alternatively you could start with a Win32 project, then the NDEBUG flag is already set correctly and it works automatically as you say.

    Like

  10. Matthias,

    You are correct; NDEBUG is not defined automatically for debug builds. NDEBUG only disables standard C++ assertions. I should not have used NDEBUG to determine the library file to include.

    The Visual C++ compiler defines _DEBUG for debug builds. I have changed the code to select the library based on that macro. Note that I have also reversed the library that is loaded based on whether this macro is set or not.

    Thanks for catching this.

    Like

  11. NDEBUG stands for "no debug" and it's only defined in release builds. _DEBUG is defined in debug builds. So you can use either one, but they are opposite in their meaning.

    Like

  12. I received a comment on this post today. From the post, I have determined that the last set of changes I made removed two #include directives, which I have added back in. I have not approved this person’s comment because of the rather offensive language in the post, and because the person went out of his way to hide his identity, both of which I do not tolerate on this blog.

    Like

  13. ok, i did all that, and even did all from your “User-Wide settings” article. build solution works just fine, but every time i try to run the app, i get error about “wxbase31ud_vc_custom.dll not found” and “wxmsw31ud_core_vc_custom.dll”. i even looked up these DLL in the folders you mentioned – they are there. any ideas?

    Like

    • I assume that you are attempting to run the built application in stand-alone mode (that is, not trying to execute it from within Visual Studio).
      The settings in User-Wide Settings are needed so that you can build and execute from within Visual Studio. When you try to run the application in stand-alone mode, the application has a standard order in which it looks for DLLs. See https://msdn.microsoft.com/en-us/library/7d83bc18.aspx for information. In your case, the DLLs are not being found in any of those locations. I leave it to you to determine the best way to fix this.

      Like

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s