647 lines
11 KiB
HTML
647 lines
11 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
|
|
<HTML
|
|
><HEAD
|
|
><TITLE
|
|
>Programming </TITLE
|
|
><META
|
|
NAME="GENERATOR"
|
|
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
|
|
REL="HOME"
|
|
TITLE="From VMS to Linux HOWTO"
|
|
HREF="index.html"><LINK
|
|
REL="PREVIOUS"
|
|
TITLE="TeXing "
|
|
HREF="texing.html"><LINK
|
|
REL="NEXT"
|
|
TITLE="Graphics "
|
|
HREF="graphics.html"></HEAD
|
|
><BODY
|
|
CLASS="SECT1"
|
|
BGCOLOR="#FFFFFF"
|
|
TEXT="#000000"
|
|
LINK="#0000FF"
|
|
VLINK="#840084"
|
|
ALINK="#0000FF"
|
|
><DIV
|
|
CLASS="NAVHEADER"
|
|
><TABLE
|
|
SUMMARY="Header navigation table"
|
|
WIDTH="100%"
|
|
BORDER="0"
|
|
CELLPADDING="0"
|
|
CELLSPACING="0"
|
|
><TR
|
|
><TH
|
|
COLSPAN="3"
|
|
ALIGN="center"
|
|
>From VMS to Linux HOWTO</TH
|
|
></TR
|
|
><TR
|
|
><TD
|
|
WIDTH="10%"
|
|
ALIGN="left"
|
|
VALIGN="bottom"
|
|
><A
|
|
HREF="texing.html"
|
|
ACCESSKEY="P"
|
|
>Prev</A
|
|
></TD
|
|
><TD
|
|
WIDTH="80%"
|
|
ALIGN="center"
|
|
VALIGN="bottom"
|
|
></TD
|
|
><TD
|
|
WIDTH="10%"
|
|
ALIGN="right"
|
|
VALIGN="bottom"
|
|
><A
|
|
HREF="graphics.html"
|
|
ACCESSKEY="N"
|
|
>Next</A
|
|
></TD
|
|
></TR
|
|
></TABLE
|
|
><HR
|
|
ALIGN="LEFT"
|
|
WIDTH="100%"></DIV
|
|
><DIV
|
|
CLASS="SECT1"
|
|
><H1
|
|
CLASS="SECT1"
|
|
><A
|
|
NAME="PROGRAMMING"
|
|
></A
|
|
>5. Programming</H1
|
|
><P
|
|
>Programming under Linux is <EM
|
|
>much</EM
|
|
> 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 <TT
|
|
CLASS="LITERAL"
|
|
>emacs</TT
|
|
> or <TT
|
|
CLASS="LITERAL"
|
|
>jed</TT
|
|
>, as seen above.</P
|
|
><DIV
|
|
CLASS="SECT2"
|
|
><H2
|
|
CLASS="SECT2"
|
|
><A
|
|
NAME="AEN284"
|
|
></A
|
|
>5.1. Fortran</H2
|
|
><P
|
|
>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 <TT
|
|
CLASS="LITERAL"
|
|
>/usr/doc/g77/DOC</TT
|
|
> or <TT
|
|
CLASS="LITERAL"
|
|
>/usr/doc/f2c/f2c.ps</TT
|
|
>
|
|
for details.</P
|
|
><P
|
|
>Your sysadm has installed a native compiler called <TT
|
|
CLASS="LITERAL"
|
|
>g77</TT
|
|
> (good but, as of
|
|
version 0.5.21, still not perfectly compatible with DEC Fortran) or possibly
|
|
the Fortran to C translator, <TT
|
|
CLASS="LITERAL"
|
|
>f2c</TT
|
|
>, and one of the front-ends that make it
|
|
mimic a native compiler. In my experience, the package <TT
|
|
CLASS="LITERAL"
|
|
>yaf77</TT
|
|
> is the
|
|
one that provides best results.</P
|
|
><P
|
|
>To compile a Fortran program with <TT
|
|
CLASS="LITERAL"
|
|
>g77</TT
|
|
>, edit the source, save it with
|
|
extension <TT
|
|
CLASS="LITERAL"
|
|
>.f</TT
|
|
>, then do:</P
|
|
><P
|
|
> <TABLE
|
|
BORDER="1"
|
|
BGCOLOR="#E0E0E0"
|
|
WIDTH="100%"
|
|
><TR
|
|
><TD
|
|
><FONT
|
|
COLOR="#000000"
|
|
><PRE
|
|
CLASS="SCREEN"
|
|
>$ g77 myprog.f</PRE
|
|
></FONT
|
|
></TD
|
|
></TR
|
|
></TABLE
|
|
> </P
|
|
><P
|
|
>which creates by default an executable called <TT
|
|
CLASS="LITERAL"
|
|
>a.out</TT
|
|
> (you don't have
|
|
to link anything). To give the executable a different name and do some
|
|
optimisation:</P
|
|
><P
|
|
> <TABLE
|
|
BORDER="1"
|
|
BGCOLOR="#E0E0E0"
|
|
WIDTH="100%"
|
|
><TR
|
|
><TD
|
|
><FONT
|
|
COLOR="#000000"
|
|
><PRE
|
|
CLASS="SCREEN"
|
|
>$ g77 -O2 -o myprog myprog.f</PRE
|
|
></FONT
|
|
></TD
|
|
></TR
|
|
></TABLE
|
|
> </P
|
|
><P
|
|
>Beware of optimisations! Ask your sysadm to read the documentation that
|
|
comes with the compiler and tell you if there are any problems.</P
|
|
><P
|
|
>To compile a subroutine:</P
|
|
><P
|
|
> <TABLE
|
|
BORDER="1"
|
|
BGCOLOR="#E0E0E0"
|
|
WIDTH="100%"
|
|
><TR
|
|
><TD
|
|
><FONT
|
|
COLOR="#000000"
|
|
><PRE
|
|
CLASS="SCREEN"
|
|
>$ g77 -c mysub.f</PRE
|
|
></FONT
|
|
></TD
|
|
></TR
|
|
></TABLE
|
|
> </P
|
|
><P
|
|
>This creates a file <TT
|
|
CLASS="LITERAL"
|
|
>mysub.o</TT
|
|
>. To link this subroutine to a program,
|
|
you'll do</P
|
|
><P
|
|
> <TABLE
|
|
BORDER="1"
|
|
BGCOLOR="#E0E0E0"
|
|
WIDTH="100%"
|
|
><TR
|
|
><TD
|
|
><FONT
|
|
COLOR="#000000"
|
|
><PRE
|
|
CLASS="SCREEN"
|
|
>$ g77 -o myprog myprog.f mysub.o</PRE
|
|
></FONT
|
|
></TD
|
|
></TR
|
|
></TABLE
|
|
> </P
|
|
><P
|
|
>If you have many external subroutines and you want to make a library, do the
|
|
following:</P
|
|
><P
|
|
> <TABLE
|
|
BORDER="1"
|
|
BGCOLOR="#E0E0E0"
|
|
WIDTH="100%"
|
|
><TR
|
|
><TD
|
|
><FONT
|
|
COLOR="#000000"
|
|
><PRE
|
|
CLASS="SCREEN"
|
|
>$ cd subroutines/
|
|
$ cat *f >mylib.f ; g77 -c mylib.f</PRE
|
|
></FONT
|
|
></TD
|
|
></TR
|
|
></TABLE
|
|
> </P
|
|
><P
|
|
>This will create <TT
|
|
CLASS="LITERAL"
|
|
>mylib.o</TT
|
|
> that you can link to your programs.</P
|
|
><P
|
|
>Finally, to link an external library called, say, <TT
|
|
CLASS="LITERAL"
|
|
>libdummy.so</TT
|
|
>:</P
|
|
><P
|
|
> <TABLE
|
|
BORDER="1"
|
|
BGCOLOR="#E0E0E0"
|
|
WIDTH="100%"
|
|
><TR
|
|
><TD
|
|
><FONT
|
|
COLOR="#000000"
|
|
><PRE
|
|
CLASS="SCREEN"
|
|
>$ g77 -o myprog myprog.f -ldummy</PRE
|
|
></FONT
|
|
></TD
|
|
></TR
|
|
></TABLE
|
|
> </P
|
|
><P
|
|
>If you have <TT
|
|
CLASS="LITERAL"
|
|
>f2c</TT
|
|
>, you only have to use <TT
|
|
CLASS="LITERAL"
|
|
>f77</TT
|
|
> or <TT
|
|
CLASS="LITERAL"
|
|
>fort77</TT
|
|
> instead
|
|
of <TT
|
|
CLASS="LITERAL"
|
|
>g77</TT
|
|
>.</P
|
|
><P
|
|
>Another useful programming tool is <TT
|
|
CLASS="LITERAL"
|
|
>make</TT
|
|
>, described below.</P
|
|
></DIV
|
|
><DIV
|
|
CLASS="SECT2"
|
|
><H2
|
|
CLASS="SECT2"
|
|
><A
|
|
NAME="AEN326"
|
|
></A
|
|
>5.2. Using <TT
|
|
CLASS="LITERAL"
|
|
>make</TT
|
|
></H2
|
|
><P
|
|
>The utility <TT
|
|
CLASS="LITERAL"
|
|
>make</TT
|
|
> is a tool to handle the compilation of programs that
|
|
are split into several source files. The VMS counterparts are <TT
|
|
CLASS="LITERAL"
|
|
>MMS</TT
|
|
>
|
|
and <TT
|
|
CLASS="LITERAL"
|
|
>MMK</TT
|
|
>, which have a different syntax.</P
|
|
><P
|
|
>Let's suppose you have source files containing your routines, <TT
|
|
CLASS="LITERAL"
|
|
>file_1.f,
|
|
file_2.f, file_3.f</TT
|
|
>, and a source file of the main program that uses the
|
|
routines, <TT
|
|
CLASS="LITERAL"
|
|
>myprog.f</TT
|
|
>. 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.</P
|
|
><P
|
|
>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.</P
|
|
><P
|
|
>In our example, you'd write a makefile like this:</P
|
|
><P
|
|
> <TABLE
|
|
BORDER="0"
|
|
BGCOLOR="#E0E0E0"
|
|
WIDTH="100%"
|
|
><TR
|
|
><TD
|
|
><FONT
|
|
COLOR="#000000"
|
|
><PRE
|
|
CLASS="PROGRAMLISTING"
|
|
> # 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.</PRE
|
|
></FONT
|
|
></TD
|
|
></TR
|
|
></TABLE
|
|
> </P
|
|
><P
|
|
>Save this file as <TT
|
|
CLASS="LITERAL"
|
|
>Makefile</TT
|
|
> and type <TT
|
|
CLASS="LITERAL"
|
|
>make</TT
|
|
> to compile your program;
|
|
alternatively, save it as <TT
|
|
CLASS="LITERAL"
|
|
>myprog.mak</TT
|
|
> and type <TT
|
|
CLASS="LITERAL"
|
|
>make -f myprog.mak</TT
|
|
>.
|
|
And of course, RMP.</P
|
|
></DIV
|
|
><DIV
|
|
CLASS="SECT2"
|
|
><H2
|
|
CLASS="SECT2"
|
|
><A
|
|
NAME="AEN345"
|
|
></A
|
|
>5.3. Shell Scripts</H2
|
|
><P
|
|
>Shell scripts are the equivalent of VMS' command files, and allow for very
|
|
powerful constructs.</P
|
|
><P
|
|
>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 <TT
|
|
CLASS="LITERAL"
|
|
>chmod +x <scriptfile></TT
|
|
>. To execute it, type its name.</P
|
|
><P
|
|
>Writing scripts under <TT
|
|
CLASS="LITERAL"
|
|
>bash</TT
|
|
> 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.</P
|
|
><P
|
|
>EXAMPLE: sample.sh</P
|
|
><P
|
|
> <TABLE
|
|
BORDER="0"
|
|
BGCOLOR="#E0E0E0"
|
|
WIDTH="100%"
|
|
><TR
|
|
><TD
|
|
><FONT
|
|
COLOR="#000000"
|
|
><PRE
|
|
CLASS="PROGRAMLISTING"
|
|
>#!/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." </PRE
|
|
></FONT
|
|
></TD
|
|
></TR
|
|
></TABLE
|
|
> </P
|
|
></DIV
|
|
><DIV
|
|
CLASS="SECT2"
|
|
><H2
|
|
CLASS="SECT2"
|
|
><A
|
|
NAME="C"
|
|
></A
|
|
>5.4. C</H2
|
|
><P
|
|
>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
|
|
<TT
|
|
CLASS="LITERAL"
|
|
>hello.c</TT
|
|
> you'll use the <TT
|
|
CLASS="LITERAL"
|
|
>gcc</TT
|
|
> compiler, which comes as part of Linux
|
|
and has the same syntax as <TT
|
|
CLASS="LITERAL"
|
|
>g77</TT
|
|
>:</P
|
|
><P
|
|
> <TABLE
|
|
BORDER="1"
|
|
BGCOLOR="#E0E0E0"
|
|
WIDTH="100%"
|
|
><TR
|
|
><TD
|
|
><FONT
|
|
COLOR="#000000"
|
|
><PRE
|
|
CLASS="SCREEN"
|
|
>$ gcc -O2 -o hello hello.c</PRE
|
|
></FONT
|
|
></TD
|
|
></TR
|
|
></TABLE
|
|
> </P
|
|
><P
|
|
>To link a library to a program, add the switch <TT
|
|
CLASS="LITERAL"
|
|
>-l<libname></TT
|
|
>.
|
|
For example, to link the math library and optimize do</P
|
|
><P
|
|
> <TABLE
|
|
BORDER="1"
|
|
BGCOLOR="#E0E0E0"
|
|
WIDTH="100%"
|
|
><TR
|
|
><TD
|
|
><FONT
|
|
COLOR="#000000"
|
|
><PRE
|
|
CLASS="SCREEN"
|
|
>$ gcc -O2 -o mathprog mathprog.c -lm</PRE
|
|
></FONT
|
|
></TD
|
|
></TR
|
|
></TABLE
|
|
> </P
|
|
><P
|
|
>(The <TT
|
|
CLASS="LITERAL"
|
|
>-l<libname></TT
|
|
> switch forces <TT
|
|
CLASS="LITERAL"
|
|
>gcc</TT
|
|
> to link the library
|
|
<TT
|
|
CLASS="LITERAL"
|
|
>/usr/lib/lib<libname>.a</TT
|
|
>; so <TT
|
|
CLASS="LITERAL"
|
|
>-lm</TT
|
|
> links
|
|
<TT
|
|
CLASS="LITERAL"
|
|
>/usr/lib/libm.a</TT
|
|
>).</P
|
|
><P
|
|
>When your program is made of several source files, you'll need to use the
|
|
utility <TT
|
|
CLASS="LITERAL"
|
|
>make</TT
|
|
> described above. Just use <TT
|
|
CLASS="LITERAL"
|
|
>gcc</TT
|
|
> and C source files in
|
|
the makefile.</P
|
|
><P
|
|
>You can invoke some help about the C functions, that are covered by man
|
|
pages, section 3; for example,</P
|
|
><P
|
|
> <TABLE
|
|
BORDER="1"
|
|
BGCOLOR="#E0E0E0"
|
|
WIDTH="100%"
|
|
><TR
|
|
><TD
|
|
><FONT
|
|
COLOR="#000000"
|
|
><PRE
|
|
CLASS="SCREEN"
|
|
>$ man 3 printf</PRE
|
|
></FONT
|
|
></TD
|
|
></TR
|
|
></TABLE
|
|
> </P
|
|
><P
|
|
>There are lots of libraries available out there; among the first you'll want
|
|
to use are <TT
|
|
CLASS="LITERAL"
|
|
>ncurses</TT
|
|
>, to handle text mode effects, and <TT
|
|
CLASS="LITERAL"
|
|
>svgalib</TT
|
|
>, to
|
|
do graphics.</P
|
|
></DIV
|
|
></DIV
|
|
><DIV
|
|
CLASS="NAVFOOTER"
|
|
><HR
|
|
ALIGN="LEFT"
|
|
WIDTH="100%"><TABLE
|
|
SUMMARY="Footer navigation table"
|
|
WIDTH="100%"
|
|
BORDER="0"
|
|
CELLPADDING="0"
|
|
CELLSPACING="0"
|
|
><TR
|
|
><TD
|
|
WIDTH="33%"
|
|
ALIGN="left"
|
|
VALIGN="top"
|
|
><A
|
|
HREF="texing.html"
|
|
ACCESSKEY="P"
|
|
>Prev</A
|
|
></TD
|
|
><TD
|
|
WIDTH="34%"
|
|
ALIGN="center"
|
|
VALIGN="top"
|
|
><A
|
|
HREF="index.html"
|
|
ACCESSKEY="H"
|
|
>Home</A
|
|
></TD
|
|
><TD
|
|
WIDTH="33%"
|
|
ALIGN="right"
|
|
VALIGN="top"
|
|
><A
|
|
HREF="graphics.html"
|
|
ACCESSKEY="N"
|
|
>Next</A
|
|
></TD
|
|
></TR
|
|
><TR
|
|
><TD
|
|
WIDTH="33%"
|
|
ALIGN="left"
|
|
VALIGN="top"
|
|
>TeXing</TD
|
|
><TD
|
|
WIDTH="34%"
|
|
ALIGN="center"
|
|
VALIGN="top"
|
|
> </TD
|
|
><TD
|
|
WIDTH="33%"
|
|
ALIGN="right"
|
|
VALIGN="top"
|
|
>Graphics</TD
|
|
></TR
|
|
></TABLE
|
|
></DIV
|
|
></BODY
|
|
></HTML
|
|
> |