Programming languages by example

Embed Size (px)

DESCRIPTION

The programming languages that have compilers operating under Linux OS and will be covered in this article are: C++, fortran77, gfortran, pascal (free pascal compiler), SmallEiffel, ADA, Haskell, Ocaml and Java. Example programs linking with the following graphical libraries will also be presented: gtkmm, OSG and free GLUT.

Citation preview

Programminglanguagesbyexample Introduction: TheprogramminglanguagesthathavecompilersoperatingunderLinuxOS(weshallbeconcerned herewithopenSuseand/orsimplyMEPISVERSION6.5or7.0)andwillbecoveredhereinthisarticle are:C++,fortran77,gfortran,pascal(freepascalcompiler),SmallEiffel,ADA,Haskell,Ocamland Java.Exampleprogramslinkingwiththefollowinggraphicallibrarieswillalsobepresented:gtkmm, OSGandfreeGLUT. Language Commandline compilingcommandor Example statement g++hellocpp.cppohello1 g77ohellog77hello.for gfortranohellogforhello.for fpchello.pas compilehello.eohellose ghcmakeohellohaskellhello.hs Compilertype& version gccv.4.0.3 gccv.3.4.6 gccv.4.0.3 v.2.2.0 gnueiffel compilerrelease 0.74 v.6.4.1

C++(GNU, g++ gcc) fortran77 g77 (GNU,gcc) gfortran(GNU, gfortran GCC) pascal(free fpc pascal) SmallEiffel (GNU) HASKELL (GHC) ADA(GNU, gcc) compile ghc gccthengnatbl

gccchelloada.adbthengnatblohello2 gccv.4.0.3 helloada.ali ocamloptcamodule.ml,ocamloptc OCaml ocamlopt bmodule.ml,ocamloptohellooc version3.10.2 amodule.cmxbmodule.cmx javachelloj.java,toruntypejava+the JDK,version Java javac classname(eg.HelloClassApp) "1.5.0_13" Let'sgetgraphical:Forexample,GTK+isahighlyusable,toolkitforcreatinggraphicaluserinterfaces (GUI)whichboastscrossplatformcompatibilityandaneasytouseAPI.GTK+itiswritteninC,but hasbindingstomanyotherpopularprogramminglanguagessuchasC++,Perl,PythonandC#among others.GTKMMisthewrapperforC++language(alibraryusedtocreategraphicaluser interfaces).gtkmmspeaksarabic.GTKMMprogramscanbecompiledandrununderWindows operatingsystem.Todoso,youmayfollowthestepsgivenhereafter: 1. 2. 3. 4. 5. 6. DownloadandinstallMingW. DownloadandinstallDevCpp. DownloadandunzipGTKlibraryunderc:\MingW. Downloadandinstallgtkmmlibraryunderc:\DevCpp. Downloadandinstallmsys. Copythesourcecodeinto/home/user_name/directoryofC:\msys.

7. Compilesourcecode,exampleforcommandlinecompilingstatementsinmsyswindow: exportPKG_CONFIG_PATH=c:/DevCpp/gtkmm/pkgconfig g++'pkgconfigcflagsgtkmm2.4'example.ccoexample'pkgconfiglibsgtkmm 2.4' 8. Runexecutable(.exe)Windowsfile Library Commandlinecompiling commandorstatement Example Compilertype& version

g++gtkmareas.ccogtkmareas`pkgconfig g++(GCC)4.2.1 gtkmm2.4cflagslibs` TherearecertainlibrariesthatarewrittenforexampleinC(orC++)thatcanbeaddedtothe microcomputerharddrve.Withtheselibrariesandtheappropriatecompiler,eg.Ccompiler,linkingto suchlibrariesandcompilingyoursource,youcanproduceanoutputfile(2Dor3Dgraphicsone). ExamplesforsuchlibrariesareCairoandOpenSceneGraphics(OSG). Commandline compiling Compilertype Library Example commandor &version statement ccohellocairo$(pkgconfigcflagslibs cc(GCC) cairo cc cairo)hellocairo.c 4.2.1 g++cHelloMain.cpp,g++cHelloSG.cpp, OpenSceneGraph g++ohelloHelloSG.oHelloMain.o g++(GCC) g++ (OSG) I/usr/includelosglosgDBlosgUtil 4.2.1 losgViewerlosgText FreeGLutility gccohellogluthelloglut.clmlGLlGLU gcc(GCC) gcc toolkit(GLUT) lglut 4.2.1 gtkmm g++ General: 1. Makeanewdirectoryunder/home/username(eg.hany)/Documentsandcallitexecutables. 2. Addtothisdirectoryeachexecutable(compiled)file. 3. Gotothedesktop,clickDocuments(Konquererwillopenwithalistoffiles&foldersplusthe menuandprobablyatreelikefiles&foldersonyourPC),clickViewonthemainmenuand thenchooseShowhiddenfiles.Thehiddenfilesthatstartwithadot(.)willappear. 4. Goto/home/username(eg.han)y/directoryandclickon.bashrcfile.Itwillopeninatext editor,probablykwrite.AddthefollowinglinetotheendofthefileexportPATH=/home/user name(eg.hany)/Documents/executables:$PATH.Thensavethefileandcose(exit)theeditor. 5. Gotokonquerer,clickViewinthemenulist,thenchooseandclickonemoretimeShowhidden files,thehiddenoneswilldisappear. Sayinghelloindifferentlanguages: C++: 1. Openanewdocumentinanytexteditor(kwrite,keditorkate). 2. Typethefollowinglines: #include intmain()

3. 4. 5. 6.

