Using Dev-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
Installing and using Dev-C++
This little article will teach you how to install the latest version of Dev-C++ and how to write a C program in it. This way, you will know how to write C programs, compile, and find the executable.
Dev-C++ is not actually a compiler, but an IDE. The link below is an installation of Dev-C++ which is prepackaged with MinGW. MinGW is the GCC compiler but made for usability in Windows.
Contents
- 1 Downloading
- 2 Installing
- 3 Using Dev-C++
- 3.1 Creating a Project
- 3.2 Compiling and Executing
- 3.3 The Interface
Downloading [ edit | edit source ]
Installing [ edit | edit source ]
Hit all the next buttons.
Using Dev-C++ [ edit | edit source ]
By default Dev-C++ is installed to C:\Dev-Cpp.
Go there and open up the .exe called devcpp.
It should look something like this.
Creating a Project [ edit | edit source ]
To write a program, you have to make the source code files. To do this you create a project.
To create a project you go to File > New > Project.
A dialog will come up asking you what kind of project you would like to make, what you would like to name it, and if you want to compile in C++ or C. There are many project templates for different types of programs. Select the Console Application template and the language you want to compile in is C. Then name the project something. It would be recommended that when you save this project, that you should save it to it’s own folder. You should probably make a folder under C:\Dev-Cpp called Projects and then make a folder this project. The reason being wherever you save this project it’ll create multiple files and could get cluttered very fast if you do not make a folder.
Compiling and Executing [ edit | edit source ]
Compiling and executing a program is very simple. You can hit F9 to compile and execute or click the button.
If all goes well then a console should come up and display “Press any key to continue. “
The Interface [ edit | edit source ]
There are many features, and the Dev-C++ is pretty easy to understand. There are two important windows that you should know to get you started.
The first is where you actually edit the source code for your programs. It has syntax highlighting and formatting making it easier to read.
The second would be the navigating of your projects and your source code files. This is so you can see how your projects are organized.
/* PROJECT : BIRTHDAY CALCULATOR PROGRAMMER : MD. SALAH UDDIN EMAIL : salahuddinlovelu@gmail.com */ #include #include /* checks whether the given input is leap year feb or not */ int lYear(int year, int mon) int flag = 0; if (year % 100 == 0) if (year % 400 == 0) if (mon == 2) flag = 1; > > > else if (year % 4 == 0) if (mon == 2) flag = 1; > > return (flag); > int main() /* days in months */ int dInM[] = 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31>; int days, month, year; char dob[100]; time_t ts; struct tm *ct; /* get the date of birth from the user */ printf("Enter your days of birth [day/month/year]: "); fgets(dob, 100, stdin); sscanf(dob, "%d/%d/%d", &days, &month, &year); ts = time(NULL); ct = localtime(&ts); /* scan the year, month and year from the input string */ printf("\nCurrent Date: %d/%d/%d\n", ct->tm_mday, ct->tm_mon + 1, ct->tm_year + 1900); days = dInM[month - 1] - days + 1; /* checking whether month is leap year feb - 29 days*/ if (lYear(year, month)) days = days + 1; > /* calculating the no of days, years and months */ days = days + ct->tm_mday; month = (12 - month) + (ct->tm_mon); year = (ct->tm_year + 1900) - year - 1; /* checking for leap year feb - 29 days */ if (lYear((ct->tm_year + 1900), (ct->tm_mon + 1))) if (days >= (dInM[ct->tm_mon] + 1)) days = days - (dInM[ct->tm_mon] + 1); month = month + 1; > > else if (days >= dInM[ct->tm_mon]) days = days - (dInM[ct->tm_mon]); month = month + 1; > if (month >= 12) year = year + 1; month = month - 12; > /* print the result */ printf("\nYour age: %d year %d months and %d days\n\n",year, month, days); printf("THANKS FOR USE THIS SOFT\n\n MD.SALAH UDDIN LOVELU\n\n LECTURER KURERPAR ADARSHA COLLEGE\n\n MURADNAGOR, COMILLA."); return 0; >
See also [ edit | edit source ]
- C++ Source Codes
- Topic:C++
- Introduction to C++
- Topic:Computer programming
- Switching from C to C++
- C++
- C programming language
Using Dev-C++
I am not longer recommending Dev-C++ as a student C++ IDE. This page is here for historical reasons only.
1. Installing Dev-C++
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 (32bit and 64bit) can be downloaded here (47MB).”) to get to the download page. The set-up file can then be executed. For normal fairly up-to-date computers you can just accept the defaults, including syntax checking etc. The “portable” version of Dev-C++ is also useful: the whole installation goes on a memory stick and you don’t need admin privileges to set it up.
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.
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.
2. Creating and compiling “helloworld”
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
- do: file > new > project
- select “Console Application”
- type the name, e.g. “helloworld”
- select C++ as language
- click OK
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 " in the appropriate places.
You can compile your program with
- Execute > Compile
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
- Execute > Run
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.
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.
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.
3. Inspecting your work from the command line
Now inspect the directory where your work is. You need to start up a "command prompt" window. On my computer this is accessed with
- start > all programs > accessories > command prompt
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.
- use "L:" to change to the disk drive with this letter (in this case L)
- use "cd directoryname" to change to the subdirectory of the current one with the name given
- use "cd .." to change to the parent directory of the current one
- use "dir" to list what is in the current directory
- use "tree" to see a tree of all directories below the current one
- C:
- cd Documents
- cd 2np
- cd helloworld
- then "dir" to see a directory listing, giving in my case
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 . 2015-12-27 02:50 PM .. 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
- helloworld.dev : the "master" project file with all settings and a list of all required files
- helloworld.exe : the final executable (or "binary") file that can be run
- main.cpp : the file containing the C++ program that you typed
- main.o : an intermediate "object" file that was produced from main.cpp when it was compiled
- Makefile.win : a special file that automates the compile process for this project, automatically created by Dev-C++
In the case of a text file, it can be printed at the console. For example the command
- type main.cpp
prints the main program at the console. You should recognise this file. Similarly,
- type helloworld.dev
- type Makefile.win
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
- helloworld
(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.)
4. Using C++ 2011, and other compiler options
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!
tools > editor options Dev C++ has a number of options to change the way the editor works. If you don't like the default you can change things here. tools > compiler options Dev C++ has a number of options to set the compiler here. A couple of them are discussed below. You can ignore the others. tools > environment options Dev C++ has a number of other options. This includes "Pause console programs after return" which you will need. You can safely ignore the others.
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
- tools > compiler options
- Settings > Code Generation > Language standard > ISO C++11
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.
Dev-C++
is a free IDE for Windows that uses either MinGW or TDM-GCC as underlying compiler.
Originally released by Bloodshed Software, but abandoned in 2006, it has recently been forked by Orwell, including a choice of more recent compilers. It can be downloaded from:
Installation
Run the downloaded executable file, and follow its instructions. The default options are fine.
Support for C++11
By default, support for the most recent version of C++ is not enabled. It shall be explicitly enabled by going to:
Tools -> Compiler Options
Here, select the "Settings" tab, and within it, the "Code Generation" tab. There, in "Language standard (-std)" select "ISO C++ 11":
Ok that. You are now ready to compile C++11!
Compiling console applications
To compile and run simple console applications such as those used as examples in these tutorials it is enough with opening the file with Dev-C++ and hit F11 .
As an example, try:
File -> New -> Source File (or Ctrl+N )
There, write the following:
1
2
3
4
5
6int main() < auto x = R"(Hello world!)"; std::cout
File -> Save As. (or Ctrl+Alt+S )
And save it with some file name with a .cpp extension, such as example.cpp .
Now, hitting F11 should compile and run the program.
If you get an error on the type of x , the compiler does not understand the new meaning given to auto since C++11. Please, make sure you downloaded the latest version as linked above, and that you enabled the compiler options to compile C++11 as described above.
Comments are closed, but trackbacks and pingbacks are open.