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.

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 the tree under “Console Application”, 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)

456 comments to 0.6 — Compiling your first program

  • Rick

    hi Alex! Just started cpp a few days ago, had knowledge on python prior to this. Currently i’m up till chapter 3 and i have only been using console application. Which chapter (if there is) will we learn to use other project formats such as win32 project/ empty project?

  • Barkha

    Hi, i am using code block compiler. When I run a programme and after that i make certain change in the same programme and try to compile or built it, the option is not highlighted. I need to open a new project again and again, and type the same thing again along with new changes, is it normal or there is some issue in the compiler. Coz in turbo c++ i dont have to name the programme again and again even after running the programme i can make changes in existing programme and rerun it. Plz suggest.

    • Alex

      Definitely not normal. I’d maybe search Google or visit a Code::Blocks specific forum and see if you can figure out what’s going on.

  • Nima

    Hello dear Alex.
    You said me for creating static or dynamic library but I read elsewhere about make and makefile which prevent from recompiling all of project even without any .lib or .dll files.(untouched sources are not recompiling)
    However compiling with make and makefile still took so long to compile.
    Any advise?
    Best Regards.

    • Alex

      Today’s IDEs will also generally only recompile what is absolutely necessary.

      A lot of what causes programs to take a long time to recompile is poor structuring of header files. A single change to a header can cause almost every code file to recompile if it’s widely included. So to that end, the best thing you can do is keep your header files small, independent, and only include what you need.

  • In all the programs I write, I always include this line after the include statements:

    using namespace std;

    So my statements for printing something on the screen becomes simple like so:

    cout << "Please enter a number: ";

    The code becomes a little bit user friendly!!.  Me thinks so anyway!!

  • Dear Alex,

    You recommend to place the projects in a subdirectory off of C drive, such as C:\CBProjects. Why?

    • Alex

      Mainly because it makes your programs easier to find and avoids long path issues since some compilers tend to create a lot of subdirectories.

  • Nima

    Hello dear Alex.
    I asked you about compiling a program that contains Eigen library.
    Compilation time was high and you told me to make Eigen to static library if doesn’t change it continuously.
    But I have another question right here:
    What about make and makefile?
    It is true if I build the project for first time and it takes so long to compile but second times or more shouldn’t take so long because I don’t change Eigen library and based on make rule in compilation process g++ is not need to compile Eigen again.
    Thanks in advance.

  • Jeremy

    Not sure how your html pages are set up, but if you use a cascading style sheet, and add these lines for ol {} (ordered lists) and li:

    ol { -moz-user-select: none; -ms-user-select: none; -webkit-user-select: none; }
    li { -moz-user-select: all; -ms-user-select: text; -webkit-user-select: all; }

    No one should be able to copy-and-paste the numbers in ordered lists anymore.

    Good to see you’re still around, Alex. Your is still the best!

  • Nima

    Hello again dear Alex.
    I have another question about compiling a program with C++ language.
    When we compile a .cpp file it provides us an assembly code which we can’t see content of that directly (main.o for instance on windows) so why people say assembly can be faster than C++?
    I’m confuse about this issue.

    • Alex

      C++ doesn’t compile into assembly code, it compiles into object files that contain machine code.

      Assembly is a different programming language that has fewer more primitive commands. Those commands are very fast, but it’s also very difficult to write large or complicated programs.

      Previously, C++ compilers weren’t that great at producing optimized code, so assembly was always much faster. That’s been increasingly less the case, as compilers and optimization techniques have gotten more advanced.

  • Nima

    Hello dear Alex.
    I have a problem with compilation time when using Eigen library.
    Simply with including .h directory (#include <Eigen/Dense>) you can use this library throughout your program.
    My problem is compilation time. It took so long in this case. (about one minute or more)
    I know this is a template library however can I reduce compilation time?
    Really thanks dear Alex.

  • My dear c++ Teacher,
    Please let me ask your suggestion on my problem.
    For the time being I work with Code::Blocks and do not know how can give first file of a project, name different than "main". With Dev it is easy.
    With regards and friendship.

  • My dear c++ Teacher,
    Please let me one more comment regarding Code::Blocks.
    You state:" You will see that the hello world program has already been written for you!".
    Indeed it is not that one you suggests! Then could you say that for help the beginner.
    When, long ago, I red it for first time was confusing. Eventually with God I understood.
    With regards and friendship.

  • My dear c++ Teacher,
    Please let me say that regarding Code:Blocks you state:
    "Open the tree under “Console Application”, open “Sources”, and double click on “main.cpp”.".
    But tree is:

    Above tree there is:

    With regards and friendship.

  • My dear c++ Teacher,
    Please let me this question: is your editor "visual studio code"?
    With regards and friendship.

    • Alex

      No, Visual Studio Code is a separate product that Microsoft now produces. It is more of a lightweight code editor. You can plug compilers into it, but it’s not trivial to do. Personally, I don’t use it.

  • Dahare

    Hi! I am using code::block. When I create a new project, I don’t find "Sources" under "Console application". I cannot start writing the code. Why is this?  How can I find it?

    • Alex

      It’s possible that newer versions of Code::Blocks have changed the UI slightly. When you create a new project, it should start you with a sample hello world program. Where does that program live within the Projects tab?

      • Dahare

        I got it. Thanks. I am  new to programming so I lack observing where things are. I got it under the projects tab.  I was looking up in the Resources tab.

      • Mahendra

        I made "helloworld" as a new project step by step as instructed, it also bulid and run smoothly. But when I tried to edit the program statement like:

        original --> cout << "Hello world!" << endl;

        Edited by me  --> cout << " Hello world!" << endl; // I just introduced one space before the word Hello.

        result on ctrl+f9 -->

        === Build: Debug in helloworld (compiler: GNU GCC Compiler) ===

        ld.exe   cannot open output file binDebughelloworld.exe Permission denied

                 error: ld returned 1 exit status

        === Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===

        This error comes on again even after removing that space before the word Hello(making it as it was originally written), but after 10-15 minutes(after removing of extra space) it build and runs successfully on ctrl+f9 & ctrl+f10. WHAT IS WRONG ?

        • Alex

          I can think of two possibilities:
          1) Your executable is still running when you try to recompile it, so the compiler is unable to overwrite the old executable with the new one.
          2) Your virus scanner (assuming you have one) may be interfering. Try disabling it and see if the problem disappears.

          • Mahendra

            1) I don’t think executable is still running because on very first build and run process(when I didn’t introduce a space before the word "Hello") it shows the execution time like 0.182 seconds on the output screen and also says to press any key to continue. Then how is it possible that it still in progress?

            2) I also tried after disabling system antivirus but nothing changed.

            Another problem: After making the very first program "hello world"(without introducing any space before the word "Hello") when I press build(ctrl+f9) it shows:

            === Build file: “no target” in “no project” (compiler: unknown) ===

            === Build finished: 0 error(s), 0 warning(s) (0 minute(s), 10 second(s)) ===

            Why “compiler: unknown” is showing in the first line? Although program executes successfully.

            IMPORTANT NOTE : I’M THE SAME PERSON WHO’S ASKING "[-std=c++14]" PROBLEM IN CHAPTER 0.5 Installing an Integrated Development Environment (IDE) COMMENTS SECTION.

            Here’s our full conversation till now (read between "****" lines):

            May 9, 2017 at 2:17 am · Reply
            After installing code blocks,in global compiler settings I found the [-std=c++11] and also C++14 ISO C++ language standard [-std=c++14]. According to instructions I selected [std=c++14] and clicked ok. Now whenever I restart code blocks, the toolbar item called “Build Target” becomes faid in colour hence can’t switch between debug & release. Besides this, on restarting, under global compiler settings the [-std=c++14] is now vanished but [-std=c++11] is still there , so I have to check box C++11. On clicking other compiler options under compiler settings [-std=c++14] is showing. Every time when I opens code block the toolbar item called “Build Target” becomes faid or disabled by itself. What is the solution?

            May 9, 2017 at 12:03 pm · Reply
            When you start code::blocks, are you reopening a project? The build target should only be available once you’ve done so.

            May 10, 2017 at 2:27 am · Reply
            Is that means if I edit any previous program one or more time then I can’t get access to build and release function at the same time. Do I have to make new project again to get build and release function? And what about [-std=c++14] issue? why it vanished and present only in other compiler options tab(under compiler settings)?

            May 10, 2017 at 3:10 am · Reply
            Another problem: After making the very first program "hello world" when I press build(ctrl+f9) it shows:

            === Build file: “no target” in “no project” (compiler: unknown) ===

            === Build finished: 0 error(s), 0 warning(s) (0 minute(s), 10 second(s)) ===

            Why “compiler: unknown” is showing in the first line? Although program executes successfully.


            I hope this may clear some doubts.

            • Alex

              If it says press any key to continue, and you haven’t done that yet, then your executable is still running until you do. If you open your operating system’s task manager, you should be able to see whether the executable is still in memory or not.

              • Mahendra

                But I pressed key on asking to press any key to continue afterwards the black output screen vanishes itself, then how is it possible that process is still going on? I’m using 32 bit windows 7 Operating system, is there any possibility of occurring errors due to 32 bit and not having 64 bit system? Is there any different setting for code blocks to run in 32 bit system.

                • Alex

                  If you pressed a key and the console dialog went away, then your process has likely terminated. 32 vs 64 bit should make no difference here.

                  Do you have a virus scanner, and if so, have you tried disabling it?

  • My dear c++ Teacher,
    Please let me ask your help on my problem with visual studio community 2017 c++ compiler.
    I’m confused with projects and files.
    1. When I click File > Open > Project/Solution, a window appears with name "Open Project" and at left some directories and some of my projects. One of the directories is "Projects". Also in the main area there are other projects of mine.
    When I click "Projects" directory, other projects appear in the main area.
    Why this dispersion of projects?
    2. I have one question for the files too.
    When I click on some project and then on "Open", another list of directories and a file appears:
    .vs (project)
    Debug (project)
    selected project
    file with same name as selected project and with extension .sln

    When I click on selected project and then on "Open", another list appears:
    Debug (project)
    file with same name as selected project and extension .vcxproj

    Why so many files and duplicated projects?
    I searched in the web but did find answer.
    With regards and friendship.

    • My dear c++ Teacher,
      Please let me answer my own comment:
      To create a big project I need to learn programming by visual studio, so  to learn all its complexity.
      With regards and friendship.

    • Alex

      When you click Open > Project/Solution, Visual Studio is showing you what’s on your hard drive, so you can find a project or solution to open inside the IDE.

      By default, all of your projects and solutions are saved inside the Project directory. What you’re calling a “project” in #2 is actually a directory on disk.

      I can’t speak to why Visual Studio decided to use the directory structure they did -- you’d have to ask Microsoft. Regardless, you generally don’t need to worry about this, as it’s application specific and outside of core C++.

      • My dear c++ Teacher,
        Please accept my many thanks for you replied my message and for your helpful answer.
        I describe situation somehow better than in my first message.
        After clicking File > Open > Project/Solution appeared window’s name is "Open Project" and its path is
        C:/Users/Amandine/Documents/Visual Studio 2017/Projects
        In the list at left, under
        "Images" (with some picture at its left) there are 3 of my projects (with yellow rectangular at their left).
        Below them there is
        "Microsoft Visual Studio 2017" (with visual studio’s logo at left)
        and under it there is
        "Projects" (with a yellow rectangular at its left)
        In the main area there is a complete list of my projects (each with yellow rectangular), including the three at left column. So these three are somehow duplicated.
        It means projects are sub-directories of directory "Projects" although with same rectangular.
        However, initially, I given path to project
        "C:VC2005Projects" later "C:VS2017Projects" and now I leave the default path that is
        c:/users/amandine/documents/visual studio 2017/Projects
        By the way, what does picture at upper left corner in your comments mean?
        Indeed it is not your picture!
        With regards and friendship.
        P.S. Unfortunelly your site does not show backslash character, so in paths I use common slash. G.T.

  • Jarod

    I want to start programming in c++ but i am not always on wifi what is the best compiler or ide to use that is free?

  • My dear c++ Teacher,
    Please let me say in "If you’re using the Visual Studio IDE" section, 4th §, recommend: "place them in a subdirectory off of your C drive, such as C:VC2005Projects." Today’s Visual Studio edition is 2017. Then please change subdirectory’s address to C:VC2017Projects.
    Also I do not understand why VC instead of VS.
    With regards and friendship.

  • TJ

    Can you use  "using namespace std;" before "int main()" instead of typing "std::" for every string

  • Alice

    I’m using SSD drive, will it still be as good on D:/?
    Why do you recommend setting the projects folder on C:/?
    I did the next tutorial and it was 21.5 MB which is a lot for such empty program.
    Thank you for all the effort.

  • Luke

    im using Microsoft visual studio 2017 and when I try to build solution it just says

    1>---- Build started: Project: HelloWorld, Configuration: Debug Win32 ----
    1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\VC\VCTargets\Platforms\Win32\PlatformToolsets\v141\Toolset.targets(34,5): error MSB8036: The Windows SDK version 8.1 was not found. Install the required version of Windows SDK or change the SDK version in the project property pages or by right-clicking the solution and selecting "Retarget solution".
    1>Done building project "HelloWorld.vcxproj" -- FAILED.
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

    so what am I doing wrong

    • Alex

      What OS are you on? If you’re on Windows 10, make sure you’ve installed the Windows 10 SDK and that your console application is using it. If you’re on Windows 8, then I’m not sure (I don’t have any Win 8 machines). You could try installing the Windows 8.1 SDK:

      • Luke

        I installed the Windows 8.1 SDK but now its giving me a different error message
        1>---- Build started: Project: HelloWorld, Configuration: Release Win32 ----
        1>c:usersspoeluk20documentsvisual studio 2017projectshelloworldhelloworldstdafx.h(10): fatal error C1083: Cannot open include file: ‘stdio.h’: No such file or directory
        1>Done building project "HelloWorld.vcxproj" -- FAILED.
        ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

        • Alex

          It sounds like maybe something in your IDE’s configuration paths isn’t set up correctly. I’m not sure how to fix this. Maybe try doing a clean uninstall of the compiler and reinstall, making sure to install any SDKs in the right-hand pane that it allows?

  • fysher5

    when i want to find an IDE you talked all about Microsoft, or Windows, but what about using apple or a Mac

    • Alex

      Code::Blocks used to work for Mac but it looks like they don’t have enough Mac developers interested in helping them produce Mac versions any more. I’d probably recommend xcode on Mac now.

  • Joseph

    I was suuuuper confused, but then after a while a realized I had Microsoft Visual Code, which isn’t an IDE. :’)

  • Matthew

    Every time I Orin the program it closes almost instantly! Anyone know how to fix it?

  • C++ Student

    What if I’m using an app on android. I am unable to download code::block or other compilers listed. I downloaded Mobile C but I can’t find anything labeled project. I AM NOT COMPLAINING IF YOU HAVE NO SOLUTION. I love this website. GREAT WORK!

  • Mehdi

    Thanks a lot.this site is wondeful.

  • Sahaj

    what is wrong in this code??
    If I give input 1 2 3 4  then it shows output 2 0 0

  • Ashish Patel

    Hi Alex,

    After posting this question on Jan 1, I went through old comments and found similar post with your response pointing out win32 application and it helped me to correct the mistake. You have created a wonderful website and I would like to sincerely thank you. I am determined to finish all tutorials within a month. Wish you a very happy and healthy new year.

  • Ashish Patel

    I intalled Visual Studio Community 2015 on my 64 bit system that has Windows 10. When the first program code for "Hello World" was tried exactly as per the tutorial, I am getting following errors.

    Severity    Code    Description    Project    File    Line    Suppression State
    Error    LNK2019    unresolved external symbol _WinMain@16 referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ)    HelloWorld    c:\Users\babu\OneDrive\documents\visual studio 2015\Projects\HelloWorld\HelloWorld\MSVCRTD.lib(exe_winmain.obj)    1    

    Severity    Code    Description    Project    File    Line    Suppression State
    Error    LNK1120    1 unresolved externals    HelloWorld    c:\users\babu\onedrive\documents\visual studio 2015\Projects\HelloWorld\Debug\HelloWorld.exe    1    

    Can anyone guide me as to what steps need to be taken? Thank you.

    #include "stdafx.h"  
    #include <iostream>

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

    • Alex

      It sounds like you created a Win32 application instead of a console application. Try recreating your project, and make sure you choose console application this time.

  • Dio Dziban

    Visual Studio Community is A LOT more confusing on the left side and idk how to get anywhere. There’s no helloworld.cpp, and if there is idk how to get there. I waws following your tutorials with a mac running code::blocks up until 3.4 and then decided to switch to windows. It is a lot more confusing than code::blocks was, please help me find everything or point me to a tutorial where it shows me, thanks!

  • Ginnungagap

    I’m using code::blocks and I’m always getting this error:
    --------- Build: Debug in HelloWorld (compiler: GNU GCC Compiler)----------

    g++ -Wall -fexceptions -g  -c home/…/main.cpp -o obj/Debug/main.o
    /bin/sh: 1: g++: not found
    Process terminated with status 127 (0 minute(s), 0 second(s))
    0 error(s), 0 warning(s) (0 minute(s), 0 second(s))

    Sorry I know this request is dumb but I’ve searched it on google and I didn’t get an answer

    • Alex

      It sound like you probably didn’t install the version of Code::blocks that comes with G++. Make sure you downloaded and installed the version that comes bundled with MinGW.

  • Magis

    First of all, I want to say that your tutorials are the best I have found on the internet, and they are very simple and pedagogical. Thank you for that 😉

    But I have a problem when i compile my program. I use the Hello World! sentence, and change it to something else. But the words Hello world! still appears on to the screen when compiled, and not the sentence i want. I may have overlooked the answer in the tutorial, and if i have, it would be awesome if someone would show me where it is. 😉


    • Alex

      Hi Magis. That definitely shouldn’t be happening, so you’re likely doing something wrong. But it’s hard to say what. I also don’t know what compiler you’re using.

      Here’s the steps you should be taking:
      1) Make sure your program is not still running. This is important, or your compiler won’t be able to overwrite the executable.
      2) Change “Hello, World” to something else.
      3) Recompile. Make sure you see the file compile and link, and that there are no errors.
      4) Run the executable produced.

      You should see the new string. If that doesn’t work, maybe try finding the executable file on disk and check the timedate stamp. Try recompiling and see if it changes. Maybe it’s compiling a different file than it’s running or something bizarre like that.

  • Abhay

    Thanks Alex.A really good tutorial.

  • Jagdeep Singh

    What is the crazy reason that c++ is not selected default in ms vs?

    • Alex

      Microsoft says that in the long run they intend to make ALL the programming languages optional, and you will choose which ones you want. So far, they’ve only done this for C++ though.

  • umm, not to be mean, but i can do that with a  Batch file…

    @echo off

    echo Hello World!

    This is also much shorter…

    • Alex

      You can. You can also do it in Java, Python, Perl, or any number of other compiled or scripted languages. But this is a C++ tutorial, and we have to start somewhere.


    Some information for fellow Eclipse users. My university requires Eclipse and to start off, make sure you installed the C++ IDE of Eclipse not the Java IDE which is provided at the front page of
    The next step is to install a compiler because Eclipse has NO OWN C++ COMPILER. My uni recommended MinGW, other compilers may work as well but I can only tell what I did. When you downloaded the MinGW setup and installed it, mark the "mingw32-base" and "mingw32-gcc-g++" packages and apply the changes to install. To check if you’ve installed it correctly, open the cmd of windows, type "g++" and enter. It should say at the end "compilation terminated".
    It is possible that you’re now able to build binaries and run the compiler but to make sure, you’ve to check if Eclipse finds the compiler’s path. Go to your Windows System > Advanced System Settings > Environment Variables. Then look for your User Variable called "Path". If there is none, create it. Now look if the path to your MinGW bin-folder is written in there. If not, search the bin-folder of MinGW up and paste the path in there. My path was something like this "C:\MinGW\bin".
    If you try to compile the Hello_World.cpp now and the console throws the error "program ‘make’ wasn’t found" or something like that, go into the MinGW bin-folder, look out for a file named mingw32-make.exe and rename it to the wanted "make.exe". Try to run the program again, it should success.

    Quite a damn hassle to figure this all out.

    • SomeNEEEERD

      Instead of changing the file name. Just open "Project" when you’ve created a project folder, then "Properties" > C/C++ Build > Change "Builder type" to "Internal Builder".

  • GEScott71

    My Hello World worked the first time - thanks!  Using Visual Studio 2015 on Windows 7.

  • Hey Alex, I just wanted to let you know that I really appreciate your effort into creating this website that has almost sprouted into a community, and I just have one question: Do you work full time on this website, or do you have another full time job other than this website
    By the way, I just started my journey into programming today 😀

    • By the way, is it weird I only get the "========== Build: 0 succeeded, 0 failed, 1 up-to-date, 0 skipped ==========" and not get the "succeeded" and the "compiling…" words?

      • Alex

        Most likely not weird at all. The compiler is telling you that it didn’t need to recompile 1 file because it was already compiled previously and hadn’t changed.

    • Alex

      I have a full time job outside of this website. This is just a passion project I’ve been working on for the last 9 years.

      Good luck in your journey!

  • Gurpreet

    i installed video studio but i don’t have "win32 console application" as a template. What should i do?

  • John Clark

    I’m having problems with my first compile Visual Studio HelloWorld.cpp

    #include  gives error -- expected file name found new line

    <iostream> gives error --- expected a declaration    

    std::cout << "Hello  gives error -- missing closing quote    
    same error for:

            world!" <<


    #include "stdafx.h"

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


  • Amy

    when i put the code in codechef, nothing happened.
    May i find out why?
    Thank you!!

  • Abdurahim

    Thanks Mr Alex.
    I did it during 2 week ( Installing IDE and writing "Hello World" )
    in that time I met different problems ( Can not installing IDE’s and Ftal Errors )
    But I fixed all of them ( even I reinstalled My Windows )
    Now everything ok !

  • iype

    I use code::blocks IDE but when i tried to build my project (for HelloWorld) it tells me that there is an "environment error" and that "can’t find compiler executable in your configured search path’s for GNU GCC compiler" what can i do about it???
    Moreover i installed the one with mingw.

    • Alex

      Hmm, something must have gone weird with the install. The minGW version should have set up the compiler.

      Try going to settings, compiler, and choose “reset to defaults”.

  • Heesoo Yang

    • Alex

      I don’t know why stdafx.h includes stdio.h and tchar.h by default. You can probably remove them. stdio.h isn’t even a C++ header, it’s a C header that includes legacy (C) input/output routines.

      That said, if you’re using precompiled headers in Visual Studio, you must have stdafx.h as the first line of any .cpp file, or Visual Studio won’t compile properly. If you don’t like this, you can disable precompiled headers, but your program may compile slower.

  • nikos-13

    I use code::blocks IDE but when i tried to build my project (for HelloWorld) it tells me that ther is an "environment error" and that "can’t find compiler executable in your configured search path’s for GNU GCC compiler" what can i do about it???

  • Patrick

    I have tried it several times and every time it does not work, it says it does not recognise the word cout?? I am not sure what to do as have typed it out exactly as instructed, Am using a new laptop with Windows 10 and the visual studios ide

    • Patrick

      I have figured out where i made my initial mistake however I am still seeing errors on line 1 . it says

      Severity    Code    Description    Project    File    Line    Suppression State
      Error    LNK2019    unresolved external symbol _WinMain@16 referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ)    HelloWorld    c:\Users\Patrick\documents\visual studio 2015\Projects\HelloWorld\HelloWorld\MSVCRTD.lib(exe_winmain.obj)    1

  • daniel bayona

    hello i have a question, i have a mac, so with this tutorial i can learn? and if i used xcode will be the same as using visual studio? and a can copy de  archives of xcode to visual studuio? thanks

  • abdullh

    first of all thanx for this awsome tutorial
    i have a problem when creating a new project in visual basic thscreen appeared as you said but there are nothing to chose from no 32 console or any other think and i made sure visual c++ is selected please help and thanx again

  • Brandon

    Hello Alex,

    I am currently trying to use Visual Studio 2015 for this tutorial and I kept getting several errors. After a little while of investigating, I found that my project doesn’t contain source files in the Solution Explorer like it does in your 2005 example. Do you have any idea what the problem could be? Thanks!

    • Brandon

      Never mind, Alex. I figured out what I was doing wrong. After clicking "New Project" I was clicking the "Console Application" option under the "Visual C#" tab instead of the "Visual C++" then "Win32" tabs. Thanks so much for this tutorial! It’s a wonderful place to start my journey in creating my first video game!

  • Roshan

    "can’t find compiler executable in your search path (GNU GCC compiler)" Please help me fix this issue in Codeblocks

  • Ronak

    i am Using DOSBox

    How will the codes differ in my case?

  • Hello Alex,
    this program is not working with online tools. Please help


  • Srilekha

    I’m following the tutorial in my mobile .I don’t know how to install these IDE and all . can you help me?

  • Avery T


    This is how to create a project with Xcode 7.3.1 for  Macs.

    1. File > New > Project
    2. Select OS X > Application > Command Line Tool > Click "Next" >"Choose options for your new project" appears
    3. Type "Hello World" in "Product Name" box > In "Organization Identifier" type "cpp" >  In "Language" select "C++" > Click "Next"
    4. Click "Create"
    5. On the left side of the project window is the project navigator, which lists all the files in your project select "main.cpp" to begin coding
    6. In the text editor you should see, select/delete the following :

    int main(int argc, const char * argv[]){
        // insert code here…  
        std::cout << "Hello, World!n";
        return 0;

    7. Copy/paste:

    #include <iostream>

    int main(int argc, const char * argv[]) {
        // insert code here…
        std::cout << "Hello, World!n";
        return 0;

    8. Press the Play icon on the top left corner of the screen. The output of the program should appear in the debug console at the bottom of the project window.

    9. Click the disclosure triangle next to the Products folder in the project navigator to show the application you built. Select the application, right-click, and choose Show in Finder. Doing this will show your application in the Finder so you can run the application outside of Xcode.

    • Alex

      Thanks! This will be super helpful for Mac users. I appreciate you taking the time to share.

      • Hi Alex and Avery

        Great tutorials so far and yes, I am a Mac user! The new project (above) with Xcode doesn’t work! Well, it does, and thanks for the info on how to create a new project, Avery - however, I’m getting an error message saying "Redefinition of main" and its highlighting in red the
        int main(int argc, const char * argv[]) part of the code. so the ‘main’ part seems to be wrong

        I downloaded Xcode yesterday (April 2016) so maybe its changed - it is also Xcode 8.3.1

        I’m wondering if I should either get Parallels or just buy a PC to learn C++

        Thanks for any help in advance.

        • Oops!  I meant to say 2017, doh!

        • Alex

          Redefinition of main() means the compiler is finding two main functions. It sounds like xcode added the main(int argc, const char * argv[]) function for you. Maybe you added your own main() function afterward? Try removing the main(int argc, const char * argv[]) function and see if it compiles.

          • Thanks (big time) for replying Alex. I didn’t add anything of my own - just copied and pasted from above. So I’m now thinking that, as I’m a lowly a carpenter/joiner, I’m totally out of my depth here and struggling already if I’m honests, but, as I liked doing coding with my degree (html), I’m going to keep going and see if I can actually learn anything from the rest of the tutorials - I’m guessing that if I persevere I can see the error of my ways! Thanks again Alex, pretty sure this aint my last message either 🙂

            • Alex

              I’m confused. 🙂 You said you copied my code, but my code uses int main(), not int main(int argc, const char * argv[]). So where did int main(int argc, const char * argv[]) come from?

              • Er, it came from Avery’s post above - sorry Avery!

                So I tried it without the (int argc, const char * argv[) and… IT WORKED!! Build Successful!

                Thanks so much Alex … onwards and upwards!

  • Adrian

    *tries to debug*
    "Access is denied.
    Press any key to continue . . ."

    I’m not sure what’s going on. I ran Visual studio as administrator but i still get the same message. Any Idea??

  • David

    Im using vs express 2013 and i tried this and keep getting the error LNK2019 and error LNK1120. How do i fix this?

  • Ye Kyaw Tun

    Thank you. I already did it. Now i am moving to
    1.1. 🙂

  • Ye Kyaw Tun

    In my IDE, when i run the "Hello World" program, it appears and disappears in very short time.
    How can i handle it?

    Thank you very much for your support and help
    Ye Kyaw Tun

  • Ady20

    Well I am completely lost. You told me to install Visual 2015, then you show me how to program in Visual 2005 lol. Visual 2005 compared to 2015 it’s like a whole new program. There are no similarities at all lol.

    • Alex

      Where are you lost?

      I’ll take a note to update the screenshots. It’s a pain to do every new release, but once every decade isn’t too bad. 🙂

  • Alex Keel

    I was having an issue getting the compile to succeed in Visual Studio Community after deleting the pre-entered code.

    ========== Build: 0 succeeded, 0 failed, 1 up-to-date, 0 skipped ==========

    For anyone else having this issue, I clicked "Build", "Clean Solution", and then compiled again:

    ========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

  • Akshay

    Really felt its what I needed. I have earlier used c   and been through the programming phase yet felt that something is missing and this is what that completes what’s left .
          The work is really commendable.

  • Shivam

    I am using codeblocks and whenever I start doing anything, an error appears, "Can’t find compiler executable in your confirmed search path’s for GNU GCC compiler." Please help

  • Paul Rombach

    Finally got it to work on VS2015.
    First try, it kept asking for a pch.h precompiler, so i substituted that for stdafx.h, no success

    I finally reviewed the common problems in lesson 7 and found the lnk2019 error - i was using the wrong type of program.

    I switched to the win32 program type, it pre-populated teh hello world with 50% of the text, so I added what was missing and success.

    For VS2015 - check the  sdk - make sure that you installed it and that the program is pointing to it.  you might need to retarget to the right version (right mouse click on the hello world program header in the  search solution explorer).

  • Daphne

    If your command prompt disappears immediately - try to compile the program with ctrl+f5.

    • Alex K

      If that doesn’t work in Visual Studio, try adding

      like this:

      It will force the prompt to stay frozen until user input. 🙂

  • Eviax

    I’ve installed Visual Studio 2015 and selected Console Application. I tried to name my project HelloWorld.cpp and whenever I write that code it finds about 14 errors and won’t go past that. It’s pretty weird! I haven’t go a clue how to fix it. Do you have anything on your mind?

  • Thanks. I am trying to compile hello world program in C in Dev c++ compiler but getting compilation error.

  • Sid McMaximoso

    Visual Studio 2015 is taking hours to install…

  • Alex brother i am using codeblocks and when i tried to compile the hello world program it shows me some errors like as stated below….


    C:cbprojectsjo.c:1:20: fatal error: iostream: No such file or directory
    #include <iostream>
    compilation terminated.
    Process terminated with status 1 (0 minute(s), 2 second(s))
    1 error(s), 0 warning(s) (0 minute(s), 2 second(s))


    ||=== Build file: "no target" in "no project" (compiler: unknown) ===|
    C:cbprojects  jo.c|1|fatal error: iostream: No such file or directory|
    ||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 2 second(s)) ===|

    i think i have mistaken somewhere please help me

    and one more thing which compllier i have to use it should br gnu gcc complier or any other

  • Stephen

    Hi Alex
    I have downloaded Visual Studio Community 2015. I have all the C++ template installed. I tried to create a new project, but the dialog box simply disappear for 1 second and reappears when I click the OK button. I’ve searched for the solution but no luck. Do you have any idea on this?

    On a special note, I cannot even use the devenv command that I found online. I have navigated to the directory which contain the devenv.exe and nothing happens when I enter the command.

    • Alex

      I have no idea, unfortunately. I’ve never heard of such a thing happening.

      • Stephen

        I have figured it out!

        For my case, it has to do with the language pack. The visual studio I have was the Chinese version. I installed the English language pack and I can’t create new project all of a sudden. Later, I switch back to Chinese and it works fine. As it turns out the language pack mess up the project creation for some reason.

  • Miles

    Hi Alex, I’m having an issue with VS 2015.  I ran the program once and got the cmd window just as pictured.  However, after attempting to write a few more lines to output and using ctrl+f5 again, Visual studio no longer runs a cmd window.  Now I just get this big empty white box.

    • Alex

      Try disabling your virus scanner if you have one. Sometimes they can interfere with the command window.

      • Miles

        I already attempted that, with no success.  The window that opens looks like an App (it gets the green little gear icon as it’s opening.)

        Is there any menu/area where I can choose how the program runs?

        • Alex

          The debug menu contains your options to launch the program. Visual Studio might have some settings in the project settings, but I’m not sure where they are if they exist.

          If you go back to your previous program that worked, does it still work? Maybe something is wrong with your program.

  • Emile winworth

    Thankz Alex. I am a beginner in C++ and I am using VS 2015. I did the HelloWorld successfully.

  • Mitchell

    Unfortunately that returns everything to default, which is all grey save for a few bits of syntax (comments, quotes, and keywords).

  • Mitchell

    I’m using visual studio 2015 but no text color, except when i use int, is being changed. It’s all grey. How do i get mine to actually color the text?

  • PurpleKitty

    Code:Block isnt compiling the helloworld. its stating "robably the toolchain path within the compiler options is not setup correctly?! (Do you have a compiler installed?)
    Goto "Settings->Compiler…->Global compiler settings->GNU GCC Compiler->Toolchain executables" and fix the compiler’s setup.

  • Arash

    Hello Alex and Hello World!

    When I copy the exact same thing from the above and click on "start without debugging" a blank CMD window shows up and when I click again on build a solution twice this shows up "Error LNK 1168" What could it be?

    Thank you.

    • Alex

      According to Google, error LNK 1168 means the program couldn’t open the file for writing. Either it’s locked for some reason, your anti-virus is interfering, or maybe the path it’s trying to write to doesn’t exist.

  • GusN

    I’m trying to do this in Eclipse on Windows, when I try to run it, it says "Launch Failed. Binary not found."

  • Immortal

    Wow nice c++ manual!

    Also as someone else has said, the comments are making the scroll bar to-infinity lol, keeping them hidden is not only less memory/ect but also shows how long the course is, you should put a "click" button to show/hide all the comments.

  • radhika

    thi is what the code::block show:
    "helloworld - Debug": The compiler’s setup (GNU GCC Compiler) is invalid, so Code::Blocks cannot find/run the compiler.
    Probably the toolchain path within the compiler options is not setup correctly?! (Do you have a compiler installed?)
    Goto "Settings->Compiler…->Global compiler settings->GNU GCC Compiler->Toolchain executables" and fix the compiler’s setup.
    Nothing to be done (all items are up-to-date).

    Target uses an invalid compiler; run aborted

  • Omar

    Visual Studio seems to have expired for me. Do I need to buy it?

  • Thanks for creating these awesome tutorials! 😀

  • Jacob

    Hi sir,

    I clicked run and it said invalid compiler. After fiddling around, I found that the program can’t find the location of the compilers even though they are in the default area in the program folder. Any suggestions?

    Thank you for your time on creating a site like these for people like us.

  • Trenton

    I downloaded the newest version if Visual Studios, and nothing is as shown above. Could you please walk me through this

  • Zachary Weisman

    What do I do if i want to use Xcode

    • Alex

      I don’t have XCode installed, so I can’t say for sure. But based on what I’m seeing elsewhere, install XCode, and then create a project as follows:

      File->New->Project, go to OSX->Application and choose Command Line Tool. Give your project a name and make sure the type is C++. Then paste in the code from the tutorial above.

  • Jeremiah

    I don’t see the "Build Solution" in the Build menu. I’m only presented with one option "Run Code Analysis on Solution".
    Also, "Start Without Debugging" in the Debug menu is grayed out. I’m using Visual Studio 2015 Community.

  • Nlen

    I ran HelloWorld just fine using MS Blend, but then I decided to change
    to code, by adding int x = 4; and printing that out.  Now MSBlend claims
    that access to helloworld.exe is denied.  The problem survives a reboot.
    I have tried numerous frustrated times.  Can you tell me how to re-build
    and re-run code once you have changed it?  Right now it seems like MS
    Blend gets in your way more than it helps.

  • ichabod

    I am using codeblock 13.12 and have a similar problem as one of the commenters above. When I run after building a black DOS screen pops up but does not have any writing. I selected the GNU GCC compiler and the text read exactly like the example show above of a successful build:
    --------- Build: Debug in helloworld (compiler: GNU GCC Compiler)----------

    mingw32-g++.exe -Wall -fexceptions -g  -c "C:\Users\USER\Documents\codeblock experiment\helloworld\main.cpp" -o obj\Debug\main.o
    mingw32-g++.exe  -o bin\Debug\helloworld.exe obj\Debug\main.o  
    Output file is bin\Debug\helloworld.exe with size 943.40 KB
    Process terminated with status 0 (0 minute(s), 14 second(s))
    0 error(s), 0 warning(s) (0 minute(s), 14 second(s))
    How do I fix this?
    Please help me. I feel so lost

  • Sam

    what is std and cout

  • naveed

    i have installed Visual Studio Community 2015. as per your instructions  when i click on new project it shows the window which is different from the one you have shown in your lesson i m using window 10

  • Andrew

    Im not sure why my code wont compile

    1>--- Build started: Project: HelloWorld, Configuration: Debug Win32 ---
    1>MSVCRTD.lib(exe_winmain.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ)
    1>c:\users\andrew\documents\visual studio 2015\Projects\HelloWorld\Debug\HelloWorld.exe : fatal error LNK1120: 1 unresolved externals
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

    • Alex

      I think you may have created the wrong kind of project. You want to create a console project. You can either recreate your project, or change Project properties -> C/C++ -> Linker -> System -> SubSystem to “Console (/SUBSYSTEM:CONSOLE)”

      • Nue

        Hey Alex why is cmd putting "Press any key to continue" on the same line as my last statement such as

        Hello World!Press any key to continue . . .

        instead of putting it on the next available line like it use to, like

        Hello World!
        Press any key to continue . . .

        BTW thanks for these tutorials. your making it extremely easy to learn cpp and I appreciate everything

        • Alex

          Because we only sent “Hello world!” to the console, not “Hello world!” and a newline character.

          If you want Press any key to continue to be on its own line, change:

          To either of these lines:

          We’ll talk about both methods in future lessons: std::endl in chapter 1, and \n in chapter 2.


    I am getting this error on visual studio

    Error    LNK2019    unresolved external symbol _WinMain@16 referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ)    HelloWorld        1

    Error    LNK1120    1 unresolved externals    HelloWorld

    • Alex

      For some reason, the compiler isn’t finding your main() function. Are you sure your main() function is named main (and not Main(), or something else)?

  • miguel

    I finallly managed to get through the HelloWorld program without being thoroughly disgusted and put to sleep.  congrats!

    I am an old man, five plus decades old, and had been trying to "get interested" in programming with c++ for years (more than 10 years).

    Not being put to sleep or gotten thorougly disgusted in the first lesson has been quite an accomplishment in both, your side and mine. (I know it with certaintity from my side).  

    What’s that thing about translating into spanish?  i might help you with it. I have lots of time right now. Let me know.   And once again, thanks for the tutorial. It really made a difference.  I managed to stay interested in the next lesson. (it took me many years to get past the HelloWorld program.  I’ll be back for more)

  • Juan Carlos Quirino

    Hi alex, im wondering if you are interested in translating into spanish

  • Meg Medicke

    Hi Alex,
    This is a FANTASTIC tutorial!  Many thanks for putting your expertise out there to help the rest of us.  I could easily follow your step-by-step approach and the in-depth detail/screen-prints let me check what I was doing each step of the way.  I have been trying to learn C coding on and off for years, but usually hit a brick wall/get frustrated when an instruction isn’t clear…or gets left out entirely. I was thrilled to be able to complete the lesson, quickly!  🙂


    I love this site 😀

  • Bill

    I’m using the sublime text 2. I copied the code
    #include <iostream>

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

    And i got this error…Did i do something wrong? used the wrong "Text Editor" or what happened… Any help would be appericiated … (Sorry for the mispelling English is not my first language )

    Here is the error I am getting.

    [Error 2] The system cannot find the file specified
    [cmd:  [u’g++’, u”, u’-o’, u’/’]]
    [dir:  C:\Users\Bill\AppData\Roaming\Sublime Text 2\Packages\User]
    [path: C:\Program Files (x86)\AMD APP\bin\x86_64;C:\Program Files (x86)\AMD APP\bin\x86;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\hp\bin\Python;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static]

  • Gregory Smith

    1>---- Build started: Project: Firsttest, Configuration: Debug Win32 ----
    1>  Firsttest.cpp
    1>MSVCRTD.lib(exe_winmain.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ)
    1>c:\users\greg\documents\visual studio 2015\Projects\Firsttest\Debug\Firsttest.exe : fatal error LNK1120: 1 unresolved externals
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

    Not really sure why mine’s not running, I copied and pasted and then wrote out myself and the same thing happened, maybe it’s to do with how I set my visual studio up. Help will be much appreciated, thanks in advance to anyone that will.

    • Gregory Smith

      Hi don’t worry I worked out it was how I was opening my new projects, thanks anyway.

      • Alex

        How were you opening your new projects, and how did you fix it? Other users may encounter the same issue, and it would be nice to know how you addressed it.

    • Nate

      If you are using Windows 8 or 8.1 with Visual Basic 2015:

      New Project > select Win32 Project > give a name to your project and select a save location > click OK

      In the window that pops up:

      Click on "Next" or select "Application Settings" at the left > select "Console Application" > uncheck the boxes for "Precompiled headers" and "Security Development Lifecycle (SDL) checks"

      Click Finish

      Your code should work now!

  • Thomas Keith


    I commend you for keeping up this site for so many years.  I’m looking at retiring soon, probably becoming an expat since it’s so expensive to live in the US, and want to be able to make some income from anywhere there is a decent internet connection.  So, programming seems to be the answer, and C++ can be a nearly universal answer.

    However, I have run into an issue.  I installed Visual Studio 2015 and followed all your installation advice, but whenever I successfully compile, execution shows a flash of an empty DOS box.  Attempts to make the box persistent have been fruitless, and the box seems empty of my text, though it is just a flash and I can’t be sure of that.  I am copying and pasting your code, so I don’t think that’s it.

    Help me, Obiwan, er.. I mean Alex, you’re my only hope.


  • Gurjot

    Can i use codechef compiler as an alternative

  • Madhav

    I am using community 2013
    whenever i am writing Hello world.cpp
    #include "stdafx.h"
    #include <iostream>

    int main()
        std::cout << "Hello world!" << std::endl;
        return 0;
    // The test result show me “this project is out of date”..
    would you like to continue
    when i go to continue it show me path file error of win32 console

  • troll

    thanks alex for doing this website. A great idea that has helped a lot. I found this site while I was looking for a great gaming language to code in after learning javascript, processing, HTML5, CSS, and SQL in that order. C++, with your site, will easily be my next language.

  • Zerocool

    I am using Visual Studio 2015 and it doesn’t let you choose any type of Template other than ‘Blank App(Universal Window). But when you
    R-Click>Properies>Configuration. It shows that the platform is indeed Win32.

    When I type in:

    #include "stdafx.h"
    #include <iostream.h>

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

    I get two Errors in the Error List

    Severity    Code    Description    Project    File    Line
    Error    C3940    ‘EventRegistrationToken’: identifier not found - possible mismatch between compiler and library versions. Please ensure vccorlib.h/.lib, vccorlib120.dll and c1xx.dll match    App1    c:\program files (x86)\microsoft visual studio 14.0\vc\include\vccorlib.h    348

    Severity    Code    Description    Project    File    Line
    Error    C1903    unable to recover from previous error(s); stopping compilation    App1    c:\program files (x86)\microsoft visual studio 14.0\vc\include\vccorlib.h    348

    These occur when I try to Build the project and bring it to life.

    Any help would be appreciated.


    • Zerocool

      An error on my part previously, I was using Microsoft Blend for Visual Studio 2015… I just searched Visual Studio 2015 on my search bar and came up with a different one. When you choose New Project it gives you several choices to choose the different type of project, and Win32 is one of them.

      This time I input the previous lines of code and it worked when I tried the CTRL+F5.

      Took some trial and error.. but I got it figured out!

  • jojo

    when and why we create empty project in VS??????

  • milan subba

    there is no win32 project in visual studio 2015.. what i have to do next.. i am stuck here.

    • Alex

      When creating a new Project, you looked under Templates -> Visual C++ -> Win32?

      Visual Studio 2015 Community doesn’t install C++ by default, you have to turn it on when you install. Did you do that?

    • Tom

      Have you just opened up Visual Studio for the first time? If so, there should be a ‘skip this part’ link somewhere. Then complete the set up and go in and open a new project as described above.

  • Rasmo

    on my first try it failed, but i looked through and found my mistake, i feel like i have lifted a truck :D.

  • Obada Salman

    hello all,i got a problem,the code isnt working,here is the error:

    Error    1    error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup    C:\VC2005Projects\HelloWorld\HelloWorld\MSVCRTD.lib(crtexew.obj)    HelloWorld

    and this one:

    Error    2    error LNK1120: 1 unresolved externals    C:\VC2005Projects\HelloWorld\Debug\HelloWorld.exe    HelloWorld

    please help =(

    • Ike Anderson

      I had the same problem!  But I found a solution.  

      My problem/solution seemed to stem from when I created the project.  I am using Visual Studio 2015 and when you create a new project and you select Win32 from the left hand side TREE (not the window) it gave me two options for the type of project template to use: "Win32 Console Application" and "Win32 Project".  MAKE SURE THAT YOU HAVE SELECTED "WIN32 CONSOLE APPLICATION"!!!!!

      When I tried with that, I no longer had those pesky LNK1120 and LNK2019 Linker errors.

  • Yujian Hong

    Best program ever

  • Francisco Mendez

  • djconcarne

    Some constructive criticism (I hope!). I followed pretty well until I got to here:

    "In the text editor, you will see that VC2005 has already created some code for you. Select and delete the _tmain function, and then type/copy the following into your compiler"

    I feel there are a number of things unclear here. Firstly, this step fails to introduce the concept of a "function", so I don’t know exactly what to delete (The word "_tmain" or that plus everything following within the parentheses, brackets, curly brackets?)

    Second, why am I deleting this? What purpose does it serve and why do we not need it in this/every case?

    Immediately following is a string of foreign text which we are asked to copy/paste into the text editor. What is <iostream>? std, endl, etc?

    I can clearly see what I should delete and what my code should look like in the next image, but I like to understand why I should do things and in what cases. "What does it mean?!?"

    Now I am sure you go into this in detail in the following tutorials, and maybe it’s the way I learn, but I like to build a foundation before i jump into something big. (Yes, this is a tiny program, but it may be daunting to a newcomer.)

    EDIT: Just read the last line of this lesson. 😀

    • Alex

      This is good feedback. Let me address it:

      1) I updated the lesson to indicate you should delete _all_ of the code that Visual Studio provides, and replace it with the provided example. Hopefully that’s clearer.
      2) Visual Studio “helpfully” provides a “blank” program for you to fill in. However, in doing so, it includes both a few things that we don’t need, and a few things that only work in Visual Studio. Our replacement is simpler, does the same thing, and will work on all compilers.
      3) Generally, I’ll take one of two approaches: I’ll talk about something, and then show you an example, or I’ll show you an example, and then dissect it. This is one of the few cases where I don’t do that (immediately). The goal here is to get something compiling, and to root out any problems that may be encountered while trying to compile a simple program. That way, when we do get to the point where each line is explained, we won’t have a bunch of people complaining they can’t get it to work.

  • Haris

    its not working for me
    this is what i typed in
    // HelloWorld.cpp : Defines the entry point for the console application.

    #include "stdafx.h"

    #include <iostream>

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

    and this is what it shows
    1>---- Build started: Project: HelloWorld, Configuration: Debug Win32 ----
    1>  HelloWorld.cpp
    1>LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

  • Methos

    I’m actually up to section 4, but this seemed to be a better place for the question. I’m using Dev C++ (which I don’t think is the issue). When I start a new project, and the first time I compile, Windows wants to know where to store things, and I wanting easy access to anything I’ve done as part of your lessons put a ‘Tutorials’ folder inside my Dev C++ folder.

    The thing is, when compiling, it tries to save main.cpp and finds that there’s already one there from a previous project. And since I want to be able to review previous projects, I have instead been renaming main.cpp (so for example, I would have Yo_World as a project and yo_world.cpp for its main file). So now I have a situation I suspect that practice has caused where the first time I attempt to compile I get a ‘couldn’t make makefile’ error which clears up if a quit out of Dev C++ and reload it and the project.

    So I guess my question is ‘Am I overthinking this?’ Which is to say, does the saved project contain a copy of main.cpp or does main.cpp only really exist once in my Tutorials folder (for whichever project I last left with default name).

    • Methos

      On further review, it looks like it’s main.o that is getting overwritten, not main.cpp. Part of my confusion came from the fact that I could load the other files from projects with multiple files to review how I did things in them, and I thought I might be able to the same with the various main.cpp’s I’ve been making. Alas, the only way to do that is load the other projects which replaces the current project.

      This as it turns out isn’t all that big a deal as I accidentally discovered I can have multiple instances of Dev C++ open at once.

      • Methos

        ufdah. Okay I was completely wrong. It is main.cpp that’s getting saved as I just did two projects in a row leaving main.cpp’s name unchanged and the second overwrote the first. When I renamed it for the second and reloaded the first project, I got an error that main.cpp couldn’t be found.

        So, I guess it’s necessary to rename main.cpp or save every project to a different folder (which I’d rather not do if I ever want to find them again).

  • I got it!

    So I think I had gotten a little turned around when creating the project in Visual Studio. I selected Win32, and then there was a dialog box that allows me to select console. This didn’t work out. On my third try I saw there was actually a selection called Win32 console, then the same dialog box pops up, but console is already selected at that point, and it all worked out.

    I don’t quite understand why though.

  • I am getting two errors when I tried to build the Hello World program:

    Error    1    error CVT1107: ‘C:\Program Files (x86)\Windows Kits\8.1\lib\winv6.3\um\x86\comdlg32.lib’ is corrupt    c:\Users\Xercius\documents\visual studio 2013\Projects\HelloWorld1\HelloWorld1\CVTRES    HelloWorld1


    Error    2    error LNK1123: failure during conversion to COFF: file invalid or corrupt    c:\Users\Xercius\documents\visual studio 2013\Projects\HelloWorld1\HelloWorld1\LINK    HelloWorld1

    I’ve been looking for solutions in these comments, in your 0.7 Common Problems section and just general searching online, but I haven’t found any fixes yet.

    I’ll keep searching, but does anyone recognize what the issue may be? I just installed MS Visual Studio Express 2013 for Windows Desktop last night and started working my way through the tutorial.


  • cpplx

    what happens to unused data(functions, constants…) in a source code when a program is compiled? included or excluded from the executable?

    • Alex

      It depends on your compiler and linker settings. Most modern compiler optimize out unused static data, but leave unused global data by default.

  • Alex

    If you followed the instructions precisely and created the right kind of project, then you should have a _tmain function in the center-right window pane where your code goes.

    You can look at what’s there by default and delete it using standard keyboard commands (e.g. arrows to move the cursor, and the delete key to delete).

    The code to replace it with follows the line you quoted.

    • william george sallaway

      When i search it is is listed under external dependencies-tchar.h-_tmain. is that the right one? (also using visual studio express 2013 if that makes a difference)

      EDIT: thank you i have now found it

  • william george sallaway

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

    Hi Alex, this is a stupid question but where do i search this and what exactly do I type?

  • A-x-E*

    Hello , i just started to learn C++.
    You tutorials are really beneficent for me thanks 🙂 but im having a problem while running that program which you mentioned about there.Im using  Microsoft Visual C++ 2010 express.The error which i found is

    Looking forwared for your help.

  • David

    Is there an explanation for Mac users? I have no idea what to do.
    I dont have any experience with programming btw.

    • Alex

      Although Code::Blocks is available for Mac, seems like a lot of Mac users are using xCode. I’d try downloading/installing xCode.

  • Obada Salman

    Hello friends! Can someone tell me why did I get this?
    // HelloWorld.cpp : Defines the entry point for the application.

    #include "stdafx.h"
    #include "HelloWorld.h"

    #define MAX_LOADSTRING 100

    // Global Variables:
    HINSTANCE hInst;                                // current instance
    TCHAR szTitle[MAX_LOADSTRING];                    // The title bar text
    TCHAR szWindowClass[MAX_LOADSTRING];            // the main window class name

    // Forward declarations of functions included in this code module:
    ATOM                MyRegisterClass(HINSTANCE hInstance);
    BOOL                InitInstance(HINSTANCE, int);

    int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,

    [Snipped to reduce length]

    Thanks alot!!!

  • Sean

    I’m using MVS express 2013 for windows desktop, and I’m wondering what the difference between shortcuts f10 and ctrl-f10 is. My program runs fine, but pressing f10 alone opens and closes the cmd windows very quickly, and the bottom of the screen becomes orange and says ready. What is happening here? Thanks in advance!

    • Alex

      The orange bar at the bottom means you’ve entered debugging mode. F10 executes the next line of code (but doesn’t enter any functions) and then stops. Ctrl-f10 runs to the currently selected line. Since we haven’t covered debugging yet, my advice is to use ctrl-f5 to run your programs in Visual Studio for now, at least until you get to the lessons that cover debugging.

  • Kud

    Hi peps,
    absolutely amazing website for beginner like me, thanks very much for the effort.

    i am mac user and using xcode Version 6.4, what should be the project version when i create new project?

    thank you very much!

  • ETN

    Hi Alex,
    I’m a complete newby!!
    I am using Code::Blocks 13.12 on Ubuntu.
    I created the project in CBProjects on C Drive - ‘Hello World’.

    Having no workspace to type in, I ‘opened’ the file ‘main.cpp’ and this is what appeared:

    [#include <iostream>

    using namespace std;

    int main()
        cout << "Hello world!" << endl;
        return 0;

    Wasn’t me! I didn’t copy or paste a thing!
    When I ran the program I got this on the Xterm:

    The whole file location ending in ‘:’, followed by:

    ‘Permission denied

    Process returned 126 (0x7E)   execution time : 0.007s
    Press ENTER to continue.’

    I tried to nut this out, checking all possibilities (ha, me . . a newby!!!)
    So I deleted the project and started from scratch.
    This time around, the whole build toolbar (including run) is greyed out.
    What don’t I know or am doing wrong??!!

    • Alex

      Sounds like a permission issue with your operating system. I’d try checking the permissions of the executable file to ensure it’s flagged as executable and owned by you. But beyond those basic troubleshooting steps, I won’t be of much help here. Perhaps another kind reader will be able to assist.

      You should also try Googling “code::blocks ubuntu permission denied” and see if any help comes up.

  • Sarthak

    I compiled the program successfully but there is no option as "start without debugging". I’m using Visual C++ 2010 express.

  • bobz


    I am attempting to follow the instructions using my Win7 laptop running Microsoft Visual Studio Express 2013 for Windows Desktop.

    From the beginning I do not get the _tmain function which you tell us we should delete?  I run Edit-Find looking for "tmain" and it comes up blank.

    I get this for the first bunch of lines:

  • Brian

    Hi, I don’t know if my problem is a corrupted installation or if I’m missing something. I’m running Visual Studio Express 2013 on Windows 8.1. When I’m in the New Project window, the only option I have under Visual C++ is Store Apps. I have already reinstalled the program, but the options remain the same.

    • Alex

      I think you may have installed “Express 2013 for Windows” instead of “Express 2013 for Windows Desktop”. Try downloading and installing the Windows Desktop version.

    • Brian

      I figured out what it was. I had to download the Windows Desktop version and run it as an administrator. I also get the "targetver.h" file.

    • Brian

      Sorry, I didn’t refresh the page before I posted the last comment. Thanks for your help though.

  • Niranjan.A.S

    Thank you sir,first VS gave me sme troubles,cause i didn’t included the i’am extremely happy..Thank you…

  • Casey

    So I’m using Xcode on a mac of course, and i made a new .cpp file and typed in all the code and even copy + pasted it from here, but some words are not the same color as they are in your examples, and that confuses me. But i did delete the first code that was in the file, it was something like this…

    Created by blah blah blah

    Then i put in the hello world code. After that i tried to run it up on the "product" drop down menu and the run button wont work because its in that darker grey font. I do NOT know how to run this. Please help!

  • Drew


    I am using Visual Studio 2013.  I got as far as "Code::Blocks"  I realize your example is from the 2005 version but I can not find or figure out how to replicate your instructions from this point on: "To create a new project in Code::Blocks, go to the File menu, and select New Project. A dialog box will pop up that looks like this:"

  • Dylan J

    Whenever I put into my code

    it gives me the error "[Error] stdafx.h: No such file or directory" I’ve tried changing the "’s to <‘s and leaving them off. It doesn’t work.

    (I’m using Dev C++)

    • Alex

      The need to #include “stdafx.h” is only required for Visual Studio, and only if you’re using precompiled headers. Since you’re not using Visual Studio, don’t worry about it. 🙂

  • Shane Boyd

    I am using the Visual Studio Express 2013 and I when I want to start a new project I can’t even get C++ as an option for a template, all that shows is C#. Also Win32 is not even an option to select either. I can’t seem to find any way around this issue.

    • Alex

      It sounds like your install is corrupted somehow. I have absolutely no idea how to fix this. Perhaps ask on Stack Overflow for ideas?

    • Ed Levy

      I am also using VSE 2013 and was able to install it correctly. If an uninstall and reinstall does not work then it might be that your computer is in an odd state. You may need to upgrade your OS in order to install VSE 2013. I once had to do this because I was having trouble installing National Instruments software for a class I was taking. Hope things work out for you….

  • Brian

    I’m using visual studio 2013 and whenever i try to open the console screen (ctr+F5)i get a notification that my project is out of date.How do I solve this

    • Alex

      If you’ve changed your project since pressing ctrl-F5, this is normal -- Visual Studio is telling you that it needs to rebuild the project. However, if you haven’t changed anything since your last run and you’re still getting this every time, then something is wrong with your project and/or configuration.

      There are some good ideas about how to diagnose this and/or fix this problem on Stack Overflow.

  • I’m trying to compile my helloworld.cpp from the command line but I don’t have g++.  Should I install the gnu c++ compiler or is there another option or should I just skip it?  I don’t plan on using the command line unless there is some dire urgency.
    FWIW: I used to do a lot of work with Pascal/Delphi.  I’ve decided to join the 21st century so I’m trying to plow thru this tutorial and learn C++.

    • Alex

      Compiling from the command line is for the folks who can’t or don’t want to install an IDE. If you already have installed an IDE, then just use that. If not, I recommend you install one and use it. It’ll make things simpler (especially debugging).

  • calvin

    hmmmm the first time i opened it it said some kind of dll was missing after that it worked great thanks man

  • Evan

    HALP MA PLZ!!!!!!!!!!!!!!!!!!!!
    OK now that i have your attention I’m using the free version of Microsoft visual studios and and it has a box with this inside
    Module Module1

        Sub Main()

        End Sub

    End Module
    i assumed i would have to put it on the outside of the box that didn’t work i looked more into it and saw the sub main so i put the hello world program and well it failed i assume I’m suppose to do something with the end sub but i have no idea what to do so as i stated in the beginning HALP MA PLZ!!!!!!!!!!!!!!!!!!!!

  • Raylee

    when i press ctrl+F10 in code::blocks that comes :
    Target uses an invalid compiler; run aborted

    • Alex

      This sounds like a job for Google Search! Try searching “Code::Blocks Target uses an invalid compiler; run aborted”.

      The top link seems promising.

  • Batuhan

    2005 express edition is better than 2010 i think


    Hi all…I was loving this tute right up until i went to start the hello world program we created. its saying im missing a bunch of kernels…

    ‘hello world.exe’: Loaded ‘C:\Users\chris\Documents\Visual Studio 2010\Projects\hello world\Debug\hello world.exe’, Symbols loaded.
    ‘hello world.exe’: Loaded ‘C:\Windows\SysWOW64\ntdll.dll’, Cannot find or open the PDB file
    ‘hello world.exe’: Loaded ‘C:\Windows\SysWOW64\kernel32.dll’, Cannot find or open the PDB file
    ‘hello world.exe’: Loaded ‘C:\Windows\SysWOW64\KernelBase.dll’, Cannot find or open the PDB file
    ‘hello world.exe’: Loaded ‘C:\Windows\SysWOW64\msvcp100d.dll’, Symbols loaded.
    ‘hello world.exe’: Loaded ‘C:\Windows\SysWOW64\msvcr100d.dll’, Symbols loaded.

    could this be because as well as visual c++ express i have the complete visual studio 2013 installed?

  • Kacy Gilbert

    Getting the same error==

    failure during conversion to COFF: file invalid or corrupt

    after writing the code exactly as its shown…could it be because i’m using VC2010 and not 2005?? what can i do to fix this?

  • Ethan

    I didn’t change the code at all but it always tells me

    1>MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup

    On Microsoft Visual C++ 2010 Express

    • Alex

      Sounds like you don’t have a main() function. Either your code is missing one, or your code isn’t being compiled into your project properly for some reason.

      • Ethan

        My code is exactly like the one above and it has the main()

        As I was browsing the internet looking for a solution there were a few other people who had the same problem.
        I heard somewhere about going to Project>HelloWorld Properties>Linker>System> and changing the SubSystem. I have no idea what this does but when I try debugging with SubSystem set to Console (/SUBSYSTEM:CONSOLE) instead of it on Windows (/SUBSYSTEM:WINDOWS) (which gave me the error) it doesn’t give me the error although I have no idea what SubSystem is/means or if I just changed something I shouldn’t have; I would be happy if you could tell me. And I did start the project as a console project.

  • Richard Crenwelge

    I followed the instructions for "hello world!", but I am getting this error message:

    1>---- Build started: Project: HelloWorld, Configuration: Debug Win32 ----
    1>  HelloWorld.cpp
    1>LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

    Since I have zero experience with this, I’m not sure how to interpret what needs to be fixed.  Help?  Thanks in advance.

  • Nate

    My code looks identical to the example and builds just fine. When i try to run it gives me “unable to start program” window. any suggestions.

  • "bob0711
    October 10, 2014 at 4:07 pm · Reply"

    My initial attempts at building having failed, I followed the clear advice under the above heading, with immediate success! Many thanks.

  • Cone

    I type in exactly what I'm supposed to, but I get this error:

    1>---- Build started: Project: helloworld, Configuration: Debug Win32 ----
    1>LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

    What am I doing wrong?

  • Akshit S

    like you said that we use iostream header file to make use of I/O functionality such as cout. Then why do we need to include "using namespace std" when the compiler is already aware of the existence of cout object in iostream header file?

    • Alex

      When you #include , it pulls all the declarations from the iostream header into your program. However, it’s up to you to access them properly.

      All of the functionality in iostream (and in all the C++ standard library headers) lives in a namespace called std. The compiler knows that cout lives within the std namespace. However, if you just write “cout” with no std:: prefix or using statement, it won’t assume you want the one in the std namespace. You have to explicitly tell it you want the cout that lives in the std namespace. There are a few different ways to do that, which are discussed further in section 1.3a -- A first look at cout, cin, endl, namespaces, and using statements.

  • Kevin You

    what is a C++ qt application.

  • macks2008

    Yay, I completed a hello world program in a new language 🙂
    I think it's my third overall, the first being Python, and second Java (which I got hung up on and didn't bother getting back to. I don't like being dependent on someone else's software, such as Oracle's JVM)
    now to figure out the rest of the language.. - something I'm sure this tutorial will help with. It hasn't let me down so far!

  • Raima

    thank you for the beautiful site .my first program is compile but i could not see it .

  • Jeremy Reilly

    Nevermind, I now see that it’s not a problem with the code, but an OS compatibility issue

  • Jeremy Reilly

    I am pretty sure I entered the code correctly, but I keep getting this as my output:

    1>---- Build started: Project: HelloWorld, Configuration: Debug Win32 ----
    1> HelloWorld.cpp
    1>LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

  • Kevin You

    How do you run a program without using CMD?

  • Yesenia

    Might be a dumb question but I’m new to this program and I have no idea how to work this. My professor wants us to make the program run by including the number 87. So far this is what I have but I only get errors. What is it that I need to fix?

    cout << "\n *** *********** \n"; << endl;
    cout << " * * ** ** \n"; << endl;
    cout << " * * ** \n"; << endl;
    cout << " * * ** \n"; << endl;
    cout << " *** ** \n"; << endl;
    cout << " * * ** \n"; << endl;
    cout << " * * ** \n"; << endl;
    cout << " * * ** \n"; << endl;
    cout << " *** ** \n"; << endl;

  • Derrick

    I am trying to compile my first program (HelloWorld) but it keeps failing and saying the file is corrupt. I’ve triple checked to make sure I am entering everything in correctly, anything you could suggest I check?

    • Bevan

      See the post about this just a few above.
      Bob0711 writes: ok for anybody using visualstudio 2010 and getting this error:
      1>LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt
      you need to do this:
      go to project properties under the project menu
      then go to configuration properties/linker/general
      once there change enable incremental linking to no

  • Thank you for your reply, Loganathan. I will forge ahead though I have copied and pasted the sections up until this point and have made a version of the Tutorial in Word format. I will take notes of these terms as I learn them, but I am hoping that they are explained. I am very excited to have these lessons, and, if you’re reading this, Alex, a huge thanks to you for your time and contribution to everyone else. Please don’t think I do not appreciate them - I should have kept reading…I will get back to it tomorrow, and please chat any time - I enjoy meeting new people and am determined to learn C++.

    Thank you for your time.

  • Randy Wood

    I would like to fully understand each concept in this Tutorial before advancing to the next one.

    I will describe each line as I understand them, but am wondering if anyone can help me to learn more about the concepts that I am unsure of.

    #include “stdafx.h”
    This line should always begin any project.

    This line indicates that the commands in this project are taken from the “Iostream” Library of commands?

    int main ()
    I know that “int” means integer, but I do not fully understand what this line means. I believe the brackets indicate that something is selected as a whole…I am lost here but want to learn.

    This bracket (not sure of the name of this type of bracket) indicates the beginning of a process?

    std::cout << "Hello World!" << std::endl;
    I can only guess what "std" means here, as well as the "::", where "cout" (I am guessing) indicates an output to the screen?
    the "<<" symbols indicate the beginning and end of what will be output to the screen, with the text between the symbols is what will be output (does the text need to also be in quotes?)?
    the "std::endl;" (once again, guessing) indicates…I won't even guess…

    return 0:
    this returns a value of 0, but to where, and why?

    This bracket indicates the end of the process (I am guessing it is called the "main" process - in which case does the "int main ()" line tell the computer that the following within these brackets will be the "main" process, procedure, or function [whatever it should be referred to as])

    Thank you very much for the time you have taken to read this - I will very much look forward to any replies and will be watching closely.

    return 0;

  • Randy Wood

    I wanted to say thank you for sharing these amazing Tutorials - they are just what I was looking for.

    I have entered the following code for my “Hello World” Project, but seem to be unable to build it:

    The following is the “Output from Build”:

    [b]1>---- Build started: Project: Hello World, Configuration: Debug Win32 ----
    1> Hello World.cpp
    1>LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========[/b]

    I cannot see what I have done wrong, and would be happy to hear from someone.

    Thank you for your time.

    • Randy Wood

      Thank you Mute (author of the post above my original message).

      I read your message and this solved my problem - my “Hello World!” Project is successfully compiled!


  • mute

    if u’ve got issues with ur compiler on visual basic 2010 version i.e u get error message LNK1123 what u need to do is to upgrade ur visual studio 2010 RTM to VS 2010 SP1

  • CpnHook

    1>---- Build started: Project: HelloWorld, Configuration: Debug Win32 ----
    1> HelloWorld.cpp
    1>e:\visual studio 2010\helloworld\helloworld\helloworld.cpp(9): error C2039: ‘cou’ : is not a member of ‘std’
    1>e:\visual studio 2010\helloworld\helloworld\helloworld.cpp(9): error C2065: ‘cou’ : undeclared identifier
    1>e:\visual studio 2010\helloworld\helloworld\helloworld.cpp(9): error C2039: ‘end’ : is not a member of ‘std’
    1>e:\visual studio 2010\helloworld\helloworld\helloworld.cpp(9): error C2065: ‘end’ : undeclared identifier
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

    Good Day CPP. I’m encountering this issue with Microsoft Visual C++ 2010 Express. Could someone please help me, I’m a noob at this and am very interested in learning C++.

  • bob0711

    ok for anybody using visualstudio 2010 and getting this error:
    1>LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt
    you need to do this:
    go to project properties under tthe project menu
    then go to configuration properties/linker/general
    once there change enable incremental linking to no
    hope this helps

  • Brian64

    Like some other post’s I’ve read I’m getting this issue

    1>---- Build started: Project: helloWorld2, Configuration: Debug Win32 ----
    1>LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

    I tried some other fixes but as of yet not able to solve this problem anyone have any suggestions?

    • Brian64

      fix for issue:

      1>—— Build started: Project: helloWorld2, Configuration: Debug Win32 ——
      1>LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt
      ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

      Project Properties
      -> Configuration Properties
      -> Linker (General)
      -> Enable Incremental Linking -> “No (/INCREMENTAL:NO)”

  • JeebuzzCPP

    After a while I was able to find a solution to my issues. It seems that once installing VS2012 RC with VS2010 express will cause the projects in VS2010 express to come up with that error. This fix is to download VS2010 SP1 (service pack1) This will fix the issues causing you to get that error.

  • JeebuzzCPP

    I’m trying very hard here to learn this. I am stuck with a problem. I know a way to do this using a different project selection. I wish to follow this guide as closely as possible. The Issue I have is as follows.

    1>---- Build started: Project: HelloWorld2, Configuration: Debug Win32 ----
    1> stdafx.cpp
    1> HelloWorld2.cpp
    1>LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

    I use exactly what you type and I can’t get it working. I do not yet know enough about what I’m typing to properly start correcting the code. If someone would be kind enough to help me either get 2005 working as I am using 2010 express or help solve this error. Again I am truly trying to learn this and any serious help is going to greatly be appreciated.

  • og

    Wow!!! Code::Blocks is really nice… ty guys!!! 🙂

  • rohstar

    I am new to C++ and I have a silly question. What is the difference between the CLR console application and the Win32 console application (when i star a new project) ? I am using Visual Studio 2010 and it’s very different from Visual Studio 2005. Thanks! 🙂

  • madhavdivya

    The following is the result window. The prompt screen pops for a fraction of a second and exits.
    Can anyone suggest further on the “Cannot find or open the PDB file”

    ‘helloworld.exe’: Loaded ‘C:\vc2005projects\helloworld\Debug\helloworld.exe’, Symbols loaded.
    ‘helloworld.exe’: Loaded ‘C:\Windows\System32\ntdll.dll’, Cannot find or open the PDB file
    ‘helloworld.exe’: Loaded ‘C:\Windows\System32\kernel32.dll’, Cannot find or open the PDB file
    ‘helloworld.exe’: Loaded ‘C:\Windows\System32\KernelBase.dll’, Cannot find or open the PDB file
    ‘helloworld.exe’: Loaded ‘C:\Windows\System32\msvcr100d.dll’, Symbols loaded.
    The program ‘[5168] helloworld.exe: Native’ has exited with code 0 (0x0).

    • Elpidius

      I got the same error when clicked on the menu, Debug > Start Debugging (F5).

      However, when you press CTRL+F5, or go the Debug menu and choose “Start Without Debugging”, it works!

      Personally I couldn’t find the “Start Without Debugging” option, so I just used CTRL+F5 and it worked fine.

  • ksherif

    I finished writing my first Hello World program and it compiled successfully but when I try to run it, I get the following error message. “Unable to start program ‘d:\documents and settings\khaled.sherif\my documents\visual studio 2010\projects\C++ 2012\debug\C++ 2012.dll”. Please advise

    • Alex

      Looks like you have your compiler set to create a .dll instead of an .exe. Right click on your project, then choose Properties->General->Configuration Type and change it to Application (.exe.)

  • rainandmoonlight

    I am using Microsoft Visual Studio Express 2012 for Windows Desktop, and I typed it in exactly as the second example shows, but on the part that says std::end1; it gives me an error saying that “end1” is not a valid thing for std? Has C++ changed and it wasn’t updated here, or am I doing something wrong? Also, when I put the last bracket in at the bottom line, it tell me Intellisense was expecting an “expression”? I do not understand, please help if you know what’s wrong or have any ideas. Thank you!

  • With a little effort, Hello World welcomed me into the world of Microsoft Visual C++ 2010 Express.

    In the early 1990s, I acquired numerous programming books and Borland’s 4.52 C++ compiler designed for Windows 95. While I dabbled some, my work fell to the wayside.

    Having viewed your website and with the successful download / install of MS Visual C++ 2010 Express, (mid-January 2012), let’s see how well I do. When I tried EXE files from my 1990’s compiler on Windows 7 operating system, they didn’t work. I’m hoping to resolve that problem with Microsoft’s newer compiler. I’ll be working on your tutorial using Windows XP Pro. However, I have a laptop with Windows 7 which I can test the programs on.

    Wish me luck! (or skill, depending upon your point of view.)

  • XxSCNxX

    awesome the int main() is the same on java, too bad i only know about the variables D:

  • Helleri

    I started off Learning C (before a friend told me I should probably start with C++ and work back for more ease of learning)…So I had learned about much of what has been said in these tutorials up to this point. I am finding two things to be different and was wondering about them. I don’t see numbered lines in my compiler, is the first thing (using Microsoft Visual c++ 2010 Express). Secondly what is called the typical ‘Hello world!’ program…well I remember in starting to learn C, that when I got to that part, It showed me how to type in what I wanted a sort of correct entry to be to return a result of Hello world and make it so that any thing else would return a goodbye world…and how to make it so the return key being hit once again is what would actually close the program… I can see this one does not give me an else type of function. And it doesn’t seem to prevent the program form automatically closing until I tell it to with the return key. But, what surprised me is that I get no ‘Hello World’ even. The picture it shows is literally what it does before snapping closed.

    I also felt it was very monkey see monkey do…It didn’t really explain to me what was happening to make these things happen. If that is in a later lesson then good…but I really felt this lesson was the place for a little explanation on what is actually going on…it has been a little while since I first started learning C and so I can say I feel left lost on what I actually did and the how’s and why’s of it.

    Okay one last thing…It is a little annoying to have to scan over instructions for people using an ‘other then top recommended’ compiler. maybe at the top of these tutorials there could be a frame or something with options for viewing how things are done in different programs… that targets the rest of the page with the users selection? I mean just so people using one program don’t have to see the instructions for all the other programs they could be using before getting back to it…If that were the case there might be room to get a little more explanatory relevant to what I have going on in front of me.

    • 1) To be honest, I have no idea why they’re disabled by default.
      2) use cin.get() or something like that to prevent the window to close automatically.
      3) Sadly it’s the same with every tutorial out there. You’ll understand what’s going out later though.
      4) To be honest, code in later sections compiles on every compiler out there. I’m not sure why he said that he’ll include instructions for different IDEs, seeing as all of them follow the same standards.

    thank you!

  • a8cplusplus

    I compiled my first program successfully. Thanks to

  • Vox520

    I type in everything fine yet when it comes to end1; it repeatedly says there is an error.
    Saying this though when I paste the line of text in from the example it works.
    Anyone know of a solution to this problem?

  • bailey53189

    Wow i can actually do this!!!! Thank you so much cant wait to work my way through the rest of your site! I downloaded VS2010 and Code Blocks have to say i like code blocks better because it has the numbered lines great website really made me believe that i can actually do this!!!

  • cmag79

    Sorry…stupid question here. I don’t have the Start without Debug option. I’m using MS Visual Studio C++ 2010 Express.

    -Start Debugging
    -Build Solution
    -Step into
    -Step over
    -Toggle Breakpoint
    -Clear all data tips (inactive)
    -Export data tips (inactive)
    -Import data tips
    -Options and Settings

  • IndyBob

    do I have to create a new project for every little program that I write or is there a way to have them all in one project? Each project eats sth like 10 MB of disc space.

  • Ryker

    Actually I see that I did not add.




  • Ryker

    Hello, I believe I typed in exactly what was shown but I’m still coming up with errors. I’m currently using Microsoft Visual Studio Ultimate 2010.

    Error List

    Error 1 error C2065: ‘cin’ : undeclared identifier
    Error 2 error C2228: left of ‘.clear’ must have class/struct/union
    Error 3 error C2065: ‘cin’ : undeclared identifier
    Error 4 error C2228: left of ‘.ignore’ must have class/struct/union
    Error 5 error C2065: ‘cin’ : undeclared identifier
    Error 6 error C2228: left of ‘.get’ must have class/struct/undefined
    7 IntelliSense: identifier “cin” is undefined

    • Pakoschild

      you should type

      #include “stdafx.h”


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

      its really easy but so cool try copy this to your clipboard but first delete everything except the green letters ;)hope I helped 😀

  • Robert

    Seth Bladen, what program are you using.

  • Seth Bladen

    I entered the code as you did but got errors for iostream and cout and endl

    here is what I used instead

    to compile it i typed in gcc -o random random.c
    and to run it i typed ./random

    I’m using Fedora 12 and i’m using vim to write my code. So my guess is that I don’t have Iostream, and the printf replaces cout and the \n replaces endl and the << are replaced with ()

    is that correct? and I don't put that using namespace std; in there at all, becuase if i do it gives me errors. I'm just the latest GCC. I just downloaded it about 3 hrs ago.

    Just curious

  • Robert

    Pretty simple remembre endl is ENDL not the number one

  • Solomon Homicz

    Fal, gedit in ubuntu is definitely the way to go.

  • Doug

    In some examples you show the line numbers and in some you don’t. “Hello World” worked fine for me using Vis Basic 2010, but I haven’t seen any line numbers so far in working with it. How can I view the line numbers?

  • mona

    hahahahaha, i conquered “helloworld” after about the tenth attempt. thank you.

  • Sid

    Hey, for those using XCode I figured it out.

    1. Open XCode.
    2. File/New Project…
    3. In the “New Project” Assistant, click on “Command Line Tool”.
    4. Below select “C++ Tool” as Type
    5. Click “Next”
    6. Give a project name and directory, then click “Finish”.
    7. Press Cmd-Shift-R to open the Console window. Output will appear there.
    8. Click the “Build and Go” toolbar button.

  • bob


    I was wondering, what is the difference start without debugging and start debugging?

    When click start without debugging, my HelloWorld program runs fine. However, when i simply click debug or start debugging, the black screen comes onto the screen and quickly disappears. Why is this?


    • Alex

      Start without debugging means just run your program like normal.

      Start with debugging means run your program with the debugger active, so you can try and figure out what’s wrong with your program.

      For now, run your program without debugging. I cover debugging in more detail in section A.4 -- Debugging your program (stepping and breakpoints)

      • Duncan


        Thanks a lot for writing this guide. I’m still having trouble with the baby steps. I’ve downloaded the most recent version of the IDE you suggested, so maybe that’s the problem, but I don’t have the “start without debugging” option, and I’ve also tried CTRL+F5 with no luck. So I think I’ve written the program correctly but I can’t run it to check it!

        Any idea how I can find an alternative to the start without debugging option?


  • Nivm

    I am using Code::Blocks to compile this, and I was wondering what compiler to choose and if I should worry about “Debug Configuration” and “Release Configuration”? I’m guessing Code::Blocks has a new version because “New Project” brings me through a different process; Project Type(Console App) => C or C++ (C++) => File name and destination (Helloworld.cbp, a folder in Documents) => Compiler and Configurations (set to “GNU GCC Compiler” and both configurations are checked with assorted output folder names) => Done. Other than the fact that it doesn’t fill in the “Build” box at the bottom, I’m not having any problems, just that I wanted to know what to do about those options and what they were for.

  • masthema

    hello all…i have a small bit of a problem. if I copy code from the clipboard, it works fine, but if I type it in myself, it dosen’t work…like this :

    this is the site’s version.

    and this :

    the above version dosen’t work. it keeps telling me :

    1>---- Build started: Project: niimc, Configuration: Debug Win32 ----
    1>c:\documents and settings\peanut\my documents\visual studio 2008\projects\learning\niimc\niimc.cpp(9) : error C2059: syntax error : ‘using’
    1>c:\documents and settings\peanut\my documents\visual studio 2008\projects\learning\niimc\niimc.cpp(9) : error C2143: syntax error : missing ‘)’ before ‘;’
    1>c:\documents and settings\peanut\my documents\visual studio 2008\projects\learning\niimc\niimc.cpp(10) : error C2143: syntax error : missing ‘;’ before ‘<c:\documents and settings\peanut\my documents\visual studio 2008\projects\learning\niimc\niimc.cpp(10) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>c:\documents and settings\peanut\my documents\visual studio 2008\projects\learning\niimc\niimc.cpp(11) : error C2059: syntax error : ‘return’
    1>c:\documents and settings\peanut\my documents\visual studio 2008\projects\learning\niimc\niimc.cpp(13) : error C2059: syntax error : ‘)’
    1>Build log was saved at “file://c:\Documents and Settings\Peanut\My Documents\Visual Studio 2008\Projects\learning\niimc\Debug\BuildLog.htm”
    1>niimc - 6 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

    i used a different name for the project, but i don’t think that’s the problem…so…anyone? please mail me to if you can see what’s wrong.

    • Shannon

      First, you have the word “count” instead of “cout”.
      Second, after “int main()”, you must start the line out with a curly brace { and end it with a curly brace }.
      You’re using an open parentheses ( and a close parentheses ).

  • Piki

    Thanks for making this tutorial so detailed and easy to follow!

  • Nathe

    This has got to be the best C++ programming website that I have been to.
    Finally, someone knows how to teach the new generation of programmers. 🙂

  • Len M

    Going great so far but….

    Not sure i shold worry about this but my outpur is showing an extra line.

    Using windows and Code::Blocks but can see nothing different in the code and it compiles correctly?

  • John

    It’s worth mentioning that Linux distros do not necessarily have xterm installed. CodeBlocks can be modified to use the gnometerminal through Settings > Environment and change the Terminal to launch program to “gnome-terminal -x”.

    You don’t have to install xterm.


  • 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 C++ code inside [code][/code] tags to use the syntax highlighter