Category Archives: Code::Blocks

A beginner’s Visual C++ tutorial

A friend of mine who just started learning C++ ran into a build error on his first program, which looks likes this:

#include <iostream>

using namespace std;

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

This is the type of program you’d expect to see in just about any beginner’s C++ tutorial. What they usually don’t tell you is that this program won’t work in Microsoft Visual C++ without some additional configuration.

Visual C++ displays the full error message in the Output pane (bottom)

Visual C++ displays the full error message in the Output pane (bottom)

You can see the full error message in the Output pane, which is normally docked at the bottom of the window. Here is the rest of the message:

1>------ Build started: Project: ConsoleApplication1, Configuration: Debug Win32 ------
1>  ConsoleApplication1.cpp
1>e:\code\visual c++ projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(1): warning C4627: '#include <iostream>': skipped when looking for precompiled header use
1>          Add directive to 'StdAfx.h' or rebuild precompiled header
1>e:\code\visual c++ projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(10): fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "StdAfx.h"' to your source?
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Right away, there’s a problem with line 1. We know this because the line number is shown in parentheses after the filename in the Output pane. This is followed by the warning code, which is C4627, and the rest of the warning message. Visual C++ is pretty clear about what the problem is, but if you’re completely new to programming or to C++ then these words probably don’t mean much to you.

Beginners often get flak in programming circles for not reading or understanding error messages – in this case, a search for warning c4627 would get you pretty far – but I think this is a bit unfair. We don’t normally expect anyone to read, much less understand, something in another language. There’s a lot of vocabulary here for a beginner to learn. So let’s break this error message down, starting with:

'#include <iostream>' skipped when looking for precompiled header use

Basically, the compiler didn’t bother to include the file we asked it to (it skipped it). What’s a “precompiled header?” It’s just another C++ file that has some code that we need in it, plus it’s already been compiled. We don’t need to compile it again because it is not going to change. (i.e., iostream will probably never be updated.)

Add directive to 'StdAfx.h' or rebuild precompiled header

A “directive” is an instruction we can give to the compiler. In our program, #include <iostream> instructs the compiler to look in the iostream header file for things like cout. We could add another directive like this to stdafx.h, another header file which is included in our project under “Header Files” in the Solution Explorer pane.

(By the way, are we having fun yet? Get used to debugging errors if you want to become a programmer. i.e. don’t get discouraged. At least 50% of my time at work is spent debugging errors.)

Visual C++ reports not one but two problems with our code. The second error message comes from line 10, error code C1010:

unexpected end of file while looking for precompiled header. Did you forget to add '#include "StdAfx.h"' to your source?

Here, the compiler is telling us it kept looking for our precompiled header ’til it hit the end of the file. It never found it, but it thinks it could be in stdafx.h. Unless you’ve been reading ahead, it won’t find it there either. It doesn’t know either way because we never told it to look there. i.e., we didn’t #include "stdafx.h" at the beginning of our file.

Want to know something freaky? Our program compiles without a hitch in Code::Blocks.

This is because new projects in Visual C++ are configured to use precompiled headers, but not in Code::Blocks. That tutorial we got our code from probably assumed we weren’t using Microsoft tools but standard tools like GNU Make. In fact, Code::Blocks normally comes with a version of GNU specifically for Windows called MinGW (Minimalist GNU for Windows). That solves that mystery.

But how do we fix our program? I’ll tell you how to change your project’s configuration to get up and running quickly, but I’ll also tell you how to take advantage of precompiled headers if you want to do things the Visual C++ way.

First, if you want Visual C++ to behave like Code::Blocks, right click on your project in the Solution Explorer pane and go to Properties:

The project's context menu as seen in the Solution Explorer pane.

The project’s context menu as seen in the Solution Explorer pane.

On the left side of the properties window, drill down to Configuration Properties > C/C++ > Precompiled Headers. Change the first option, simply named Precompiled Header, from “Use (/Yu)” to “Not Using Precompiled Headers,” then click OK.

We don't need no stinking precompiled headers... except for when we do.

We don’t need no stinking precompiled headers… except for when we do.

Now you can run your program!

If you want to use precompiled headers instead, simply change the first line of your program:

#include "stdafx.h"

using namespace std;

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

Then add the missing directive to stdafx.h:

#pragma once

#include "targetver.h"

#include <stdio.h>
#include <tchar.h>

#include <iostream>

Now, run your program and celebrate. You are a programmer.

Did you see the console window appear for a brief second and then disappear? Typically, console programs do their thing and then close immediately. You can tell your program to pause and wait by adding a call to system("pause") before the line return 0:

#include "stdafx.h"

using namespace std;

int main()
{	
    cout << "Hello world!" << endl;
    system("pause");
    return 0;
}

That way you’ll have time to admire your beautiful program before closing it.

For extra credit, modify your program to print something awesome to the console.

For extra credit, modify your program to print something awesome to the console.

Advertisements