supplement 1 c

Upload: tanzid-sultan

Post on 03-Jun-2018

212 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/12/2019 Supplement 1 c

    1/4 Copyright Y. Daniel Liang, 2007

    1

    Supplement I.C

    Creating, Compiling and Running C++ Programs from

    the Command Window

    For Introduction to C++ ProgrammingBy Y. Daniel Liang

    Thi s suppl ement cover s t he f ol l owi ng t opi cs:

    Openi ng a Command Wi ndow ( 1. 2) .

    Usi ng Si mpl e DOS Commands ( cr eat e, change di r ect or i es,di spl ay f i l es, del et e f i l es and di rect or i es) ( 1. 3) .

    Cr eat i ng and Edi t i ng Progr ams Usi ng Not epad and Wor dPad( 1. 4) .

    Compi l i ng and Runni ng Progr ams ( 1. 5) .

    1.1 Introduction

    Devel opi ng C++ pr ograms f r om t he command wi ndows, you havet o t ype the commands. Assume you have successf ul l y i nst al l eda C++ compi l er . St ar t a Command wi ndow by cl i cki ng t heWi ndows St ar t but t on and choosi ng Run t o open t he Run di al ogbox, as shown i n Fi gur e 1. 1. Ent er cmd i n t he Open f i el d andcl i ck OK t o di spl ay a command wi ndow, as shown i n Fi gur e1. 2.

    Figure 1.1

    You can start the command window from the Windows Start

    button.

    The Run dialog

  • 8/12/2019 Supplement 1 c

    2/4 Copyright Y. Daniel Liang, 2007

    2

    You wi l l see t he command prompt wi ndow di spl ayed as shown i nFi gur e 1. 2.

    Figure 1.2

    You can type the commands in the command window.

    Now t ype g++ i n t he DOS pr ompt . I f your C++ compi l er i sconf i gur ed pr oper l y, t he command di spl ays t he message, as i nFi gur e 1. 3. Ot her wi se, an er r or message i s di spl ayed t oi ndi cate t hat t he command i s not f ound, as shown i n Fi gur e1. 4. To f i x thi s probl em, add c: \ Mi nGW\ bi n t o your Wi ndowsPATH envi r onment var i abl e.

    Figure 1.3

    The g++command compiles C++ programs.

    Figure 1.4

    The g++command is not found if the command is not in thePATH.

    1.3 Simple DOS Commands

  • 8/12/2019 Supplement 1 c

    3/4 Copyright Y. Daniel Liang, 2007

    3

    To wor k wi t h J DK on Wi ndows, you need t o know some si mpl eDOS commands. Her e ar e several f r equent l y needed commands:

    dir - - Li st s al l f i l es i n the di rectory.

    mkdir dirName - - Cr eat es a new di r ect ory nameddi r Name.

    cd dirName - - Changes t o t he speci f i ed di r ect or y. Forexampl e, cd c:\ changes t o t he di r ect or y c: \

    cd . . - - Changes t o t he par ent di r ect or y.

    del filename Del et es a f i l e. For exampl e, delTest.cpp del et es t he f i l e named Test . cpp i n t he cur r entdi r ect or y.

    del *.* Del et es al l f i l es i n t he di r ect or y. [ Caut i on:f i l es del et ed f r om t he command wi ndow cannot ber ecover ed. ]

    rmdir dirName Del et es t he speci f i ed di r ect or ydi r Name.

    type filename.cpp Di spl ays t he cont ent s of t hespeci f i ed f i l e.

    1.4 Creating and Editing Programs Using Notepad and WordPad

    You can cr eat e pr ograms usi ng Wi ndows Not epad or Wor dPad. Todo so, f i r st open a command wi ndow; change t he di r ect ory t owher e your progr ams shoul d be st ored, and t ype notepadfilename.cpp or write filename.cpp t o cr eat e a f i l e f or C++sour ce code. For exampl e, t he f ol l owi ng command st ar t sNot epad t o creat e or edi t Wel come. cpp:

    notepad Welcome.cpp

    I f Wel come. cpp does not exi st , a di al og box i s di spl ayed toal er t you t hat i t i s a new f i l e, as shown i n Fi gur e 1. 5.

    Figure 1.5

    The Notepad alerts you that the specified file is new.

    Cl i ck Yes t o creat e a new f i l e. You can t ype t he code f or

  • 8/12/2019 Supplement 1 c

    4/4