Compiling WxWidgets with MSVC (2)

From WxWiki
Jump to: navigation, search
This article applies to the following versions
Platform wxWidgets Visual C++
Windows ?

(not specified)

2.?

(not specified)

6.0 - ?
Status: Unclear

Compiling on Windows with MSVC


Microsoft Visual C++ 6.0

You can also compile wxWidgets using only the command-line interface to Visual C++ 6.0. See Compiling Using MSVC On The Commandline. The command line compiler is now available free from Microsoft as Microsoft Visual C++ Toolkit 2003.

Microsoft Visual Studio

Static Compilation

Follow the proceeding instructions if you wish to compile the wxWidgets library, sample applications, or your own applications built on top of wxWidgets, in Visual C++. This will perform static compilation, creating an .exe application that will run on any Windows platform with no external dependencies that need to be supplied by the developer.

Microsoft Visual C++ Express Editions

Compiling wxWidgets with the Express Edition is different from the Standard editions.

One key thing to note: The Express Edition does not come with the Win32 Platform SDK. You have to download it separately. [1], or do a Google search for the Windows SDK. If you are getting errors about "windows.h cannot be found", the compiler is not pointed to the correct Platform SDK location.

Download the Express Edition and install it. Download the Platform SDK as shown above. You will have to make sure your projects are set to point to the SDK. Tools -> Options -> Projects and Solutions -> VC++ Directories. Use the "Show Directories For" pulldown menu, and make sure the Include and Library pulldowns have the correct Platform SDK directories. You can now begin the compilation of wxWidgets.

wxWidgets Library

Follow the same instructions as the VC++ Standard/Professional Edition. - Hint: if you receive the linker error "missing odbc32.lib", you may need to install the Data Access components for Visual Studio.

Sample Projects

1-3) Follow the exact same instructions as with VC++ Standard/Professional Editions below.

4) For both Debug and Release, you will need to add the following libraries to the beginning of your "Additional Dependencies".

 comctl32.lib rpcrt4.lib shell32.lib gdi32.lib kernel32.lib
 user32.lib comdlg32.lib ole32.lib oleaut32.lib advapi32.lib
Your Application

- For both Debug/Release, follow the exact same instructions as with VC++ Standard/Professional Editions. - REMOVE odbc32.lib from both Debug/Release builds. - For both Debug and Release, you will need to add the following libraries to the beginning of your "Additional Dependencies".

comctl32.lib rpcrt4.lib shell32.lib gdi32.lib kernel32.lib
user32.lib comdlg32.lib ole32.lib oleaut32.lib advapi32.lib

Microsoft Visual C++ Standard/Professional Editions

wxWidgets Library

1) Download wxWidgets.exe] (The .exe will set the environment variable WXWIN). Or you can download the .zip, but you will either have to manually add your environment variables, or type in the paths manually for includes and libs.

2) Compile wxWidgets.

 - Open build\msw\wx.dsw 
 - Convert all projects (Yes to All).
 - If you want to link against the static runtime libraries you should
 - For each project, right click, click Properties. Configuration Properties > C/C++ > Code Generation 
 - For Debug, change the Runtime Library to Multi-Threaded Debug (/MTd) 
 - For Release, change the Runtime Library to Multi-Threaded (/MT)
 Shortcut:
   You can accomplish the same effect if you open the .vcproj file(s) 
   in text editor and replace the following strings 
      - RuntimeLibrary="3" with RuntimeLibrary="1"
      - RuntimeLibrary="2" with RuntimeLibrary="0"
 If you have an editor which can do the replacement across all opened files (as Notepad++), the job is much easier.
 Note: Most usually you don't want to do this (link against the static runtime libraries, that is).
 - Batch build: Select the "Debug" and "Release" builds (not DLL nor Universal) 
 - Build all projects.
   (Any other libs you want to compile (such as in contrib) will follow the same process).
Sample Projects

1) If you want to link against the static runtime libraries you should:

  For each project, right click, click Properties. Configuration Properties -> C/C++ -> Code Generation 
    - For Debug, change Run Time Library to Multi-Threaded Debug (/MTd) 
    - For Release, change Runtime Library to Multi-Threaded (/MT)    
  Shortcut:    
    You can accomplish the same effect if you open .vcproj file(s) 
    in text editor and replace the following strings 
       - RuntimeLibrary="3" with RuntimeLibrary="1"
       - RuntimeLibrary="2" with RuntimeLibrary="0"
    If you have an editor which can do the replacement across all opened files (as Notepad++), the job is much easier.

2) If project uses a resource (.rc), add the line: #define wxUSE_NO_MANIFEST 1

  - i.e. the 'text' sample .rc file will look like this: 
       
  mondrian ICON "mondrian.ico" 
  #define wxUSE_NO_MANIFEST 1 
  #include "wx/msw/wx.rc"
               

3) Build Debug or Release versions (not DLL)

Your Application
DEBUG

