Compiling Source Code

This is a quick guide to getting started with some compilers. It is neither comprehensive nor intended to endorse or disparage any particular compiler, editor or integrated development environment(IDE). If your favourite compiler, ide or editor is not described here do not assume that the sample code elsewhere on this site will not work with it; this page only describes those compilers etc. which I have tested the example code with.

Compilers used are:

IDEs and editors used are:

MinGW - Minimalist GNU Windows toolset

These are free, open source compilers and associated tools ported from GCC (GNU compiler collection). While they do not come with an ide to manipulate them they are nontheless excellent and easy to use; if an ide is required then the ever popular Dev-Cpp and the more recent Code::Blocks are both free and use MinGW by default. If an ide is not required then I would recommend using 'msys' (minimalist system) to utilise them in a POSIX environment but this is not necessary when starting out as they can be used adequately from the windows command shell.

While you can use an ide to build programs with MinGW, you can also use MinGW's make utility (or mingw32-make) with a makefile, which is essentially a script containing rules for how a particular build should proceed. To avoid amending environment variables for your operating system, you can use a batch file (*.bat or *.cmd) file to automate the build process and invoke make. Each of the examples presented elsewhere on this website have a general makefile which can be used to assist compiling the source using the following approach:

  1. Create a folder with the name of the project eg. 'Project1'.
  2. Copy the the file makefile.mak into this folder.
  3. Create a batch file - call it build.bat - and set the path to your MinGW bin directory and the command to invoke make within it. For example, if you have installed MinGW in C:\MinGW then your batch file would contain:
    set path=c:\mingw\bin;%path%
    mingw32-make -f makefile.mak
    The batch file should be in the same directory as the makefile, makefile.mak, ie in the 'Project1' directory.
  4. Create a sub directory of 'Project1' and call it 'Source'.
  5. Copy all *.cpp and *.h files into the 'Source' directory except for, if the project contains resources, resource.h (see below).
  6. If the project contains resources (usually 'script.rc' and 'resource.h' with, perhaps, some other files) create a sub-directory of 'Project1' and call it 'Resources'.
  7. If the project contains resources copy the files resource.h, script.rc and other resource files eg icons into this folder.
  8. Open a DOS prompt in the 'Project1' directory and run build.bat as follows:
    build.bat
    If everything works then a sub folder of 'Project1' will have been created and will contain an exe and a number of other files(objects, *.o and, if the project contains resources, resource objects, *.res). These files are temporary and can therefore be deleted to recover disk space. The makefile comes with clean and cleanall rules which will remove all temporary objects or all built project files, including the executable. To invoke either of these rules, simply append the rule name to the command line used to call make, for example:
    mingw32-make -f makefile.mak clean
    will remove all temporary objects. Note that the 'clean' and 'cleanall' rules use rm.exe which is not included with MinGW (it is with MSys - 'minimalist system').

The makefile used is more or less the same for each example - provided it is supplied with the path to source and resource files and their file extensions, it should automatically compile all relevant inermediary object files and link them to produce a final executable in the output directory (which you can also modify in the makefile). A full, example makefile is viewable online:

Note that if you need to add any preprocessor definitions then you should amend the makefile's PREPROC macro and use the -D switch for each defintion required. For example, to build a unicode application simply append:

-D_UNICODE -DUNICODE

to the 'PREPROC' macro line.

makefile.mak

Code::Blocks

Code::Blocks, an open source ide built using wxWidgets, can be configured to use many different compilers. At the time of writing, it supports MinGW, bcc55 and vctoolkit. Although the steps are essentially the same for building code, successful compilation is entirely dependent on the proper installation of the key components of compilers and related tools - if these are mis-configured or not installed then don't expect to be able to build much. Refer to code::blocks documentation and forums for information regarding proper configuration. The following is a quick guide to building the source examples given elsewhere in this site:

  1. Select 'New Project' from the 'File' menu. This displays the 'New from template' dialog box.
  2. Check the 'Do not create any files' check box and select 'Win32 GUI application' from the available templates then click the 'Create' button. This displays the 'Save project' common dialog.
  3. Select a suitable directory to save your project using the 'Save Project' common dialog. Note that you will have to create a suitable directory for your project if you wish it to have one, as Code::Blocks will not do this for you.
  4. Add in the source file(s) for your project by selecting 'Add files...' from the 'Project' menu.
  5. Configure any extra options required such as 'Build Options' or 'Properties'. You can also access the 'Compiler Options' directly from the 'Compile' menu.
  6. Build the project by selecting 'Rebuild' from the 'Compile' menu or, if you have only made a modest change in one or two files you may prefer to select one of the compile submenu options from the 'Compile' menu.

