Once you've installed MinGW with the .msi installer, your system should be ready to go.
Using Explorer, create a directory you will use to put your program source files. For the examples here, I will use
D:\prog\example
Using a plain text editor (like Notepad, or preferrably something better like one of your other programming language's IDEs -- I tend to use my old Delphi 5 IDE to edit everything -- other people like Emacs or Notepad++, etc).
Create the following files:
example.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
// example.cpp
//
// The example's main program.
//
#include <iostream>
#include <limits>
#include <string>
using namespace std;
#include "module.hpp"
using namespace example_module;
int aaiiieee()
{
cout << "Aaaiiieeeee!\n";
return 0;
}
int main()
{
string users_name;
string users_quest;
string users_favorite_color;
cout << "What is your name? " << flush;
cin >> users_name;
cin.ignore( numeric_limits <streamsize> ::max(), '\n' );
users_name = titlecase( users_name );
cout << "Hello " << users_name << ", what is your quest? " << flush;
getline( cin, users_quest );
if (lowercase( users_quest ).find( "grail" ) == string::npos)
return aaiiieee();
cout << "What is your favorite color? " << flush;
getline( cin, users_favorite_color );
if (lowercase( users_favorite_color ) != "blue")
return aaiiieee();
cout << "Right. Off you go, " << users_name << ".\n";
return 0;
}
|
module.hpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
// module.hpp
//
// An example module header file.
//
#pragma once
#ifndef MY_EXAMPLE_MODULE_HPP
#define MY_EXAMPLE_MODULE_HPP
#include <string>
namespace example_module
{
std::string titlecase( const std::string& s );
std::string lowercase( const std::string& s );
}
#endif
|
module.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
// module.cpp
//
// An example module source file.
//
#include <algorithm>
#include <cctype>
#include <functional>
#include <string>
#include "module.hpp"
namespace example_module
{
std::string titlecase( const std::string& s )
{
std::string result = lowercase( s );
if (!result.empty()) result[ 0 ] = toupper( result[ 0 ] );
return result;
}
std::string lowercase( const std::string& s )
{
std::string result( s.length(), '\0' );
std::transform(
s.begin(),
s.end(),
result.begin(),
std::ptr_fun <int, int> ( tolower )
);
return result;
}
}
|
Makefile
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
CC = g++
CFLAGS = -Wall -ansi -pedantic
LDFLAGS =
example: example.o module.o
$(CC) $(LDFLAGS) -o $@ $^
example.o: example.cpp module.hpp
$(CC) $(CFLAGS) -c $<
module.o: module.cpp module.hpp
$(CC) $(CFLAGS) -c $<
clean:
del *.o
|
Open the command prompt (Start --> Programs --> Accessories --> Command Prompt).
Change to the directory you created.
C:\WINDOWS> d:
D:\> cd prog\example
D:\prog\example> _
Make sure the g++ and make executables are in your shell PATH:
D:\prog\example> path
PATH=C:\MinGW\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\system32\wbem;...
D:\prog\example> _
You should see it in there somewhere (between the semicolons). In my example here, it is listed first (
C:\MinGW\bin).
If it is not there, you will need to add it.
D:\prog\example> path C:\mingw\bin;%PATH%
D:\prog\example> _
Now run gmake to compile your program.
D:\prog\example> mingw32-make
g++ -Wall -ansi -pedantic -c example.cpp
g++ -Wall -ansi -pedantic -c module.cpp
g++ -o example example.o module.o
D:\prog\example> _
The makefile is setup so that if any of your files changes, only the stuff that depends on it is recompiled.
Example one: Load the "
module.hpp" file into your plain-text editor and re-save it. Run gmake again. You will notice that everything gets recompiled, because everything depends on that header.
Example two: Load the "
module.cpp" file into your plain-text editor and re-save it. Run gmake again. This time the "
example.cpp" file was not recompiled, because it did not depend on "
module.cpp". The "
module.o" object file was rebuilt, and the "
example.exe" executable was linked again, but that's all.
Once done all that, you can run your program as usual:
D:\prog\example> example
What is your name? _
et cetera.
You can use gmake to clean up after yourself.
D:\prog\example> mingw32-make clean
del *.o
D:\prog\example> _
To learn more about using Make, see the
GNU Make Manual
http://www.gnu.org/software/make/manual/make.html
Hope this helps get you started.