dev-c++ tutorial for csc 161...

7
11/25/11 DeY-C++ TXWRUiaO fRU CSC 161 SWXdeQWV 1/7 ZZZ.XQiTXeQeVV-WePSOaWe.cRP/deYcSS/#VWeS4 Dev-C++ Tutorial for CSC 161 Students (Maintained b\ Michael Serrano) Update (9/12/2006): IW'V beeQ TXiWe VRPe WiPe ViQce I'Ye XSdaWed WhiV Sage; iQ facW, I ORVW WUacN Rf iW fRU aZhiOe. IQ Whe hRSeV WhaW iW Pa\ VWiOO be XVefXO, I'Ye XSdaWed WhiV WXWRUiaO WR be cXUUeQW ZiWh Whe OaWeVW YeUViRQ Rf DeY-C++ (aV Rf WhiV ZUiWiQg, VeUViRQ 5 BeWa 9--a.N.a. VeUViRQ 4.9.9.2). ½½ Mike ¾¾ What is Dev-C++? DeY-C++, deYeORSed b\ BORRdVhed SRfWZaUe , iV a fXOO\ feaWXUed gUaShicaO IDE (IQWegUaWed DeYeORSPeQW EQYiURQPeQW), Zhich iV abOe WR cUeaWe WiQdRZV RU cRQVROe- baVed C/C++ SURgUaPV XViQg Whe MiQGW cRPSiOeU V\VWeP. MiQGW (MiQiPaOiVW GNU* fRU WiQdRZV) XVeV GCC (Whe GNU g++ cRPSiOeU cROOecWiRQ), Zhich iV eVVeQWiaOO\ Whe VaPe cRPSiOeU V\VWeP WhaW iV iQ C\gZiQ (Whe XQi[ eQYiURQPeQW SURgUaP fRU WiQdRZV) aQd PRVW YeUViRQV Rf LiQX[. TheUe aUe, hRZeYeU, diffeUeQceV beWZeeQ C\gZiQ aQd MiQGW; OiQN WR DiffeUeQceV beWZeeQ C\gZiQ aQd MiQGW fRU PRUe iQfRUPaWiRQ. *GNU iV a UecXUViYe acUon\m foU "GNU'V NoW Uni["; iW iV pUonoXnced "gXh-NEW". The GNU PUojecW ZaV laXnched in 1984 Wo deYelop a fUee and compleWe Uni[-like opeUaWing V\VWem. Click picWXre Wo enlarge. Bloodshed!? I'OO be Whe fiUVW WR Va\ WhaW Whe QaPe BORRdVhed ZRQ'W giYe \RX ZaUP aQd fX]]ieV, bXW I WhiQN iW'V beVW if Whe cUeaWRU Rf BORRdVhed e[SOaiQV: First I would like to sa\ that I am not a satanist, that I hate violence/war and that I don't like heav\ metal / hard-rock music. I am french, but I do know the meaning of the "Bloodshed" word, and I use this name because I think it sounds well. If \ou are offended b\ the name, I am ver\ sorr\ but it would be a big mess to change the name now. There's also a reason wh\ I k eep the Bloodshed name. I don't want people to think Bloodshed is a compan\, because it isn't. I'm just doing this to help people. Here is a good remark on the Bloodshed name I received from JohnS: I assumed that this was a reference to the time and effort it requires of you to make these nice software programs, a la "Blood, Sweat and Tears". Peace and freedom, Colin Laplace

Upload: tranhuong

Post on 11-Feb-2018

234 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Dev-C++ Tutorial for CSC 161 Studentsvulms.vu.edu.pk/Courses/CS602/Downloads/DevCppTutorialGraphics_… · .iee-eae.c/dec/#e4 1/7 Dev-C++ Tutorial for CSC 161 Students (Maintained

11/25/11 Dev-C++ Tutorial for CSC 161 Students

1/7www.uniqueness-template.com/devcpp/#step4

Dev-C++ Tutorial for CSC 161 Students (Maintained by

Michael Serrano)

Update (9/12/2006):It's been quite some time since I've updated this page; in fact, I lost track of it for awhile. In the hopes that it may

still be useful, I've updated this tutorial to be current with the latest version of Dev-C++ (as of this writing,

Version 5 Beta 9--a.k.a. Version 4.9.9.2). ‹‹ Mike ››

What is Dev-C++?Dev-C++, developed by Bloodshed Software, is a fully