To add preprocessor directives:

  1. Select 'Compiler Options' from the 'Compile' menu. This displays the 'Compiler Settings' dialog box.
  2. The 'Compiler Options' dialog has nested tab controls. With 'Compiler' selected in the top tab control, select '#defines' from the bottom. Add in any preprocessor directives there. For example, to #define WIN32_LEAN_AND_MEAN just enter 'WIN32_LEAN_AND_MEAN' in the field provided.
Dev-Cpp

Colin Laplace's ever-popular Dev-Cpp ide is an excellent open source(Delphi) tool that has been around for many years, is quick to install and very easy to use. The following is a quick guide to building the source examples given elsewhere in this site:

  1. From the File menu, select new-->project.
  2. From the 'New Project' dialog that appears, select 'Empty Project' from the 'Basic' tab, make sure that the 'C++ Project' radio button is checked, type in a suitable name for the project in the 'Name' field and then click the 'ok' button.
  3. Using the 'Create new project' dialog, navigate to or create a suitable base directory for the project. Save your project to that directory.
  4. Make the project a windows graphical user interface(gui) project by opening the 'Project Options' dialog from the 'Project' menu and set the 'Type' in the 'General' tab to 'Win32 GUI'.
  5. With the 'Project Options' dialog still open select the 'Build Options' tab and set the output directories for objects and executable if required; dev-cpp will create these directories if they don't already exist. You can change the default executable name from here if you want to. If you change nothing here then all objects, including the executable, will be output to your project directory.
  6. With the 'Project Options' dialog still open you can configure build parameters from the 'Compiler' tab. Dev-cpp will include debugging information in executables it builds by default which will make them quite large. Once you are finished debugging your project and are satisfied that it is complete you will probably want to remove all this debugging information and reduce your program to a more reasonable size. To do so, select 'Linker' from the left-hand list and set 'Generate debugging information' to 'no' and 'Strip executable' to 'yes' in the corresponding right-hand list. Rebuild the project and your executable should now be considerably smaller.
  7. Add in the source files for your project by selecting the 'Project' menu--> 'Add to project'. This opens up the 'Open File' common dialog box with which you can locate and add the source file(s) to your project.
  8. With the source file(s) added and your project configured you are now ready to build it. From the 'Execute' menu, select 'Rebuild all'. This launches a 'Compile Progress' dialog box that keeps you informed about the build process and, assuming there are no errors, will display 'Done' in the 'status' field when the build is complete. Select the 'Close' button to dismiss the 'Compile progress' dialog.
  9. To run the built executable, select 'Run' from the 'Execute' menu.
If you need to set extra compiler options, such as preprocessor directives, or linker options, such as additional libraries, then you would do so as follows:
  1. From the 'Project's menu, select 'Project Options'. This displays the 'Project Options' dialog box.
  2. Select the 'Parameters' tab and enter your additional compiler switches in the 'C++ compiler' field. For example, to define WIN32_LEAN_AND_MEAN for your project simply enter -DWIN32_LEAN_AND_MEAN into the 'C++ Compiler field; ie. to set project-wide #defines use the '-D' compiler switch. Additional libraries can be specified in the 'Linker' field but note the syntax is different from some other compilers. For example, if you wished to link with two libraries typically used in opengl programming, namely opengl32 and glu32(these should be listed in your Dev-Cpp lib directory as libopengl32.a and libglu32.a respectively), you would enter -lopengl32 and -lglu32 on separate lines in the linker field; note that is a lowercase 'L' after the minus sign in each case.
Microsoft's Visual C++(msvc)

