1. Computing

Discuss in my forum

Linux Newbie Administrator Guide

By

"Hello World" -command exit

pack .my_button

The first line (starting with the "#!" pound-bang) tells the shell what utility to use to execute my text file. The next two lines are an example of a simple tk program. First, I created a button called "my_button" and placed it at the root of my class hierarchy (the dot in front of "my_button"). To the button, I tied the text "Hello World" and a command that exits the program (when the button is pressed). Last line makes my program's window adjust its size to just big enough to contain my button.

After saving the file, I make it executable:

chmod a+x try_tk

after which I can run it by typing (in the X-terminal, because it requires X-windows to run):

./try_tk

Tk is very popular for building GUI front ends.

8.2.4 ruby

ruby

A purely object-oriented scripting language. This language is a relative newcomer, but it is rapidly gaining popularity, and may well be the flavour of the future of programming.

To write a simple program in ruby, I open my favorite text editor and start a program with the following first line:

#!/usr/bin/ruby

Here is an example of a program that I wrote to help me understand the basics of the ruby language:

#!/usr/bin/ruby

#This is a comment

a = Array.new

print "Please enter a few words (type EXIT to stop):\n"

i = 0

while enterWord = STDIN.gets

enterWord.chop!

if enterWord == "EXIT"

break

end

a = enterWord

i += 1

end

#sort the array

for i in 0...a.length-1 do

for j in i+1...a.length do

if a[j] < a

tmp = a

a = a[j]

a[j] = tmp

end

end

end

#Output the results

print "You entered " + a.length.to_s + " entries.\n\n"

for i in 0...a.length do

print "Entry " + (i+1).to_s + ": "+ a + "\n"

end

I save my ruby script to file "myprogram". To execute it, I need to type on the command line:

./myprogram

8.2.5 C

gcc filename.c

GNU C compiler. Quite straight-forward if you know C. Extensive free manuals are available on the net.

How do I compile a simple C program?

Start your favourite text editor and type in your source code. For example, I may use pico:

pico hello.c

and type in the Kerningham and Richie (the creators of "c") intro example of a C program:

#include <stdio.h>

void main(void) {

printf("hello world\n") ;

}

I save the file and then invoke the GNU C compiler to compile the file "hello.c":

gcc hello.c

The gcc compiler produces an executable binary file "a.out", which I can run:

./a.out

8.2.6 C++

g++ filename.C

GNU C++ compiler. The capital "C" is often used for C++ sources. If you need an "integrated development environment" (IDE), kdevelop is really something you would probably like to look at.

How do I compile a simple C++ program?

Just like in c, I open a text editor and write my program. For example, using pico, I write the following program:

//This is a comment (to the end of line, C++ style)

#include <stdio.h>

#include <math.h>

#include <iostream.h>

#include <stdlib.h>

//define a function

double wheeldrop (double dGap, double dDiameter) {

double dDrop, dRadius, dNotDrop;


dRadius = dDiameter * 0.5;

dDrop = dRadius - sqrt( (dRadius*dRadius)-(0.25*dGap*dGap) ) ;


return (dDrop) ;

} //end of the function


//The function main is the entry point to the program

void main(void) {

double dGap, dDiameter, dDrop, dRadius, dNotDrop; //variables


for (;;) { //infinite loop

cout << "Please enter gap between track segments and \n"

<< "diameter of train wheel in

  1. About.com
  2. Computing
  3. Linux

©2014 About.com. All rights reserved.