featured graphical IDE (Integrated Development

Environment), which is able to create Windows or console-

based C/C++ programs using the MinGW compiler system.MinGW (Minimalist GNU* for Windows) uses GCC (the

GNU g++ compiler collection), which is essentially the same

compiler system that is in Cygwin (the unix environment

program for Windows) and most versions of Linux. Thereare, however, differences between Cygwin and MinGW;

link to Differences between Cygwin and MinGW for more

information.

*GNU is a recursive acronym for "GNU's Not Unix"; it ispronounced "guh-NEW". The GNU Project was launched in 1984to develop a free and complete Unix-like operating system.

Click picture to enlarge.

Bloodshed!?I'll be the first to say that the name Bloodshed won't give you warm and fuzzies, but I think it's best if the creator

of Bloodshed explains:

First I would like to say that I am not a satanist, that I hate violence/war and that I don't like heavymetal / hard-rock music. I am french, but I do know the meaning of the "Bloodshed" word, and I usethis name because I think it sounds well. If you are offended by the name, I am very sorry but it wouldbe a big mess to change the name now.

There's also a reason why I keep the Bloodshed name. I don't want people to think Bloodshed is acompany, because it isn't. I'm just doing this to help people.

Here is a good remark on the Bloodshed name I received from JohnS:I assumed that this was a reference to the time and effort it requires of you to make these nice softwareprograms, a la "Blood, Sweat and Tears".

Peace and freedom,

Colin Laplace

Page 2: Dev-C++ Tutorial for CSC 161 Studentsvulms.vu.edu.pk/Courses/CS602/Downloads/DevCppTutorialGraphics_… · .iee-eae.c/dec/#e4 1/7 Dev-C++ Tutorial for CSC 161 Students (Maintained

11/25/11 Dev-C++ Tutorial for CSC 161 Students

2/7www.uniqueness-template.com/devcpp/#step4

