222 lines
8.8 KiB
HTML
222 lines
8.8 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
|
|
<HTML>
|
|
<HEAD>
|
|
<META NAME="GENERATOR" CONTENT="SGML-Tools 1.0.9">
|
|
<TITLE> From DOS/Windows to Linux HOWTO : A Bit of Programming</TITLE>
|
|
<LINK HREF="DOS-Win-to-Linux-HOWTO-11.html" REL=next>
|
|
<LINK HREF="DOS-Win-to-Linux-HOWTO-9.html" REL=previous>
|
|
<LINK HREF="DOS-Win-to-Linux-HOWTO.html#toc10" REL=contents>
|
|
</HEAD>
|
|
<BODY>
|
|
<A HREF="DOS-Win-to-Linux-HOWTO-11.html">Next</A>
|
|
<A HREF="DOS-Win-to-Linux-HOWTO-9.html">Previous</A>
|
|
<A HREF="DOS-Win-to-Linux-HOWTO.html#toc10">Contents</A>
|
|
<HR>
|
|
<H2><A NAME="s10">10. A Bit of Programming</A></H2>
|
|
|
|
<P>
|
|
<P>
|
|
<P>
|
|
<H2><A NAME="Shell Scripts"></A> <A NAME="ss10.1">10.1 Shell Scripts: .BAT Files on Steroids </A>
|
|
</H2>
|
|
|
|
<P>
|
|
<P>If you used .BAT files to create shortcuts of long command lines (I did a
|
|
lot), this goal can be attained by inserting appropriate alias lines (see
|
|
example above) in <CODE>profile</CODE> or <CODE>.bash_profile</CODE>. But if your
|
|
.BATs were more complicated, then you'll love the scripting language made
|
|
available by the shell: it's as powerful as good ol' QBasic, if not more. It
|
|
has variables, structures like while, for, case, if... then... else, and
|
|
lots of other features: it can be a good alternative to a ``real''
|
|
programming language.
|
|
<P>To write a script---the equivalent of a .BAT file under DOS---all you have
|
|
to do is write a standard ASCII file containing the instructions, save it,
|
|
then make it executable with the command <CODE>chmod +x <scriptfile></CODE>.
|
|
To execute it, type its name.
|
|
<P>A word of warning. The system editor is called <CODE>vi</CODE>, and in my
|
|
experience most new users find it very difficult to use. I'm not going to
|
|
explain how to use it; please consult Matt Welsh's book or search for a
|
|
tutorial on the net. Suffice it here to say that:
|
|
<P>
|
|
<UL>
|
|
<LI> to insert some text, type <CODE>i</CODE> then your text;</LI>
|
|
<LI> to delete characters, type <ESC> then <CODE>x</CODE>;</LI>
|
|
<LI> to quit <CODE>vi</CODE> whithout saving, type <ESC> then <CODE>:q!</CODE></LI>
|
|
<LI> to save and quit, type <ESC> then <CODE>:wq</CODE>.</LI>
|
|
</UL>
|
|
<P>A good beginner editor is <CODE>joe</CODE>: invoking it by typing <CODE>jstar</CODE>
|
|
you'll get the same key bindings as the DOS/Win editor. <CODE>jed</CODE> in
|
|
WordStar or IDE mode is even better. Please consult Section
|
|
<A HREF="DOS-Win-to-Linux-HOWTO-11.html#Programs">Where to Find Applications</A> to see where to get these
|
|
editors.
|
|
<P>Writing scripts under <CODE>bash</CODE> 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 an example of shell script, from which you can extract some basic
|
|
rules:
|
|
<P>
|
|
<HR>
|
|
<PRE>
|
|
#!/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 "The 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` # 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>
|
|
<HR>
|
|
<P>
|
|
<P>
|
|
<H2><A NAME="ss10.2">10.2 C for Yourself</A>
|
|
</H2>
|
|
|
|
<P>
|
|
<P>Under UNIX, the system language is C, love it or hate it. Scores of other
|
|
languages (Java, FORTRAN, Pascal, Lisp, Basic, Perl, awk...) are also
|
|
available.
|
|
<P>Taken for granted that you know C, here are a couple of guidelines for those
|
|
of you who have been spoilt by Turbo C++ or one of its DOS kin. Linux's C
|
|
compiler is called <CODE>gcc</CODE> and lacks all the bells and whistles that
|
|
usually accompany its DOS counterparts: no IDE, on-line help, integrated
|
|
debugger, etc. It's just a rough command-line compiler, very powerful and
|
|
efficient. To compile your standard <CODE>hello.c</CODE> you'll do:
|
|
<P>
|
|
<BLOCKQUOTE><CODE>
|
|
<PRE>
|
|
$ gcc hello.c
|
|
</PRE>
|
|
</CODE></BLOCKQUOTE>
|
|
<P>which will create an executable file called <CODE>a.out</CODE>. To give the
|
|
executable a different name, do
|
|
<P>
|
|
<BLOCKQUOTE><CODE>
|
|
<PRE>
|
|
$ gcc -o hola hello.c
|
|
</PRE>
|
|
</CODE></BLOCKQUOTE>
|
|
<P>To link a library against a program, add the switch -l<libname>. For
|
|
example, to link in the math library:
|
|
<P>
|
|
<BLOCKQUOTE><CODE>
|
|
<PRE>
|
|
$ gcc -o mathprog mathprog.c -lm
|
|
</PRE>
|
|
</CODE></BLOCKQUOTE>
|
|
<P>(The <CODE>-l<libname></CODE> switch forces <CODE>gcc</CODE> to link the
|
|
library <CODE>/usr/lib/lib<libname>.so</CODE>; so <CODE>-lm</CODE> links
|
|
<CODE>/usr/lib/libm.so</CODE>).
|
|
<P>So far, so good. But when your prog is made of several source files, you'll
|
|
need to use the utility <CODE>make</CODE>. Let's suppose you have written an
|
|
expression parser: its source file is called <CODE>parser.c</CODE> and
|
|
#includes two header files, <CODE>parser.h</CODE> and <CODE>xy.h</CODE>. Then
|
|
you want to use the routines in <CODE>parser.c</CODE> in a program, say,
|
|
<CODE>calc.c</CODE>, which in turn #includes <CODE>parser.h</CODE>. What a mess!
|
|
What do you have to do to compile <CODE>calc.c</CODE>?
|
|
<P>You'll have to write a so-called <CODE>Makefile</CODE>, which teaches the
|
|
compiler the dependencies between sources and objects files. In our example:
|
|
<P>
|
|
<HR>
|
|
<PRE>
|
|
# This is Makefile, used to compile calc.c
|
|
# Press the <TAB> key where indicated!
|
|
|
|
calc: calc.o parser.o
|
|
<TAB>gcc -o calc calc.o parser.o -lm
|
|
# calc depends on two object files: calc.o and parser.o
|
|
|
|
calc.o: calc.c parser.h
|
|
<TAB>gcc -c calc.c
|
|
# calc.o depends on two source files
|
|
|
|
parser.o: parser.c parser.h xy.h
|
|
<TAB>gcc -c parser.c
|
|
# parser.o depends on three source files
|
|
|
|
# end of Makefile.
|
|
</PRE>
|
|
<HR>
|
|
<P>Save this file as <CODE>Makefile</CODE> and type <CODE>make</CODE> to compile your
|
|
program; alternatively, save it as <CODE>calc.mak</CODE> and type <CODE>make -f
|
|
calc.mak</CODE>, and of course RMP. You can invoke some help about the C
|
|
functions, that are covered by man pages, section 3; for example,
|
|
<P>
|
|
<BLOCKQUOTE><CODE>
|
|
<PRE>
|
|
$ man 3 printf
|
|
</PRE>
|
|
</CODE></BLOCKQUOTE>
|
|
<P>To debug your programs, use <CODE>gdb</CODE>. <CODE>info gdb</CODE> to learn how to
|
|
use it.
|
|
<P>There are lots of libraries available; among the first you may want to use
|
|
are <CODE>ncurses</CODE> (textmode effects), and <CODE>svgalib</CODE> (console
|
|
graphics). Many editors can act as an IDE; <CODE>emacs</CODE> and <CODE>jed</CODE>,
|
|
for instance, also feature syntax highlighting, automatic indent, and so on.
|
|
Alternatively, get the package <CODE>rhide</CODE> from
|
|
<A HREF="ftp://metalab.unc.edu:/pub/Linux/devel/debuggers/">ftp://metalab.unc.edu:/pub/Linux/devel/debuggers/</A>. It's a Borland IDE
|
|
clone, and chances are that you'll like it.
|
|
<P>
|
|
<P>
|
|
<H2><A NAME="ss10.3">10.3 X11 Programming</A>
|
|
</H2>
|
|
|
|
<P>
|
|
<P>If you feel brave enough to tackle X11 programming (it's not that
|
|
difficult), there are several libraries that make writing X11 programs a
|
|
breeze. The main sites to visit are those of GTK+,
|
|
<A HREF="http://www.gtk.org">http://www.gtk.org</A>, and Qt,
|
|
<A HREF="http://www.troll.no">http://www.troll.no</A>. Gtk+ is
|
|
a C-based widget set originally written for the graphic package The GIMP
|
|
(
|
|
<A HREF="http://www.gimp.org">http://www.gimp.org</A>), and is used by the Gnome environment.
|
|
Kdeveloper is based on C++-based Qt, used by KDE. Most likely, you'll use
|
|
one of these.
|
|
<P>Some of the best tools for visual programming are Kdevelop for Qt,
|
|
<A HREF="http://www.kdevelop.org">http://www.kdevelop.org</A>, and Glade for GTK+,
|
|
<A HREF="http://glade.pn.org">http://glade.pn.org</A>. This page has more information:
|
|
<A HREF="http://www.free-soft.org/guitool/">http://www.free-soft.org/guitool/</A>.
|
|
<P>
|
|
<P>
|
|
<H3>Multi-Platform Programming</H3>
|
|
|
|
<P>
|
|
<P>Wouldn't it be nice if you could write code that compiled seamlessly under
|
|
Linux <EM>and</EM> Windows using <CODE>gcc</CODE>? As of this writing, there are
|
|
some widget sets that allow for more-or-less stable multi-platform
|
|
programming. As far as stability and completeness are concerned though, I
|
|
would say that the choice is narrowed down to only one: FLTK, the Fast Light
|
|
Tool Kit
|
|
<A HREF="http://www.fltk.org">http://www.fltk.org</A>. It's amazingly small, quick, and
|
|
stable. It also has a semi-visual builder called Fluid.
|
|
<P>
|
|
<P>
|
|
<HR>
|
|
<A HREF="DOS-Win-to-Linux-HOWTO-11.html">Next</A>
|
|
<A HREF="DOS-Win-to-Linux-HOWTO-9.html">Previous</A>
|
|
<A HREF="DOS-Win-to-Linux-HOWTO.html#toc10">Contents</A>
|
|
</BODY>
|
|
</HTML>
|