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:
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:
- Create a folder with the name of the project eg. 'Project1'.
- Copy the the file makefile.mak into this folder.
- 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.
- Create a sub directory of 'Project1' and call it 'Source'.
- Copy all *.cpp and *.h files into the 'Source' directory except for, if the project contains resources,
resource.h (see below).
- 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'.
- If the project contains resources copy the files resource.h, script.rc and other resource files
eg icons into this folder.
- 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, 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:
- Select 'New Project' from the 'File' menu. This displays the 'New from template'
dialog box.
- 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.
- 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.
- Add in the source file(s) for your project by selecting 'Add files...' from the
'Project' menu.
- Configure any extra options required such as 'Build Options' or 'Properties'.
You can also access the 'Compiler Options' directly from the 'Compile' menu.
- 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:
- Select 'Compiler Options' from the 'Compile' menu. This displays the 'Compiler
Settings' dialog box.
- 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.
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:
- From the File menu, select new-->project.
- 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.
- Using the 'Create new project' dialog, navigate to or create a suitable base
directory for the project. Save your project to that directory.
- 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'.
- 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.
- 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.
- 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.
- 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.
- 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:
- From the 'Project's menu, select 'Project Options'. This displays the 'Project Options' dialog box.
- 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.
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
- From the 'File' menu select 'New project'. this displays the 'New project' dialog box.
- Select 'Win32 Project' from the available options, select a suitable project directory and type in
a project name. This launches the 'Win32 Application Wizard'.
- 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.
- 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.
- 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.
- 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.
- 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).
- 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:
- From the 'File' menu select 'New project'. this displays the 'New project' dialog box.
- 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'.
- 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.
- 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.
- 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.
- 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.
- 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='*'"
- 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.