0.6 — Compiling your first program

Before we can write our first program (which we will do very soon), we need to know a few things about development environments.

First, although our programs will be written inside .cpp files, the .cpp files themselves will be added to a project. The project stores the names of all the code files we want to compile, and also saves various IDE settings. Every time we reopen the project, it will restore the state of the IDE to where we left off. When we choose to compile our program, the project tells the compiler and linker which files to compile and link. It is worth noting that project files for one IDE will not work in another IDE. You will need to create a new project for each program you write (or overwrite an old one).

Second, there are different kinds of projects. When you create a new project, you will have to pick a project type. All of the projects that we will create in this tutorial will be console projects. A console project means that we are going to create programs that can be run from the dos or linux command-line. By default, console applications have no graphical user interface (GUI) and are compiled into stand-alone executable files. This is perfect for learning C++, because it keeps the complexity to a minimum.

Third, when you create a new project for your program, many IDEs will automatically add your project to a “workspace” or a “solution”. A workspace or solution is a container that can hold one or more related projects. Although you can add multiple projects to a single solution, we recommend creating a new workspace or solution for each program. It’s simpler and there’s less chance of something going wrong.

Traditionally, the first program programmers write in a new language is the infamous hello world program, and we aren’t going to deprive you of that experience! You’ll thank us later. Maybe.

A quick note about examples containing code

Starting with this lesson, you will see many examples of C++ code presented. Most of these examples will look something like this:

If you select the code from these examples with your mouse and then copy/paste it into your IDE, you may also get the line numbers (depending on how you made the selection). If so, you’ll need to remove these manually.

If you’re using the Visual Studio IDE

Although the following section was written using Visual Studio 2005, it essentially works the same for all versions of Visual Studio.

To create a new project in Visual Studio, go to the File menu, and select New -> Project. A dialog box will pop up that looks like this:

VC2005 Project Dialog

First, make sure “Visual C++” is selected on the left side.

Second, underneath “Visual C++”, select the Win32 project type, and Win32 Console Application will automatically be selected for you. In the Name field, you will enter the name of your program. Type in HelloWorld. In the Location field, pick a directory that you would like your project to be placed into. We recommend you place them in a subdirectory off of your C drive, such as C:\VC2005Projects. Click OK, and then Finish.

On the left side, in the Solution Explorer, Visual Studio has created a number of files for you, including stdafx.h, HelloWorld.cpp, and stdafx.cpp.

Initial Visual C++ program

In the text editor, you will see that VC2005 has already created some code for you. Select and delete all of the code, and type/copy the following into your compiler:

What you end up with should look like this:

Visual C++ hello world program

To compile your program, either press F7 or go to the Build menu and choose “Build Solution”. If all goes well, you should see the following appear in the Output window:

Successful build

This means your compile was successful!

To run your compiled program, press ctrl-F5, or go the Debug menu and choose “Start Without Debugging”. You will see the following:

Program run

That is the result of your program!

Note: If you see the console window (black box) without any text, your anti-virus may be interfering. Try turning your anti-virus off temporarily and try again.

Important note to Visual Studio users: Visual studio programs should ALWAYS begin with the following line:

Otherwise you will receive a compiler warning, such as c:testtest.cpp(21) : fatal error C1010: unexpected end of file while looking for precompiled header directive

Alternately, you can turn off precompiled headers. However, using precompiled headers will make your program compile much faster, so we recommend leaving them on unless you are developing a cross-platform program (a program that can be compiled on different operating systems/architectures).

The example programs we show you throughout the tutorial will not include this line, because it is specific to your compiler.

If you’re using the Code::Blocks IDE

To create a new project, go to the File menu, and select New Project. A dialog box will pop up that looks something like this:

Code::Blocks Project Dialog

Select Console Application and press the Go button.

This should pop up a wizard:

Code::Blocks Wizard Dialog

On the next page, select “C++” as your language. Click Next.

Code::Blocks Wizard Dialog