Noteworthy settings (different from default .NET project)

  • Configuration Properties:
    • General:
      • Character Set: Not Set (Unless you need Unicode support. My app doesn't need Unicode, so this setting lets me avoid prefixing my strings w/ Unicode macros).
  • C/C++:
    • General:
      • Additional Include Directories: $(WXWIN)\include;$(WXWIN)\contrib\include;$(WXWIN)\lib\vc_lib\mswd (This will be different if you didn't install with the .exe and didn't set environment variables).
    • Preprocessor:
      • Preprocessor Definitions: WIN32;_DEBUG;__WXMSW__;__WXDEBUG__;_WINDOWS;NOPCH;_CRT_NONSTDC_NO_DEPRECATE (This last one eliminates many warning messages)
    • Code Generation:
      • Runtime Library: Multi-Threaded Debug DLL (/MDd)
    • Linker:
      • General:
        • Additional Library Directories: "$(WXWIN)\lib";"$(WXWIN)\contrib\lib";"$(WXWIN)\lib\vc_lib"
        • (The above will be different if you didn't install with the .exe and didn't set environment variables).
    • Input:
      • Additional Dependencies: wxmsw28d_core.lib wxbase28d.lib wxtiffd.lib wxjpegd.lib wxpngd.lib wxzlibd.lib wxregexd.lib wxexpatd.lib winmm.lib comctl32.lib rpcrt4.lib wsock32.lib oleacc.lib
    • Manifest File:
      • Generate Manifest: No
  • To get your resources working properly, you need to create a .rc file.
    • Create a new text file, with a .rc extension. Include this to your project. The text file should contain the line:
 #include <wx/msw/wx.rc>
    • Right click the resource and set the Additional Includes to the same as above.
 ------RELEASE------ 
- Noteworthy settings (different from default .NET project) 
 Configuration Properties:
  General: 
  - Character Set: Not Set (Unless you need Unicode support. My app doesn't need Unicode, so this setting lets me avoid prefixing my strings w/ Unicode macros). 
  C/C++: 
   General: 
    - Additional Include Directories: $(WXWIN)\include;$(WXWIN)\contrib\include;$(WXWIN)\lib\vc_lib\msw
    - (The above will be different if you didn't install with the .exe and didn't set environment variables).
    Preprocessor: 
     - Preprocessor Definitions:
     WIN32;__WXMSW__;_WINDOWS;NOPCH;_CRT_NONSTDC_NO_DEPRECATE
    (This last one eliminates many warning messages) 
     Code Generation: 
      - Runtime Library: Multi-Threaded DLL (/MD) 
       Linker: 
        General: 
         - Additional Library Directories: "$(WXWIN)\lib";"$(WXWIN)\contrib\lib";"$(WXWIN)\lib\vc_lib"
         - (The above will be different if you didn't install with the .exe and didn't set environment variables).
      Input: 
       - Additional Dependencies: wxmsw28_core.lib wxbase28.lib wxtiff.lib
        wxjpeg.lib wxpng.lib wxzlib.lib wxregex.lib
        wxexpat.lib winmm.lib comctl32.lib rpcrt4.lib
        wsock32.lib oleacc.lib odbc32.lib 
       Manifest File: 
        - Generate Manifest: No
        - To get your resources working properly, you need to create a .rc file.
        - Create a new text file, with a .rc extension. Include this to your project. The text file should contain the line:
          #include <wx/msw/wx.rc>
        - Right click the resource and set the Additional Includes to the same as above.

Additional Notes

In WxWidgets 2.6.3, wxregex.lib and wxregexd.lib seem to have been renamed to regex.lib and regexd.lib - update appropriate linkage in instructions above if you are using this version.

You need to go into the 'src' directory and use open wxWidgets.dsw (not wxWidgets.dsp or wxBase, unless you're certain that's what you want)

Before compiling, you might want to alter the defaults in <code>include\wx\msw\setup.h</code> to enable or disable threads support, i18n support, socket support, opengl support, etcetera. Also at some later point, when more setup.h's have been generated, this is the one to edit. See Setup.h

Also, if you're compiling wxWidgets to use in a specific one of the projects that use wxWidgets, do READ that project's instructions too. Sometimes you may need to change the default options a little, (e.g. to build statically linked libraries rather than DLLs) to get options to match what you need to work with that project.

If you have errors like:

MSVCRTD.lib(MSVCR80D.dll) : error LNK2005: _free already defined in LIBCMTD.lib(dbgheap.obj)

put this in Linker > Input > "Ignore Specific Library"  : LIBCMTD in debug or LIBCMT in release.

Compiling Contrib

If you want to build the contrib samples then you may have to add the appropriate contrib library to the linker input files (this is certainly true for the VC++ project files up to 2.3.4). Otherwise you may get errors like (for VC++):

    tree.obj : error LNK2001: unresolved external symbol 
       "public: __thiscall wxTreeCompanionWindow::wxTreeCompanionWindowclass wxWindow *,int,class wxPoint const &,class wxSize const &,long)"

Then if you get errors regarding duplicate symbols such as

  gizmos.lib(splittree.obj) : error LNK2005: 
     "public: __thiscall wxString::~wxString(void)" ([email protected]@[email protected]) already defined in wxmsw233.lib(wxmsw233.dll)

try setting WXUSINGDLL in the compiler preprocessor flags for the contrib library. Up to 2.3.4 (and maybe later) the contrib samples have DLL versions but some of the contrib libraries (e.g. gizmos) only have static versions.