Before we can write our first program (which we will do very soon), we need to know two things about development environments.
First, although our programs will be written inside .cpp files, the .cpp files themselves will be added to a project. Some IDEs call projects “workspaces”, or “solutions”. 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.
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.
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:
#include <iostream>
int main()
{
using namespace std;
cout << "Hello world!" << endl;
return 0;
}
If you select the code from these examples with your mouse and then copy/past it into your compiler, you will also get the line numbers, which you will have to strip out manually. Instead, click the “copy to clipboard” link at the top of the example. This will copy the code to your clipboard without the line numbers, which you can then paste into your compiler without any editing required.
Visual Studio 2005 Express
To create a new project in Visual Studio 2005 Express, go to the File menu, and select New -> Project. A dialog box will pop up that looks like this:

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.

In the text editor, you will see that VC2005 has already created some code for you. Select and delete the _tchar function, and then type/copy the following into your compiler:
#include "stdafx.h"
#include <iostream>
int main()
{
std::cout << "Hello world!" << std::endl;
return 0;
}
What you end up with should look like this:

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:

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:

That is the result of your program!
Important note to Visual Studio users: Visual studio programs should ALWAYS begin with the following line:
#include "stdafx.h"
Otherwise you will receive a compiler warning, such as c:\test\test.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.
Code::Blocks
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:

Select Console Application and press the Create button.
You will be asked to save your project. You can save it wherever you wish, though we recommend you save it in a subdirectory off of the C drive, such as C:\CBProjects. Name the project HelloWorld.

You will see “Console Application” under the default workspace:

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:

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:

That is the result of your program!
Using a command-line based compiler
Paste the following into a text file named HelloWorld.cpp:
#include <iostream>
int main()
{
using namespace std;
cout << "Hello world!" << endl;
return 0;
}
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.
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:
#include <iostream>
int main()
{
using namespace std;
cout << "Hello world!" << endl;
return 0;
}
4) Compile the project
5) Run the project
If compiling fails
If compiling the above program fails, check to ensure that you’ve typed or pasted the code in correctly. The compiler’s error message may give you a clue as to where or what the problem is.
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:
#include <iostream.h>
int main()
{
cout << "Hello world!" << endl;
return 0;
}
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 lesson 0.7 — a few common cpp problems.
Conclusion
Congratulations, you made it through the hardest part of this tutorial (installing the IDE and compiling your first program)! You are now ready to learn C++!
0.7 — A few common C++ problems
|
Index
|
0.5 — Installing an Integrated Development Environment (IDE)
|
0.7 — A few common C++ problems
Index
0.5 — Installing an Integrated Development Environment (IDE)
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 ]
Very useful tutorial- especially for beginers for me! Thanks a lot!
Extremely useful and easy to understand ans follow. Thank you guys for a great work.
How could I get this for Dev-Cpp? I did it, but the executable file flashes for .1 of a second and closes.
Add the following line just before the return statement in main():
Thus,
#include <iostream> int main() { using namespace std; cout < < "Hello world!" << endl; cin.get(); return 0; }Thanks!
Thanks Brian for that comment.
Since I had no experience at all in anything, I got confused.
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.
Some people do:
#include <iostream> int main() { std::cout << "Hello World!n"; return 0; }Is there a difference and a reason? Do some compilers only accept that?
I presume you are talking about the difference between:
and
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:
Thanks. Great site you got by the way.
I’ll stick with the using statement because I’ve seen people use a lot of I/O and it looked very ugly as you said.
Maybe I’m not doing it right.
Your #include iostream looks fine to me.
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.
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 (-).
thanks bro, this helped me out alot! thnaks for helping me write my first C++ program
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?
thanks!
That’s addressed in the very next section: a few common cpp problems. :)
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?
–edit–
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.
Hi, i am sorry i know this is very basic but i cannot find how to open Code::Blocks please somebody help me. By the way i have both Visual C++ 2005 Express Edition (which i am using for tutorial) and i have Microsoft Visual Basic 2008 Express Edition (decided to use 2005 for a better reference from tutorial) Please somebody help me.
I don’t know if anyone else has this problem but when I compile it fails. It’s “error prj0003 : error spawning ‘rc.exe’.” I’ll copy your code into my file but I just want to know why this happens.
It’s odd but I figured the problem was with my comp and the IDE clashing at some point. After a couple of reinstallations I gave up and installed the 2005 version. Everything worked.
CHEERS
Hi Alex, I am trying to compile a “.c” file using this compiler but I fail.
I can, following through this tutorial compile a “.cpp” file.
Could you help me please?
Thanks in advance.
nevermind Alex, I’ve figured it out just now.
thanks, anyway
Very nice. This got me up and running quite nicely.