Next, you’ll be asked to name your project and choose a location. For your project title, set a name (such as HelloWorld). In the Project Title field, you will enter the name of your program. Type in HelloWorld. In the Folder to create project in field, pick a directory that you would like your project to be placed into. We recommend you place them in a subdirectory off of your C drive, such as C:\CBProjects. Click Next.

Code::Blocks Wizard Dialog

On the next screen, Code::Blocks asks you what compiler you want to use, and what configurations you want. Don’t touch anything here, just hit Finish.

Code::Blocks Wizard Dialog

Now you’ve created your project, and should see your project under the default workspace:

Code::Blocks Project Closed

Open your project node, then open “Sources”, and double click on “main.cpp”. You will see that the hello world program has already been written for you!

To build your project, press ctrl-F9, or go to the Build menu and choose “Build”. If all goes well, you should see the following appear in the Build log window:

Successful build

This means your compile was successful!

To run your compiled program, press ctrl-F10, or go the Build menu and choose “Run”. You will see something similar to the following:

Program run

That is the result of your program!

If you’re using a command-line based compiler

Paste the following into a text file named HelloWorld.cpp:

From the command line, type:

g++ -o HelloWorld HelloWorld.cpp

This will compile and link HelloWorld.cpp. To run it, type:

HelloWorld (or possibly ./HelloWorld), and you will see the output of your program.

If you’re using a web-based compiler temporarily

Paste the following into the input form:

and then press “Run”. You should see your output below the form.

Once you install a full IDE, you’ll want to return to this lesson again to learn how to create a project in your IDE.

If you’re using other IDEs

You will have to figure out how to do the following on your own:
1) Create a console project
2) Add a .cpp file to the project (if necessary)
3) Paste the following code into the file:

4) Compile the project
5) Run the project

If compiling fails (“Something went wrong!”)

It’s okay, take a deep breath. We can probably fix it. 🙂

First, check to ensure that you’ve typed the code in correctly, with no typos or misspellings (also, make sure you’re not including line numbers in your code). The compiler’s error message may give you a clue as to where or what the problem is.

Second, check lesson 0.7 -- A few common C++ problems, as many common problems are addressed there (including the COFF error that many of you are encountering).

If that fails, try searching for your error message on Google. It’s likely someone else has encountered this before and figured out how to fix it.

If you are using a much older C++ compiler, the compiler may give an error about not understanding how to include iostream. If this is the case, try the following program instead:

In this case, you should upgrade your compiler to something more compliant with recent standards.

If your program runs but the window closes immediately

This is an issue with some compilers, such as Bloodshed’s Dev-C++. We present a solution to this problem in section 0.7 -- A few common C++ problems.


Congratulations, you made it through the hardest part of this tutorial (installing the IDE and compiling your first program)!

Don’t worry if you don’t understand what all the different lines in the Hello World program do. We’ll look at and explain each line in detail in the upcoming lesson 1.1 -- Structure of a program.

0.6a -- Build configurations
0.5 -- Installing an Integrated Development Environment (IDE)

