Compiler setup and use

I'm looking for instructions on how to setup a compiler on my Windows XP machine, and then actually compile a C++ program. I'm a long time programmer in interpreted and higher level languages, so I've never had to deal with compilers or make files or linkers or header files or any of that very confusing stuff. For me, if my code is standards compliant, then it will be interpreted by whatever virtual machine the client is running, I just deal with the code. So though I know a thing or two about programming, I know nothing about turning .txt files into a .exe file, and all the beginners C++ tutorials want to start with "This is a /comment/, and these are /variables/" -- and say nothing about how to compile.

I don't want to use an IDE, at least not a fully featured one. I want to be able to write all the files myself, without the IDE hiding code in project files or anything like that. I want to write out the #include directives, not have the IDE insert them for me when I click check boxes next to the file name. I've used Programmer's Notepad in the past; I understand that there are ways of tying make and a compiler into that.

Here's the basic problem I'm encountering:

I know that code is plain text, so I download the simplest "Hello World" program I can find.

I know that the code must then be "compiled" into the executable, so I look online for "compilers".

I find g++, or cygwin, or mingw, etc., and I click the download link. I get an installer. This installer creates a directory filled with many assorted files and directories, and no "MinGW.exe", or similar program, in sight. I understand that the compiler will be a command line program, that I can deal with. What I don't understand is what I'm supposed to do with this random collection of files.

If someone could show me how to take my text file and compile it into an .exe, that would be very helpful, but that's only the first step. Next I have to figure out how to compile a program from several source code files, one or more resource files, one or more resources (such as .png files), or one or more header files. I understand that "make" is part of this process, but, again, what is make, and how do I use it?

Please help me out, I am very confused by all this.
That's because MinGW is a windows port of GNU tools.
If you want to use windows, then inside the MinGW directory should be something like "gcc.exe" or "g++.exe" -- it's the same programs, just that they happen to run on windows.

You could add the directory to your PATH variable if you know how.
If not, http://vlaurie.com/computers2/Articles/environment.htm
Last edited on
closed account (S6k9GNh0)
I suggest you setup MinGW with MSys. That should be a lot more convenient and is the more popular choice with MinGW. Also, MinGW is known to be setup as GCC compiler in Windows. It's used in NetBeans, Eclipse, Code::Blocks, and Dev-C++ (old).
closed account (S6k9GNh0)
I suggest you setup MinGW with MSys. That should be a lot more convenient and is the more popular choice with MinGW. Also, MinGW is known to be setup as GCC compiler in Windows. It's used in NetBeans, Eclipse, Code::Blocks, and Dev-C++ (old).
You might also find this interesting :
http://www.cplusplus.com/forum/articles/7263/
Last edited on
Thanks for the responses, but I'm not really getting the info I need. I've already read the article R0mai linked to, and it's a prime example of the stone wall I'm running into: It says a lot about what a compiler is, but leaves you with no information on how to use it.

chrisname: "You could add the directory to your PATH variable if you know how.
If not, http://vlaurie.com/computers2/Articles/environment.htm" - It may be completely obvious to you how this solves my problem, but you're dealing with someone who doesn't understand the compilation process. I could set a path variable, but MinGW is currently installed right there in the C drive where it's expected... and I don't understand how changing the path variable is going to help me learn what to actually do with all these files.

So I've got WinGW installed (I'd install MSys, but I don't know which of the many items listed here [ http://sourceforge.net/projects/mingw/files/ ] is what I'm supposed to be installing) at the root of C, and now there are all these folders and junk. I've found g++ buried in the bin directory, and I've got the directory with my source file. What do I do with them?

Do I click something? Do I drag and drop something? Do I open the command line and type something? I'm looking everywhere for info, and all I ever find is "Read the instructions which came with your compiler/IDE".

Where are these instructions?
Last edited on
Thanks for the sympathetic reply, audit. I've had some luck with these two pages:

http://www.programmingforums.org/thread7219.html
http://gcc.gnu.org/onlinedocs/gcc-4.4.1/gcc/Overall-Options.html#Overall-Options
(How do I make these links clickable? html tags don't work, and the forum styled [url] doesn't work, either.)

Mostly because they don't try to hide anything behind the IDE, or say "It just happens, we'll explain it later". I'm the sort of person who's used to having a reference in front of me detailing the minutest details of how something runs; auto-configuring installers and magical IDEs confuse the hell out of me.

I still havn't found any references on using make, though, or what it really does, and now I'm reading about linkers and linking. This compiling process is awfully complicated.
Last edited on
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.
Thank you for the clear step by step instructions! I'm still iffy on Make, but I think that stems less from a lack of understanding the syntax and process (which is simple enough), and more from a lack of understanding of dependencies in C++.

C++ inherited this sequential compilation from C, whereas the interpreted languages I've dealt with create a sort of object tree first, and then compile from that. So this idea of having to include one thing /before/ another thing is foreign to me; normally I just define them anywhere, and as long as they're defined /somewhere/ the interpreter is happy.

I have some more questions (as I'm bound to over the next couple weeks), like how to include resources in a project. (The tutorial here [http://www.winprog.org/tutorial/menus.html] isn't working for me; I don't know if I'm doing something wrong of if it's IDE dependent). Should I post them here so that people know where I'm coming from, or is it more in-line with forum rules to post a separate topic?
Last edited on
Topic archived. No new replies allowed.