{ printf("Helloworldfromgcc/g++\n"); } Savethefileashellocpp.cpp Compileitpertheabovetable Copytheexecutable(compiled)filetotheexecutablesfolder(directory),eg.hello1 Openthekonsolewindowandrunhello1(i.e.typehello1attheprompt).

Fortran77: 1. Openanewdocumentinanytexteditor(kwrite,keditorkate). 2. Typethefollowinglines: programhello cThisprogramprintshello,leaveatleast7spacesbeforetypingprogramhelloabove write(*,*)'hellofromg77,fortran77' stop end 3. Savethefileashello.for 4. Compileitpertheabovetable 5. Copytheexecutable(compiled)filetotheexecutablesfolder(directory),eg.hellog77 6. Openthekonsolewindowandrunhellog77(i.e.typehellog77attheprompt). gFortran: 1. Openanewdocumentinanytexteditor(kwrite,keditorkate). 2. Typethefollowinglines: programhello cThisprogramprintshello,leaveatleast7spacesbeforetypingprogramhelloabove write(*,*)'hellofromg77,fortran77' stop end 3. Savethefileashello.for 4. Compileitpertheabovetable 5. Copytheexecutable(compiled)filetotheexecutablesfolder(directory),eg.hellogfor 6. Openthekonsolewindowandrunhellogfor(i.e.typehellogforattheprompt). Pascal: 1. Openanewdocumentinanytexteditor(kwrite,keditorkate)oropenatrminal(konsole) windowandtypefptoopentheFreePascalIDEforLinux. 2. Typethefollowinglines: programhello; begin writeln('hellofrompascal'); end. 3. Savethefileashello.pas 4. Compileitpertheabovetable 5. Copytheexecutable(compiled)filetotheexecutablesfolder(directory),eg.hello 6. Openthekonsolewindowandrunhello(i.e.typehelloattheprompt).

SmallEiffel: 1. Openanewdocumentinanytexteditor(kwrite,keditorkate). 2. Typethefollowinglines: classSIMPLE creation make feature makeis do io.put_string("HellofromSmallEiffel.%N"); io.put_new_line end; end 3. Savethefileashello.e 4. Compileitpertheabovetable 5. Copytheexecutable(compiled)filetotheexecutablesfolder(directory),eg.hellose 6. Openthekonsolewindowandrunhellose(i.e.typehelloseattheprompt). Haskell: 1. Openanewdocumentinanytexteditor(kwrite,keditorkate). 2. Typethefollowinglines: main=do putStrLn"hello,world,fromhaskellghc!" 3. Savethefileashello.hs 4. Compileitpertheabovetable 5. Copytheexecutable(compiled)filetotheexecutablesfolder(directory),eg.hellohaskell 6. Openthekonsolewindowandrunhellohaskell(i.e.typehellohaskellattheprompt). ADA: 1. Openanewdocumentinanytexteditor(kwrite,keditorkate). 2. Typethefollowinglines: Printasimplemessage. withAda.Text_IO; procedureHelloadais begin Ada.Text_IO.Put_Line("Hello,worldfromada!"); endHelloada; 3. Savethefileashelloada.adb 4. Compileitpertheabovetable 5. Copytheexecutable(compiled)filetotheexecutablesfolder(directory),eg.hello2 6. Openthekonsolewindowandrunhello2(i.e.typehello2attheprompt). OCaml: 1. Openanewdocumentinanytexteditor(kwrite,keditorkate). 2. Typethefollowingline: lethello()=print_endline"HellofromOCaml"

3. 4. 5. 6. Java:

Savethisfileasamodule.ml Openanewdocumentinanytexteditor(kwrite,keditorkate)andtypethefollowingline: Amodule.hello() Savethisfileasbmodule.ml Compileitpertheabovetable Copytheexecutable(compiled)filetotheexecutablesfolder(directory),eg.hellooc Openthekonsolewindowandrunhellooc(i.e.typehelloocattheprompt).

1. Openanewdocumentinanytexteditor(kwrite,keditorkate). 2. Typethefollowinglines: /** *TheHelloWorldAppclassimplementsanapplicationthat *simplyprints"HelloWorld!"tostandardoutput. */ classHelloWorldApp{ publicstaticvoidmain(String[]args){ System.out.println("HelloWorldfromJava!");//Displaythestring. } } 3. Savethefileashellojava.java 4. Compileitpertheabovetable 5. Runthefile,whileyouareinthedirectorywherehellojava.javaandHelloWorldApp.classare residing,bytypingjavaHelloWorldApp gtkmm: 1. Openanewdocumentinanytexteditor(kwrite,keditorkate). 2. Typethefollowingline:#include #include #include #include class HelloWorld : public Gtk::Window { public: HelloWorld(); virtual ~HelloWorld(); protected: //Signal handlers: virtual void on_button_clicked(); //Member widgets: Gtk::Button m_button; }; int main (int argc, char *argv[]) { Gtk::Main kit(argc, argv);

HelloWorld helloworld; //Shows the window and returns when it is closed. Gtk::Main::run(helloworld); } return 0;

HelloWorld::HelloWorld() : m_button("Hello World") // creates a new button with label "Hello World". { // Sets the border width of the window. set_border_width(10); // When the button receives the "clicked" signal, it will call the // on_button_clicked() method defined below. m_button.signal_clicked().connect(sigc::mem_fun(*this, &HelloWorld::on_button_clicked)); // This packs the button into the Window (a container). add(m_button); // The final step is to display this newly created widget... m_button.show();

}

HelloWorld::~HelloWorld() { } void HelloWorld::on_button_clicked() { std::cout