Advanced Linux Programming - Chapter 01-advanced-unix-programming-with-linux.pdf

(230 KB) Pobierz
../alp/advanced-linux-programming.pdf (2)
I
Advanced UNIX Programming
with Linux
1 Getting Started
2 Writing Good GNU/Linux Software
3 Processes
4 Threads
5 Interprocess Communication
3444835.003.png
3444835.004.png
1
Getting Started
T HIS CHAPTER SHOWS YOU HOW TO PERFORM THE BASIC steps required to create a
C or C++ Linux program. In particular, this chapter shows you how to create and
modify C and C++ source code, compile that code, and debug the result. If you’re
already accustomed to programming under Linux, you can skip ahead to Chapter 2,
“Writing Good GNU/Linux Software;” pay careful attention to Section 2.3, “Writing
and Using Libraries,” for information about static versus dynamic linking that you
might not already know.
Throughout this book, we’ll assume that you’re familiar with the C or C++ pro-
gramming languages and the most common functions in the standard C library.The
source code examples in this book are in C, except when demonstrating a particular
feature or complication of C++ programming.We also assume that you know how to
perform basic operations in the Linux command shell, such as creating directories and
copying files. Because many Linux programmers got started programming in the
Windows environment, we’ll occasionally point out similarities and contrasts between
Windows and Linux.
3444835.005.png 3444835.006.png
4
Chapter 1 Getting Started
1.1 Editing with Emacs
An editor is the program that you use to edit source code. Lots of different editors are
available for Linux, but the most popular and full-featured editor is probably GNU
Emacs.
About Emacs
Emacs is much more than an editor. It is an incredibly powerful program, so much so that at
CodeSourcery, it is affectionately known as the One True Program, or just the OTP for short. You can read
and send email from within Emacs, and you can customize and extend Emacs in ways far too numerous
to discuss here. You can even browse the Web from within Emacs!
If you’re familiar with another editor, you can certainly use it instead. Nothing in the
rest of this book depends on using Emacs. If you don’t already have a favorite Linux
editor, then you should follow along with the mini-tutorial given here.
If you like Emacs and want to learn about its advanced features, you might consider
reading one of the many Emacs books available. One excellent tutorial, Learning
GNU Emacs , is written by Debra Cameron, Bill Rosenblatt, and Eric S. Raymond
(O’Reilly, 1996).
1.1.1 Opening a C or C++ Source File
You can start Emacs by typing emacs in your terminal window and pressing the
Return key.When Emacs has been started, you can use the menus at the top to create
a new source file. Click the Files menu, choose Open Files, and then type the name of
the file that you want to open in the “minibuffer” at the bottom of the screen. 1 If you
want to create a C source file, use a filename that ends in .c or .h . If you want to
create a C++ source file, use a filename that ends in .cpp , .hpp , .cxx , .hxx , .C , or .H .
When the file is open, you can type as you would in any ordinary word-processing
program.To save the file, choose the Save Buffer entry on the Files menu.When
you’re finished using Emacs, you can choose the Exit Emacs option on the Files
menu.
If you don’t like to point and click, you can use keyboard shortcuts to automatically
open files, save files, and exit Emacs.To open a file, type C-x C-f . (The C-x means to
hold down the Control key and then press the x key.) To save a file, type C-x C-s .To
exit Emacs, just type C-x C-c . If you want to get a little better acquainted with Emacs,
choose the Emacs Tutorial entry on the Help menu.The tutorial provides you with
lots of tips on how to use Emacs effectively.
1. If you’re not running in an X Window system, you’ll have to press F10 to access the
menus.
3444835.001.png
1.1 Editing with Emacs
5
1.1.2 Automatic Formatting
If you’re accustomed to programming in an Integrated Development Environment (IDE) ,
you’ll also be accustomed to having the editor help you format your code. Emacs can
provide the same kind of functionality. If you open a C or C++ source file, Emacs
automatically figures out that the file contains source code, not just ordinary text. If
you hit the Tab key on a blank line, Emacs moves the cursor to an appropriately
indented point. If you hit the Tab key on a line that already contains some text, Emacs
indents the text. So, for example, suppose that you have typed in the following:
int main ()
{
printf (“Hello, world\n”);
}
If you press the Tab key on the line with the call to printf , Emacs will reformat your
code to look like this:
int main ()
{
printf (“Hello, world\n”);
}
Notice how the line has been appropriately indented.
As you use Emacs more, you’ll see how it can help you perform all kinds of
complicated formatting tasks. If you’re ambitious, you can program Emacs to perform
literally any kind of automatic formatting you can imagine. People have used this
facility to implement Emacs modes for editing just about every kind of document,
to implement games 2 , and to implement database front ends.
1.1.3 Syntax Highlighting
In addition to formatting your code, Emacs can make it easier to read C and C++
code by coloring different syntax elements. For example, Emacs can turn keywords
one color, built-in types such as int another color, and comments another color.
Using color makes it a lot easier to spot some common syntax errors.
The easiest way to turn on colorization is to edit the file ~/.emacs and insert the
following string:
(global-font-lock-mode t)
Save the file, exit Emacs, and restart. Now open a C or C++ source file and enjoy!
You might have noticed that the string you inserted into your .emacs looks like
code from the LISP programming language.That’s because it is LISP code! Much of
Emacs is actually written in LISP. You can add functionality to Emacs by writing more
LISP code.
2.Try running the command M-x dunnet if you want to play an old-fashioned text
adventure game.
3444835.002.png
Zgłoś jeśli naruszono regulamin