If you have purchased one of Microsoft's 'visual c++' compilers you will have comprehensive documentation provided with it. If you have downloaded the free visual c++ express edition then you can consult the same comprehensive documentation in the form of msdn online.

msvc2003.net/msvc2005

  1. From the 'File' menu select 'New project'. this displays the 'New project' dialog box.
  2. Select 'Win32 Project' from the available options, select a suitable project directory and type in a project name. This launches the 'Win32 Application Wizard'.
  3. Within the 'Win32 Application Wizard', select 'Application settings in the left-hand panel; ensure the 'application type' is set to 'window application' and check 'empty project' in 'additional options'. Click the 'Finish' button. This creates an empty winapi project.
  4. Add project files to your project from the 'Project menu' by selecting 'add existing item...'; navigate to the directories containing the files you need to add, such as source code (*.cpp), headers(*.h) and resource scripts(*rc), and add them to your project.
  5. To link with addtional libraries (eg comctl32.lib), open your project's properties from the 'project' menu by selecting properties. Select 'linker - input' from the tree control on the left of your project properties dialog; the names of extra libraries should be added to the 'Additional Dependencies' field in the right hand panel.
  6. To add project wide preprocessor directives, such as WIN32_LEAN_AND_MEAN, to your project, open your project's properties from the 'project' menu by selecting properties. Select 'c++ - preprocessor' from the tree control on the left of your project properties dialog; the extra preprocessor symbols should be added to the 'Preprocessor Definitions' field in the right hand panel.
  7. Release buiilds only. You may have to dynamically link runtime libraries for 'release' builds to avoid errors when building applications that use stl features, such as std::string; this may be mandatory if you also have a version of msvc2005 installed. To do so, open your project's properties from the 'project' menu by selecting properties. Select 'c++ - Code Generation' from the tree control on the left of your project properties dialog and set the 'Runtime Library' field in the right-hand panel to 'Multi-threaded(/MT).
  8. Build the program by selecting 'rebuild solution' from the build menu. Use the 'batch build' option from the 'build menu' to build other configurations such as 'release' builds.

msvc2005-express

In order to do any sort of winapi programming with msvc2005-express you need to install the platform software development kit(psdk) and configure the ide for use as described in the following msdn webpage:

Using Visual C++ 2005 Express Edition with the Microsoft Platform SDK

Once you have configured the ide to use the psdk you can build the examples on this website as follows:

  1. From the 'File' menu select 'New project'. this displays the 'New project' dialog box.
  2. Select 'Win32 Console application from the available options, select a suitable project directory and type in a project name and click the 'ok' button.. This launches the 'Win32 Application Wizard'.
  3. Within the 'Win32 Application Wizard', select 'Application settings in the left-hand panel; ensure the 'application type' is set to 'window application' and check 'empty project' in 'additional options'. Click the 'Finish' button. This creates an empty winapi project.
  4. Add project files to your project from the 'Project menu' by selecting 'add existing item...'; navigate to the directories containing the files you need to add, such as source code (*.cpp), headers(*.h) and resource scripts(*rc), and add them to your project.
  5. To link with addtional libraries (eg comctl32.lib), open your project's properties from the 'project' menu by selecting properties. Select 'linker - input' from the tree control on the left of your project properties dialog; the names of extra libraries should be added to the 'Additional Dependencies' field in the right hand panel.
  6. To add project wide preprocessor directives, such as WIN32_LEAN_AND_MEAN, to your project, open your project's properties from the 'project' menu by selecting properties. Select 'c++ - preprocessor' from the tree control on the left of your project properties dialog; the extra preprocessor symbols should be added to the 'Preprocessor Definitions' field in the right hand panel.
  7. To add a resource manifest that will result in the application being drawn with winxp visual styles, open your project's properties from the 'project' menu by selecting properties. Select 'linker - Manifest File' from the tree control on the left of your project properties dialog; the following string, complete with quotes' should be added to the 'Additional Manifest Dependencies' field in the right hand panel:

    "type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'"

  8. Build the program by selecting 'rebuild solution' from the build menu or by pressing ctrl + alt + F7. Use the 'batch build' option from the 'build menu' to build other configurations such as 'release' builds.