The OpenNET Project / Index page

[ новости /+++ | форум | теги | ]

Поиск:  Каталог документации

Next Previous Contents

5. Programming

Programming under Linux is much better: there are lots of tools that make programming easier and quicker. For instance, the drudgery of editing--saving--exiting--compiling--re-editing can be cut short by using editors like emacs or jed, as seen above.

5.1 Fortran

Not substantial differences here, but note that at the time of writing the available (free) compilers are not 100% compatible with VMS'; expect some minor quirks. (It's actually the VMS compiler which has non-standard extensions.) See /usr/doc/g77/DOC or /usr/doc/f2c/f2c.ps for details.

Your sysadm has installed a native compiler called g77 (good but, as of version 0.5.21, still not perfectly compatible with DEC Fortran) or possibly the Fortran to C translator, f2c, and one of the front-ends that make it mimic a native compiler. In my experience, the package yaf77 is the one that provides best results.

To compile a Fortran program with g77, edit the source, save it with extension .f, then do:

$ g77 myprog.f

which creates by default an executable called a.out (you don't have to link anything). To give the executable a different name and do some optimisation:

$ g77 -O2 -o myprog myprog.f

Beware of optimisations! Ask your sysadm to read the documentation that comes with the compiler and tell you if there are any problems.

To compile a subroutine:

$ g77 -c mysub.f

This creates a file mysub.o. To link this subroutine to a program, you'll do

$ g77 -o myprog myprog.f mysub.o

If you have many external subroutines and you want to make a library, do the following:

$ cd subroutines/
$ cat *f >mylib.f ; g77 -c mylib.f

This will create mylib.o that you can link to your programs.

Finally, to link an external library called, say, libdummy.so:

$ g77 -o myprog myprog.f -ldummy

If you have f2c, you only have to use f77 or fort77 instead of g77.

Another useful programming tool is make, described below.

5.2 Using make

The utility make is a tool to handle the compilation of programs that are split into several source files. The VMS counterparts are MMS and MMK, which have a different syntax.

Let's suppose you have source files containing your routines, file_1.f, file_2.f, file_3.f, and a source file of the main program that uses the routines, myprog.f. If you compile your program manually, whenever you modify one of the source files you have to figure out which file depends on which, which file to recompile first, and so on.

Instead of getting mad, you can write a `makefile'. This is a text file containing the dependencies between your sources: when one is modified, only the ones that depend on the modified file will be recompiled.

In our example, you'd write a makefile like this:



# This is makefile
# Press the <TAB> key where you see <TAB>!
# It's important: don't use spaces instead.

myprog: myprog.o file_1.o file_2.o file_3.o
<TAB>g77 -o myprog myprog.o file_1.o file_2.o file_3.o
# myprog depends on four object files

myprog.o: myprog.f
<TAB>g77 -c myprog.f
# myprog.o depends on its source file

file_1.o: file_1.f
<TAB>g77 -c file_1.f
# file_1.o depends on its source file

file_2.o: file_2.f file_1.o
<TAB>g77 -c file_2.f file_1.o
# file_2.o depends on its source file and an object file

file_3.o: file_3.f file_2.o
<TAB>g77 -c file_3.f file_2.o
# file_3.o depends on its source file and an object file

# end of makefile.

Save this file as Makefile and type make to compile your program; alternatively, save it as myprog.mak and type make -f myprog.mak. And of course, RMP.

5.3 Shell Scripts

Shell scripts are the equivalent of VMS' command files, and allow for very powerful constructs.

To write a script, all you have to do is write a standard ASCII file containing the commands, save it, then make it executable with the command chmod +x <scriptfile>. To execute it, type its name.

Writing scripts under bash is such a vast subject it would require a book by itself, and I will not delve into the topic any further. I'll just give you a more-or-less comprehensive and (hopefully) useful example you can extract some basic rules from.

EXAMPLE: sample.sh


#!/bin/sh
# sample.sh
# I am a comment
# don't change the first line, it must be there
echo "This system is: `uname -a`" # use the output of the command
echo "My name is $0" # built-in variables
echo "You gave me the following $# parameters: "$*
echo "First parameter is: "$1
echo -n "What's your name? " ; read your_name
echo notice the difference: "hi $your_name" # quoting with "
echo notice the difference: 'hi $your_name' # quoting with '
DIRS=0 ; FILES=0
for file in `ls .` ; do
  if [ -d ${file} ] ; then # if file is a directory
    DIRS=`expr $DIRS + 1`  # this means DIRS = DIRS + 1
  elif [ -f ${file} ] ; then
    FILES=`expr $FILES + 1`
  fi
  case ${file} in
    *.gif|*jpg) echo "${file}: graphic file" ;;
    *.txt|*.tex) echo "${file}: text file" ;;
    *.c|*.f|*.for) echo "${file}: source file" ;;
    *) echo "${file}: generic file" ;;
  esac
done
echo "there are ${DIRS} directories and ${FILES} files"
ls | grep "ZxY--!!!WKW"
if [ $? != 0 ] ; then # exit code of last command
  echo "ZxY--!!!WKW not found"
fi
echo "enough... type 'man bash' if you want more info."

5.4 C

Linux is an excellent environment to program in C. Taken for granted that you know C, here are a couple of guidelines. To compile your standard hello.c you'll use the gcc compiler, which comes as part of Linux and has the same syntax as g77:

$ gcc -O2 -o hello hello.c

To link a library to a program, add the switch -l<libname>. For example, to link the math library and optimize do

$ gcc -O2 -o mathprog mathprog.c -lm

(The -l<libname> switch forces gcc to link the library /usr/lib/lib<libname>.a; so -lm links /usr/lib/libm.a).

When your program is made of several source files, you'll need to use the utility make described above. Just use gcc and C source files in the makefile.

You can invoke some help about the C functions, that are covered by man pages, section 3; for example,

$ man 3 printf

There are lots of libraries available out there; among the first you'll want to use are ncurses, to handle text mode effects, and svgalib, to do graphics.


Next Previous Contents


Партнёры:
PostgresPro
Inferno Solutions
Hosting by Hoster.ru
Хостинг:

Закладки на сайте
Проследить за страницей
Created 1996-2024 by Maxim Chirkov
Добавить, Поддержать, Вебмастеру