I am not longer recommending Dev-C++ as a student C++ IDE. This page is here for historical reasons only.
To work with C++ you will need an editor to write your programs and save them, and a compiler to convert what you write to code that can be run on your computer. Any reasonable editor and compiler will suffice. (Note that some of the examples here require C++ 2011 or later.) Your computer may already have one installed.
The work for this module can, in principle, be done with almost any C++ compiler, but the computer labs at the university are equipped with Microsoft Windows and Dev-C++, so acquaintance with this is probably important.
Dev-C++ is single package that combines an editor and C++ compiler. (It is an "integrated development environment" or IDE.) It tries to make your work easier by highlighting C++ syntax and providing help for you as you type. How much you use its features is entirely up to you. I will expect you to understand the basics, including some understanding of what is happening when you press various buttons. The more advanced features are all easier to understand and use when you know the basics.
You can install dev-C++ from
http://orwelldevcpp.blogspot.co.uk/.
Follow the link (currently reading
"The setup which includes TDM-GCC 4.9.2
Beware that older versions of Dev-C++ are also available on the web. The one above (at least this version, or later) is the one we shall be using and I don't really recommend older ones.
Note.
For students wanting to install software on a linux or Mac computer you can try codelite http://codelite.org/ or codeblocks http://www.codeblocks.org/ . Both are supposed to work on more or less any computer and one or both of these are likely to be packaged with your linux distro making installation easier. Eclipse is another possibility (https://eclipse.org/cdt/). If you already use Eclipse and like it that's fine, but I find it a bit "heavy" for small programs like the ones here. You will need to ensure that you have a C++ compiler that is capable of C++2011. Any recent versions of GCC should do this and GCC is the usual compiler that is packaged with these IDEs (except for Eclipse). For Mac users, I understand Xcode http://developer.apple.com/tools/xcode/ is available, free, and works well, though I have no experience of it myself. The requirements for this module are minimal, and in fact an IDE is not needed if you are happy using a command line tool. It would suffice just to have a good editor and command line access to a compiler, all linux distributions contain this, and this is in fact how I normally work myself.
The default for this module is the IDE called Dev-C++ under MS-windows. If you are using some other system you should create and compile "helloworld" in that system. (No further help will be provided here.)
To make the "hello world" program using Dev-C++, first read the web page that describes it. Then the procedure is as follows.
For beginners, please note that Dev-C++ adds a layer of complexity at the very first stage, but it will repay you by making a number of other things easier later on. But you MUST start up your project correctly. Start Dev-C++ first, then
Save somewhere that you can easily remember, somewhere where you can save safely, somewhere that is separate from your other work and that you can return to easily. Use either your "home" networked folder (not a local folder on the computer you happen to be sitting at) or on a memory stick. I went to "Documents" then made a subdirectory "2np" for all 2np work, and a subdirectory of that called "helloworld". Finally I saved the newproject in there as "helloworld". If in doubt make a note of this location on paper.
The procedure above is important.
Note that you save project settings in a project file (that ends ".dev" though MS-windows unhelpfully hides this part of the file name). It is best to have a new folder for each project. Don't forget "Console Application". (I won't explain how to write any other sort of application here.)
NOTE: you should NEVER include spaces in your filenames. This applies to all filenames, not just for this module. Putting spaces in filenames will always eventually result in difficult-to-fix errors.
(The problem with spaces in filenames is that sooner or later a file
called my file.cpp
will have its quote marks removed and some program
somewhere will interpret the name as two files, called my
and file.cpp
.
Neither will be found and there will be an incomprehensible error message.
I have seen students wasting time over "strange DevC++ behaviour"
which is due to precisely this. Similar problems occur in all OSs, e.g.
when a file is opened in a particular application. All such problems can be
avoided by omitting spaces in filenames.)
When you want to restart your project, start Dev-C++ and then open the project file using file -> open. Don't double-click a random file and hope the settings will be right. They might not be.
Dev-C++ writes a starting template program for you. Some of the details may be different to the one I recommend. If you see it, I won't be explaining "int argc, char** argv" here, and you don't need it in this module. Delete everything and start again as on the helloworld web page. In particular, add the lines "using namespace std;" and "cout << "Hello, world!" << endl;" in the appropriate places.
You can compile your program with
The first time you compile you will be asked to give the name of the "main" c++ file. This is as well as the "project" file. I saved as "main.cpp" in the "helloworld" subdirectory.
You can run the program with
Check it works as you expected. If it doesn't, read to the end of this web page and don't ask for help unless you have tried everything that is suggested here. There is another web page obtained by following the link devoted to some quirks of Dev C++ and the university computer environment which you might like to check.
If the program works, change it and recompile and re-run it a few times. There may be a few other things to try, explained on the first worksheet for the module.
Remark.
There is a possible problem for some users in computer labs. What happens is that when a program is run a console flashes up so quickly and then closes so you might not even see it. This is the program running and presumably you'd like to see what it has done. If this happens check the setting in Dev C++ ( Tools > Environment options > Pause console programs after return ).
Unfortunately the computer labs are set up so that some settings are preserved on each particular computer from one user to the next. Even if you don't get this problem the first time, you might experience this problem again in another computer lab, so please remember it.
Remark.
Another possible problem for some users in computer labs is that if you do not close the console after it has run, the running console itself might prevent you making changes and recompiling your program. This is a Microsoft Windows "feature" that originally arose as a bug-fix to a problem in MS-DOS 3.1 and which we have been suffering from every since. Please make sure you close all consoles by pressing a key at the end, or if you need to, by pressing control-C.
Now inspect the directory where your work is. You need to start up a "command prompt" window. On my computer this is accessed with
but it may be different under other versions of MS-Windows. From here you type commands to change to the directory or folder where your work is. All commands should be concluded with the ENTER key.
I did,
C:\Users\Richard Kaye\Documents\2np\helloworld> dir Volume in drive C is Acer Volume Serial Number is 9C84-78FB Directory of C:\Users\Richard Kaye\Documents\2np\helloworld 2015-12-27 02:50 PM <DIR> . 2015-12-27 02:50 PM <DIR> .. 2015-12-27 02:40 PM 777 helloworld.dev 2015-12-27 02:50 PM 1,853,389 helloworld.exe 2015-12-27 02:50 PM 112 main.cpp 2015-12-27 02:50 PM 1,783 main.o 2015-12-27 02:50 PM 1,174 Makefile.win 5 File(s) 1,857,235 bytes 2 Dir(s) 160,350,916,608 bytes free
You will see our "project" now has 5 files. These are
In the case of a text file, it can be printed at the console. For example the command
prints the main program at the console. You should recognise this file. Similarly,
print the various settings file that Dev-C++ uses. But you really don't need to understand all these!
The other two files (main.o and helloworld.exe) are special "binary" files in a format that is easy for the computer to understand, but difficult or impossible for humans.
Once it is compiled, the final program can be run directly from the console with the command
(This is a special command that runs the file called helloworld.exe in the current directory. The format of this file is designed to make it easy for the computer to run it directly. It is called an executable file.)
This much information about using Dev-C++ and C++ should be (almost) enough for this module, but Dev-C++ has a lot of buttons for making your work a bit easier, and for more advanced work. I am not going to explain them all. If you change options you are assumed to know what you are doing. If your program stops working as a result you will need to be able to fix it!
Dev-C++ has a number of tools to help display your source file in a nice way (using different colours, fonts) and tools to help you march brackets and indent your code. I strongly suggest you get to know how these work. (If you hate them, you can turn them off.) Writing easy-to-read and correctly indented code will save masses of time.
One feature you will occasionally need, however, is to switch Dev-C++ from the usual default compiler to the 2011 C++ compiler standard. This is done with
and then
Personally, I also find it best to turn "on" the feature that generates a lot of warnings (as well as error messages) generated by the compiler and strongly recommend you always do this. In principle you do it in Dev-C++ by tools > compiler options > Settings > Warnings > Show most warnings. However, there are two problems. First, this doesn't always seem to work. (I don't know why not. A bug?) Second, for beginners it is useful to have even more warnings than this.
Therefore I strongly recommend that you do the following for all your projects this term.
In project > project options, under "parameters", add in the "C++ compiler" column
the following options -Wall -Wconversion -Wempty-body
.
(These can go on one line or in three separate lines.) This will trap most of the
common student errors and will help you considerably later on.
This will save these options for this particular project.
If you want them for other projects you will have to do this again for each.
You can also save these options as a default for all projects on this particular computer
with tools > compiler options, under "General", tick: "Add the following
commands when calling the compiler"; and underneath this add
-Wall -Wconversion -Wempty-body
. The problem with this is that if this
is a public university computer, you do not know when you will return to it or
who might change the settings in the meantime...