Getting Started with C and C++ on Windows

Note: If you are wondering about Visual C++, don't get it. Unless you already have it, it's not worth getting.

First, we're going to need a compiler. What is a compiler? It is a program that turns the C++ code into an executable, which is in binary format (as opposed to text). Picture it like turning a Word Document into a program that you can run. Well, understanding this isn't necessary to programming, and if you're confused, you'll pick it up later on.

So, you're ready to start downloading? Good. You'll need a compiler, a text editor, and a Graphical User Interface (aka GUI, and it's optional).

To make sure that MinGW is running, run cmd (I'm assuming you can do this). Once you're on cmd, type in 'gcc', then hit enter. It should say "Error: no input files" or something similar. If it didn't, make sure that you've got the bin folder path added to the environment variable as stated above.

Now that you've got all of the files, make a directory structure for yourself. Make a nice little section in your folder for programming, or somehow get organized. You're going to regret it later if you don't do this. This is how mine goes: In my folder called 'Saketh', I have a folder for programming called 'devel' (short for development). Within this, I have a folder called 'MinGW', where I installed MinGW to. It's okay if you installed your MinGW somewhere different, as long as you know where it is. Along with the 'MinGW' folder, there is a folder called 'projects' that I made. I also made a batch file that automatically creates projects for me. Project.bat

@echo off
cls
mkdir %1
cd %1
rem mkdir bin
rem mkdir src
echo gcc -o %1 %1.c > %1.bat
echo /*insert code here*/  > %1.c
exit
Copy it into your text editor, and save it as "project.bat" to your 'projects' folder. Let's test it out. Go to cmd, navigate to your projects folder. Type in:

project hello
It should create a folder called 'hello' in your projects folder. It will also create a batch file inside the folder 'hello' that you can use to compile the file.

How to create a console application

Here's an example. The following is a code sample for a simple C program. Cut and paste it into a file named hello.c to try it out. Put hello.c into the hello folder that we made by means of the project.bat.

   #include <stdio.h>

   int main(int argc, char **argv)
   {
      printf ("Hello\n");
      return (0);
   }
               
If you want to create a console mode executable hello.exe from a c file called hello.c, try the following (after navigating to your projects\hello folder):
   gcc -c hello.c
               
This compiles hello.c into an object file, hello.o
   gcc -o hello hello.o
               
This creates an executable hello.exe from hello.o. Alternatively, you can compile and link in one step using:
   gcc -o hello hello.c
               

The following is a code sample for a simple C++ program. Cut and paste it into a file named hello.cpp to try it out.

   #include <iostream>
   int main(int argc, char **argv)
   {
     std::cout << "Hello" << std::endl;
     return (0);
   }
               

For the C++ program, use the following to compile and link:

   g++ -c hello.cpp
   g++ -o hello hello.o
               
Adapted from http://www.mingw.org/docs.shtml

You don't need to know what the above programs do; you need to learn C++ for that to happen. Thoroughly read the resources that I gave links to up there (Thinking in C++, and the other resource). Learn C++ from there, and then come back. If you're really confused, Gamedev.net is an awesome programming resource. I'll probably write another tutorial by the time you're done with the books, about the installers, etc.

I will be updating this tutorial, but I am tired right now. I'll do it later.


Home
Hosted by www.Geocities.ws

1