579 comments to 0.6 — Compiling your first program

  • Jack

    Whenever I build Helloworld, the output shows up fine, but when I press Crl-F5,it says

    Executable for Debug Session

    Please specify the name of the executable file to be used for the debug session.

    Executable file name:

    URL where the project can be accessed (ATL Server only):


    And then the command prompt does not run.
    Please Help!!!

    • TS

      Did you ever work this out as I'm having the same problem.

      • ab

        i had the same problem. i right clicked on the bold HelloWorld in the explorer on the right, chose properties > General > Project Defaults and for "configuration type" i changed it from DLL to exe. this seems to have worked. i am a newbie, so take this all with a big grain of salt.

  • Matthew

    I love this site. However it would be nice to see a Makefile tutorial.. those things are a real brain ache.

    • That's one of the best things about modern IDEs -- no need to create Makefiles because the IDE does it for you!

      I was never very good at Makefiles anyway.

  • Elijah

    ok so is there a diference between codeblocks and visual, do i need them both or only 1? ahh this is alot of stuff lol

  • Tom

    g++ is not recognized as an internal or external command.

    Thanks in advance. Great Site!

  • Chris

    Hi, got down to the section in 0.6 - "Using a command-line based compiler", on a Windows XP machine, am a bit confused as to where/what directory the HelloWorld.cpp 'text' file, created in notepad, should be placed. And where the command line is. If i run the command 'g++ -o HelloWorld HelloWorld.cpp' from the Windows 'cmd' screen, I get an error.

    Regards Chris

    • On Windows, I highly recommend using an IDE. It makes things much easier.

      You probably got an error because you never installed g++, which I do not cover how to do in this tutorial because using an IDE is much simpler.

  • Jonathan

    Hi alex i have a problem when i run the first version of the program in "Start without Debugging " i get a message "The application has failed to start because MSVCR90.dll was not found.Re-Installing the application max fix the problem"

    What have i done wrong i followed everything you said?
    Please reply soon i cant continue on!

    • I am not sure what is going on. MSVCR90.dll is a windows dll that programs compiled with visual studio 2008 need to run. It should be in your windowssystem32 directory (I think).

  • i just started to learn C++
    i have mocrosoft visual studio 2008 professional
    i see here you use mvs2005
    so i cant do some things that u do here as code::blocks; please help me... it is my first time and i have no one to help me

    • All of the code that works with MSVS 2005 should work with the 2008 version as well. The code::blocks stuff is just for people using that compiler and can be skipped if you're using the MS compiler.

  • Roger

    the following is what i have entered, i must have made a mistake when it said to delete the _tchar function.
    What have i done wrong.
    cannot get the iostream to display in this message

    // HelloWorld.cpp : Defines the entry point for the console application.

    #include "stdafx.h"
    #include iostream

    int _tmain(int argc)
    return 0;
    int main()
    std::cout << "Hello world!" << std::endl;
    return 0;

    • Get rid of _tmain(). You don't need it for these kinds of programs. Also, it should be:

      Don't forget the angle brackets.

      (PS: Put your code in <pre> tags and you will be able to use angle brackets in messages)

  • Hello I Am Using Code::Blocks, And Am Having trouble with compiling. When I Select console application, I am prompted to select a compiler, but there is like 50 to choose from on the list, and all of them give me the same error when i compile.

    Please Help

    • wert

      i have found that the first-run compiler autodetection fails on vista. or maybe it is that it never even runs. you need to go into the compiler settings and the toolchain executables tab and choose autodetect. this should find the mingw compiler that should have been installed with your codeblocks (you should be running the codeblocks installer that comes WITH mingw and has mingw in the name, unless you are installing it seperately). this fixed the invalid compiler error for me.

    • csvan

      You need to download and install a compiler I think, I do not believe Code::Blocks comes packed with one. A good compiler to use is the GCC (Gnu Compiler Collection), but to my knowledge it can be quite hard to set up a compiler on your own if you do not have the appropriate knowledge.

      Depending on what platform you use, there are solutions to this. If you run OSX (as I do), you will find GCC and the complete IDE Xcode on the installation disc for OSX (just insert it and look for optional installs and Xcode tools), that will install GCC as well.

      If you are running Windows, I would suggest downloading and using DevC++, its good, and comes with a compiler out of the box I think.

      Email me if you have further problems (

  • Noah

    I did it without looking!! (on my fourth try).

  • Aeris

    Every time I try to create a new project, it says "Automation sever cannot create object." Or something to that effect. What's going on?

  • Once again, never mind. I figured it out. In case someone else ends up with the same problem down the road, you can't have spaces in the filepath and I don't think it liked my main folder name "C++Programming" either.

  • danaldo

    microsoft visual studio 2008

    new project

    visual c++


    comes with two 'visual studio installed templates'

    1) Win32 console application
    2) win32 project.

    whats the differnece between the two?

    the simple reason and the complex reason would be much appreciated.
    thanx dan

    • A console project is one that uses the MS Dos console and has no windows graphical user interface. A win32 project is one that has an actual window/dialog box.

  • I'm using Code Blocks for Mac Os X and I got the program compiled correctly, however; when I use ctrl-F10 to start it, I get a message saying that the file does not exist. I can go through and open it by itself from outside the Code Blocks program and it runs correctly. I was wondering if there was some way to fix this problem so I can run it from Code Blocks. Thanks in advance.

    • csvan

      Hmmm, I have used Code::Blocks in the past and not had this problem. Did you start a new project, or did you just create a new file? I believe your problem lies there. Always start by creating a new project.

      • Alex

        No, I have the same error, and I opened a new Project. It says, and I quote:

        "Conversion - Debug" uses an invalid compiler. Skipping...
        Nothing to be done.

        I've looked all over the internet, and nobody seems to have any idea how to fix this.

        • Elsa

          I have the same error when I try to compile it, it does the exact same thing as that!

          (“Conversion – Debug” uses an invalid compiler. Skipping…
          Nothing to be done.)

          Have you found anything to solve it?

          It would be great if somebody could help 🙂

  • shamun.toha

    As i saw this is a standard online guide for C++ and Hellow World. Questions and Request:

    1. Can it be update for Mobile/PDA OS Nokia, Microsoft "hello world" too, using code block or IDE MS2008 ?

    i am tired to find it and central url as my blog, but c++ tutorial seems great in this site.

    • The information presented on this site is generally platform agnostic. Anything that is Windows specific is explicitly noted (or should be -- if it isn't, let me know). If you're developing on another platform, the code presented in these tutorials should work (assuming your compiler implements language support correctly), but you will of course have to figure out how to actually get it to compile yourself. 🙂

  • Tim

    I am wondering how to get the Code::Blocks templates to show up. I have tried the add and create on the solution thingy.
    The link below is what I have done

  • Ben

    Hi Alex,

    I use Visual C++ 2008 and when I create a Win32 console application I get an extra file: "targetver.h". This file seems to define the minimum configuration required for my program:

    #pragma once

    // The following macros define the minimum required platform. The minimum required platform
    // is the earliest version of Windows, Internet Explorer etc. that has the necessary features to run
    // your application. The macros work by enabling all features available on platform versions up to and
    // including the version specified.

    // Modify the following defines if you have to target a platform prior to the ones specified below.
    // Refer to MSDN for the latest info on corresponding values for different platforms.
    #ifndef _WIN32_WINNT // Specifies that the minimum required platform is Windows Vista.
    #define _WIN32_WINNT 0x0600 // Change this to the appropriate value to target other versions of Windows.

    But I don't have Windows Vista, just XP. So far it runs just fine, but I'm wondering if I shoudn't swith for Visual C++ 2005 right now; or can I keep going like that?

    This file "targetver.h" is called in "stdafx.h", and if I remove the command, it runs again. But maybe some more advanced features will require this file.

    To make it short, I'm just wondering if I can run C++ 2008 without having Vista.

    Thanks a lot for this tutorial.

    • If I'm not mistaken (and someone correct me if I am), the _WIN32_WINNT #define tells Visual Studio which set of Windows header files to compile your programs with. Visual Studio 2008 defaults to 0x0600 because they assume you will be coding Vista programs. If you're coding programs that you want to run on WinXP, you should probably change this to 0x0501 (0x0502 for XP SP2) as this will ensure you don't accidentally use Vista-only functions.

      Note that this has nothing to do with whether you can run VS2008 on XP -- you can. It only has to do with what OS the programs you COMPILE using VS2008 can run on.

  • Derek

    Umm I'm a complete noob...

    I'm using Code::Blocks and for some reason I can't run the hello world program...

    I put the code in, pressed build (successful), and when I click 'run' nothing happens.
    Any ideas...?

    • My guess is that your program is running and then terminating so fast you can't see the result. Read the part of this tutorial entitled "If your program runs but the window closes immediately" and see if that fixes your issue.

  • Very nice. This got me up and running quite nicely.

  • Masss

    I'm following this well, but the problem is that using this Microsoft Visual C++ 2008 version, I don't have the header file or the source file created from the start. I only have three empty files in the tree.

    I've managed to write as you did, but how do i create those other files?
    I managed to add a header item to the folder and coded

    and it seemed to work! Is that what is coded in the header file in the example above?

    • I'm not sure I understand your question. However, just to make it clear, in order to use cout (which is what puts your text on the screen), you have to include iostream, because that's where cout is defined.

  • louis

    code compiles fine, and i can run the .exe from the command line. the problem is this...
    once the program is complete the console window closes automatically.

    so i only see the hello world for a nanosecond!

    how can i make the window stay open, or do the "press any key to continue" bit?


  • Josh

    thanks bro, this helped me out alot! thnaks for helping me write my first C++ program

  • When using a command line to compile (ms vs c 2008 express ed.), what does the "-c" mean in the following command? (i.e. cl SomeFile.cpp -c).

    • Depends on the compiler. With g++, -c means "compile to object files, but don't link into an executable file". With Microsoft's command line compiler, -c doesn't mean anything because cl.exe uses slash commands (/) rather than minus commands (-).

  • Abhishek

    I don't get the "using namespace std" thing coz i don't know what's a namespace and what's std(standard??)

    And in my book they have written using namespace std before int main().......does that make any difference?

    • Don't worry about the namespace thing right now. I cover namespaces in a future lesson when you're better equipped to understand what a namespace is.

      If you put using namespace std outside of main (but not in another function), it affects the entire rest of the file. If you put it inside a function, it only affects rest of the function.

      There's really no right or wrong way to do it. I prefer to do it on a function-by-function basis because it's slightly safer, but for simple programs either way is fine.

  • Wesley

    Some people do:

    Is there a difference and a reason? Do some compilers only accept that?

    • I presume you are talking about the difference between:


      It's really just two similar but slightly different ways of doing the same thing. Both should work on all compilers.

      To elucidate slightly, the cout object lives inside a namespace called std. If you just use the name cout, the compiler doesn't know to look inside of the std namespace. Consequently, you have to tell it to look there. There are two ways to do this: directly (std::cout), or indirectly (using the using statement, which basically sets up a default namespace that the compiler will look in if it can't resolve symbols). Using std::cout is the safer solution, but can make for ugly code if you're doing lots of I/O. In those cases, the using statement can be a better choice.

      BTW: Including iostream inside PRE tags seems to work fine for me:

      • Brian

        What do you mean by "Using std:: is the safer solution?" Is it more efficient/faster by not loading the entire namespace or just more compatible? Thanks!

        • Alex

          When you use the std:: prefix, you're referencing a specific thing, so the odds of something going wrong is very small.

          When you use "using namespace std", you're telling the compiler to check the std namespace whenever it can't resolve a name. This opens up a small possibility that the compiler could resolve a name to something in the std namespace when you didn't intend for it to do so. This is unlikely, so it's generally not worth worrying about.

  • Tiomon

    By the way the end1 is endl (LOWERCASE L)

    i thought it was a 1 and i took an hour trying to figure out what was wrong lol.

  • Quinton

    How could I get this for Dev-Cpp? I did it, but the executable file flashes for .1 of a second and closes.

  • Zung

    Extremely useful and easy to understand ans follow. Thank you guys for a great work.

  • Suntha

    Very useful tutorial- especially for beginers for me! Thanks a lot!

  • Bryan

    There are 2 different things you have "using namespace std;" while in the pic example you have "std:: cout

    [ Code::Blocks does that by default, so I just left it. Either way works. -Alex ]

Leave a Comment

Put all code inside code tags: [code]your code here[/code]