1 Supplement I.C Creating, Compiling and Running C++ Programs from the Command Window For Introduction to C++ Programming By 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 r ect or i es) ( 1. 3) . Cr eat i ng and Edi t i ng Pr ogr ams Usi ng Not epad and Wor dPad ( 1. 4) . Compi l i ng and Runni ng Pr ogr ams ( 1. 5) .
1.1 Introduction
Devel opi ng C++ pr ogr ams f r omt he command wi ndows, you have t o t ype t he commands. Assume you have successf ul l y i nst al l ed a C++ compi l er . St ar t a Command wi ndow by cl i cki ng t he Wi ndows St ar t but t on and choosi ng Run t o open t he Run di al og box, as shown i n Fi gur e 1. 1. Ent er cmd i n t he Open f i el d and cl i ck OK t o di spl ay a command wi ndow, as shown i n Fi gur e 1. 2.
Figure 1.1 You can start the command window from the Windows Start button. The Run dialog
Copyright Y. Daniel Liang, 2007
2 You wi l l see t he command pr ompt wi ndow di spl ayed as shown i n Fi 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 s conf i gur ed pr oper l y, t he command di spl ays t he message, as i n Fi gur e 1. 3. Ot her wi se, an er r or message i s di spl ayed t o i ndi cat e t hat t he command i s not f ound, as shown i n Fi gur e 1. 4. To f i x t hi s pr obl em, add c: \ Mi nGW\ bi n t o your Wi ndows PATH 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 the PATH. 1.3 Simple DOS Commands
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 e DOS commands. Her e ar e sever al f r equent l y needed commands:
dir - - Li st s al l f i l es i n t he di r ect or y. mkdir dirName - - Cr eat es a new di r ect or y named di r Name. cd dirName - - Changes t o t he speci f i ed di r ect or y. For exampl 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, del Test.cpp del et es t he f i l e named Test . cpp i n t he cur r ent di 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 omt he command wi ndow cannot be r ecover ed. ] rmdir dirName Del et es t he speci f i ed di r ect or y di r Name. type filename.cpp Di spl ays t he cont ent s of t he speci f i ed f i l e.
1.4 Creating and Editing Programs Using Notepad and WordPad
You can cr eat e pr ogr ams usi ng Wi ndows Not epad or Wor dPad. To do so, f i r st open a command wi ndow; change t he di r ect or y t o wher e your pr ogr ams shoul d be st or ed, and t ype notepad filename.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 s Not epad t o cr eat 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 t o al 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 cr eat e a new f i l e. You can t ype t he code f or Copyright Y. Daniel Liang, 2007
4 t he pr ogr ami n t he Not epad, as shown i n Fi gur e 1. 6. Choose File, Save t o save t he f i l e.
Figure 1.6 You can use Notepad to create and edit C++ source files.
1.5 Compiling and Running Programs
To compi l e t he sour ce code, use t he bcc32 command. For exampl e, t he f ol l owi ng command compi l es Wel come. cpp: g++ Welcome.cpp
I f compi l ed successf ul l y, t he execut abl e f i l e Wel come. exe i s cr eat ed. To r un t he f i l e, si mpl y t ype Welcome, as shown i n Fi gur e 1. 7.
Python Programming Reference Guide: A Comprehensive Guide for Beginners to Master the Basics of Python Programming Language with Practical Coding & Learning Tips