首页 > C/C++语言 > C/C++基本语法 > [C Language]About pragma
2006
01-09

[C Language]About pragma

Title    : About pragma
Author: Peter Van Der Linden,<Expert C Programming Deep C Secrets>

Some Light Relief—The Implementation-Defined Effects of Pragmas . . .

The Free Software Foundation is a unique organization founded by ace MIT hacker Richard Stallman.By the way, we use “hacker” in the old benevolent sense of “gifted programmer;” the term has been debased by the media, so outsiders use it to mean “evil genius.” Like the adjective bad, “hacker” now has two opposing meanings, and you have to figure it out from the context.

Stallman’s Free Software Foundation was founded on the philosophy that software should be free and freely available to all. FSF’s charter is “to eliminate restrictions on copying, redistribution, understanding and modification of computer programs” and their ambition is to create a publicdomain implementation of UNIX called GNU (it stands for “GNU’s Not UNIX.” Yes, really.)

Many computer science graduate students and others agree with the GNU philosophy, and have worked on software products that FSF packages and distributes for free. This pool of skilled labor donating their talent has resulted in some good software. One of FSF’s best products is the GNU C compiler family. gcc is a robust, aggressive optimizing compiler, available for many hardware platforms and sometimes better than the manufacturer’s compiler. Using gcc would not be appropriate for all projects; there are questions of maintenance and future product continuity. There are other tools needed besides a compiler, and the GNU debugger was unable to operate on shared libraries for a long time. GNU C has also occasionally been a little, shall we say, giddy in development.

When the ANSI C standard was under development, the pragma directive was introduced.Borrowed from Ada, #pragma is used to convey hints to the compiler, such as the desire to expand a particular function in-line or suppress range checks. Not previously seen in C, pragma met with some initial resistance from a gcc implementor, who took the “implementation-defined” effect very literally—in gcc version 1.34, the use of pragma causes the compiler to stop compiling and launch a
computer game instead! The gcc manual contained the following:

The “#pragma” command is specified in the ANSI standard to have an arbitrary implementationdefined effect. In the GNU C preprocessor, “#pragma” first attempts to run the game “rogue”; if that fails, it tries to run the game “hack”; if that fails, it tries to run GNU Emacs displaying the Tower of Hanoi; if that fails, it reports a fatal error. In any case, preprocessing does not continue.
                                                                                    —Manual for version 1.34 of the GNU C compiler

And the corresponding source code in the preprocessor part of the compiler was:

/*
 * the behavior of the #pragma directive is implementation defined.
 * this implementation defines it as follows.
 */

do_pragma ()
{
    close (0);
    if ( open( “/dev/tty”, O_RDONLY, 0666 ) != 0 )
        goto nope;
    close (1);
    if ( open( “/dev/tty”, O_WRONLY, 0666 ) != 1 )
        goto nope;
    execl( “/usr/games/hack”, “#pragma”, 0 );
    execl( “/usr/games/rogue”, “#pragma”, 0 );
    execl( “/usr/new/emacs”, “-f”, “hanoi”, “9″, “-kill”, 0 );
    execl( “/usr/local/emacs”, “-f”, “hanoi”, “9″, “-kill”, 0 );
    nope:
    fatal( “You are in a maze of twisty compiler features, all different” );
}

Especially droll is the fact that the description in the user manual is wrong, in that the code shows that “hack” is tried before “rogue”.


留下一个回复