Getting Dev-C++The author has released Dev-C++ as free software (under GPL) but also offers a CD for purchase which cancontain all Bloodshed software (it's customizable), including Dev-C++ with all updates/patches.

Link to Bloodshed Dev-C++ for a list of Dev-C++ download sites.

You should let the installer put Dev-C++ in the default directory of C:\Dev-Cpp, as it will make it easier to laterinstall add-ons or upgrades.

Using Dev-C++This section is probably why you are here.

All programming done for CSC161 will require separate compilation projects (i.e. class header file(s), class

implementation file(s) and a main/application/client/driver file). This process is relatively easy as long as you know

what Dev-C++ requires to do this.

Step 1: Configure Dev-C++.

We need to modify one of the default settings to allow you to use the debugger with your programs.

Go to the "T o o l s " menu and select "C o m p i l e r O p t i o n s ".

In the "S e t t i n g s " tab, click on "Linker" in the left panel, and change "G e n e r a t e d e b u g g i n g

i n f o r m a t i o n " to "Y e s ":

Click "O K ".

Step 2: Create a new project. A "project" can be considered as a container that is used to store all the elements that are required to compile aprogram.

Go to the "F i l e " menu and select "N e w ", "P r o j e c t . . . ".Choose "E m p t y P r o j e c t " and make sure "C + + p r o j e c t " is selected.Here you will also give your project a name. You can give your project any valid filename, but keep in

mind that the name of your project will also be the name of your final executable.Once you have entered a name for your project, click "O K ".

Dev-C++ will now ask you where to save your project.

Page 3: Dev-C++ Tutorial for CSC 161 Studentsvulms.vu.edu.pk/Courses/CS602/Downloads/DevCppTutorialGraphics_… · .iee-eae.c/dec/#e4 1/7 Dev-C++ Tutorial for CSC 161 Students (Maintained

11/25/11 Dev-C++ Tutorial for CSC 161 Students

3/7www.uniqueness-template.com/devcpp/#step4

Step 3: Create/add source file(s).

You can add empty source files one of two ways:

Go to the "F i l e " menu and select "N e w S o u r c e F i l e " (or just press CTRL+N) ORGo to the "P r o j e c t " menu and select "N e w F i l e ".

Note that Dev-C++ will not ask for a filename for any new source file until you attempt to:1. Compile

2. Save the project3. Save the source file

4. Exit Dev-C++

You can add pre-existing source files one of two ways:

Go to the "P r o j e c t " menu and select "A d d t o P r o j e c t " OR

Right-click on the project name in the left-hand panel and select "A d d t o P r o j e c t ".

EXAMPLE: Multiple source files

In this example, more than 3 fi les are required to compile the program; The "driver.cpp" fi le

references "Deque.h" (which requires "Deque.cpp") and "Deque.cpp" references "Queue.h" (which

requires "Queue.cpp").

Step 4: Compile.Once you have entered all of your source code, you are ready to compile.

Go to the "E x e c u t e " menu and select "C o m p i l e " (or just press CTRL+F9).

It is likely that you will get some kind of compiler or linker error the first time you attempt to compile a

project. Syntax errors will be displayed in the "C o m p i l e r " tab at the bottom of the screen. You candouble-click on any error to take you to the place in the source code where it occurred. The "L i n k e r "

tab will flash if there are any linker errors. Linker errors are generally the result of syntax errors not

allowing one of the files to compile.

Once your project successfully compiles, the "C o m p i l e P r o g r e s s " dialog box will have a status of "D o n e ". At

this point, you may click "C l o s e ".

Step 5: Execute.

Page 4: Dev-C++ Tutorial for CSC 161 Studentsvulms.vu.edu.pk/Courses/CS602/Downloads/DevCppTutorialGraphics_… · .iee-eae.c/dec/#e4 1/7 Dev-C++ Tutorial for CSC 161 Students (Maintained

11/25/11 Dev-C++ Tutorial for CSC 161 Students

4/7www.uniqueness-template.com/devcpp/#step4

You can now run your program.

Go to the "E x e c u t e " menu, choose "R u n ".

Note: to pass command-line parameters to your program, go to the "E x e c u t e " menu, choose "P a r a m e t e r s "

and type in any paramaters you wish to pass.

Disappearing windows

If you execute your program (with or without parameters), you may notice something peculiar; a console window

will pop up, flash some text and disappear. The problem is that, if directly executed, console program windows

close after the program exits. You can solve this problem one of two ways:

Method 1 - Scaffolding:

Add the following code before any r e t u r n statement in m a i n ( ) or any e x i t ( ) or a b o r t ( ) statement

(in any function):

/ * S c a f f o l d i n g c o d e f o r t e s t i n g p u r p o s e s * / c i n . i g n o r e ( 2 5 6 , ' \ n ' ) ;c o u t < < " P r e s s E N T E R t o c o n t i n u e . . . " < < e n d l ;c i n . g e t ( ) ;

/ * E n d S c a f f o l d i n g * /

This will give you a chance to view any output before the program terminates and the window closes.

Method 2 - Command-prompt:

Alternatively, instead of using Dev-C++ to invoke your program, you can just open an MS-DOS Prompt,

go to the directory where your program was compiled (i.e. where you saved the project) and enter theprogram name (along with any parameters). The command-prompt window will not close when the

program terminates.

For what it's worth, I use the command-line method.

Step 6: Debug.

When things aren't happening the way you planned, a source-level debugger can be a great tool in determining

what really is going on. Dev-C++'s basic debugger functions are controlled via the "D e b u g " tab at the bottom ofthe screen; more advanced functions are available in the "D e b u g " menu.

Using the debugger:

The various features of the debugger are pretty obvious. Click the "R u n t o c u r s o r " icon to runyour program and pause at the current source code cursor location; Click "N e x t S t e p " to step

through the code; Click "A d d W a t c h " to monitor variables.

Setting breakpoints is as easy as clicking in the black space next to the line in the source code.See the Dev-C++ help topic "Debugging Your Program" for more information.

Dev-C++ User F.A.Q.

Why do I keep getting errors about "cout", "cin", and "endl" being undeclared?

Page 5: Dev-C++ Tutorial for CSC 161 Studentsvulms.vu.edu.pk/Courses/CS602/Downloads/DevCppTutorialGraphics_… · .iee-eae.c/dec/#e4 1/7 Dev-C++ Tutorial for CSC 161 Students (Maintained

11/25/11 Dev-C++ Tutorial for CSC 161 Students

5/7www.uniqueness-template.com/devcpp/#step4

It has to do with namespaces. You need to add the following line after the includes of your implementation (.cpp)

files:

u s i n g n a m e s p a c e s t d ;

How do I use the C++ string class?Again, it probably has to do with namespaces. First of all, make sure you "#include <string>" (not string.h).

Next, make sure you add "using namespace std;" after your includes.

Example:

# i n c l u d e < i o s t r e a m > # i n c l u d e < s t r i n g > u s i n g n a m e s p a c e s t d ;

i n t m a i n ( ) { s t r i n g s ; s = " T h i s i s a t e s t " ; c o u t < < s < < e n d l ; s y s t e m ( " P A U S E " ) ; r e t u r n 0 ; }

How do I use Borland Graphics Interface (graphics.h)?

For those of you migrating from Borland, you may be wondering where graphics.h is. Unfortunately, graphics.h isa Borland specific library and cannot be used with Dev-C++. Fortunately, a benevolent soul by the name of

Michael Main has modified a BGI emulation library for Windows applications to be used under MinGW (and

therefore Dev-C++) which he has aptly named WinBGIm.

The files we need are:

graphics.h (download to C:\Dev-Cpp\include)

libbgi.a (download to C:\Dev-Cpp\lib)

After you have downloaded the files to the correct locations, you can now use WinBGIm's graphic.h as you

would Borland's graphics.h with a few caveats.

Using library files:

First, you have to tell Dev-C++ where to find the library functions that WinBGIm references--this is done in the

"P r o j e c t O p t i o n s " dialog box.Here are instructions on how to do this with a new project:

Follow step 2 and step 3 of "Using Dev-C++".

Go to " P r o j e c t " menu and choose " P r o j e c t O p t i o n s " (or just press ALT+P).Go to the " P a r a m e t e r s " tab

In the "L i n k e r " field, enter the following text:

Page 6: Dev-C++ Tutorial for CSC 161 Studentsvulms.vu.edu.pk/Courses/CS602/Downloads/DevCppTutorialGraphics_… · .iee-eae.c/dec/#e4 1/7 Dev-C++ Tutorial for CSC 161 Students (Maintained

11/25/11 Dev-C++ Tutorial for CSC 161 Students

6/7www.uniqueness-template.com/devcpp/#step4

- l b g i- l g d i 3 2- l c o m d l g 3 2- l u u i d- l o l e a u t 3 2- l o l e 3 2

Project Options -> Parameters:

Click "O K ".Follow step 4, step 5 and step 6 of "Using Dev-C++".

BGI and WinBGIm differences:

WinBGIm is a superset of BGI and as such may have functions and features with which you are unfamiliar. SeeMichael Main's BGI Documentation for more information.

Test code:

Just to make sure you've got everything set up correctly, try this test code in a new Dev-C++ WinBGIm project:

# i n c l u d e < g r a p h i c s . h >

i n t m a i n ( ) { i n i t w i n d o w ( 4 0 0 , 3 0 0 ) ; / / o p e n a 4 0 0 x 3 0 0 g r a p h i c s w i n d o w m o v e t o ( 0 , 0 ) ; l i n e t o ( 5 0 , 5 0 ) ; w h i l e ( ! k b h i t ( ) ) ; / / w a i t f o r u s e r t o p r e s s a k e y c l o s e g r a p h ( ) ; / / c l o s e g r a p h i c s w i n d o w r e t u r n 0 ; }

If you've done everything correctly, you should get a simple graphic that closes when the user presses a key.

Where is <sstream> (string streams)?

In previous versions of Dev-C++, the included compiler did not implement sstream. Fortunately, new versions of

Dev-C++ do not have this problem. Upgrade to the latest version of Dev-C++.

Page change log:

9/12/2006:Overhauled page to be current with Dev-C++ 5 Beta 9 (4.9.9.2).

10/10/2001:Added sstream info in FAQ.

Page 7: Dev-C++ Tutorial for CSC 161 Studentsvulms.vu.edu.pk/Courses/CS602/Downloads/DevCppTutorialGraphics_… · .iee-eae.c/dec/#e4 1/7 Dev-C++ Tutorial for CSC 161 Students (Maintained

11/25/11 Dev-C++ Tutorial for CSC 161 Students

7/7www.uniqueness-template.com/devcpp/#step4

4/16/2001:Fixed error in winbgim.h (conio.h confict).

3/23/2001:Updated WinBGIm procedures.

3/21/2001:Started FAQ section with string and BGI info.

3/19/2001:Updated string info.

3/16/2001:Added string info.

That's it for now.I am not a Dev-C++ expert by any means (in fact, I now use Apple's Xcode), but if you have anyquestions, feel free to email me at [email protected]

Happy coding!

Copyright ©2006 Michael Serrano.

Last Modified 3:13 PM 9/12/2006