Using the GNU Compiler Collection


The GNU C Compiler (GCC), first written by Richard Stallman in 1987, has grown over time to support six other languages: Ada, C++, Fortran, Java, Objective-C, and Objective-C++. This is why the official name changed to the GNU Compiler Collection. Version 4.0 came out in April 2005.

Open a shell, change to the directory you saved your program to, and compile your program:

gcc -o hello hello.c 

Tip

Your editor may have a direct link or command to run the compiler, so you can test your program without leaving the editor. Consult the editor's documentation.


Tip

The GCC determines which compiler to use based on the file extension of your source code. Table 28.1 shows common extensions for the supported languages. You can override this behavior by using the -x option (see man 1 gcc for more information).

Table 28.1. GCC Expected File Extensions for Source Codes

Language

Extension

Ada

Source: .ads, .adb

C

Source: .c

 

Header: .h

C++

Source: .C, .cc, .cp, .CPP, .cpp, .c++, .cxx

 

Header: .H, .hh

Fortran

Source: .F, .FPP, .fpp, .r

Objective-C

Source: .m

 

Header: .h

Objective-C++

Source: .M

 

Header: .h



The compiler creates an executable program called hello from the source file, hello.c. Heed any warnings you may get from the compiler. If you get any, you've mistyped something or forgot to press Enter (new line) at the end of the code. If necessary, compile again until you get a fresh prompt with no feedback. GCC delivers only bad news; if things work, it just does its thing.

If you look in the directory you will see a new file called hello with no extension. Now you can run the program:

./hello Hello, World. 

What did you do? You saved the source code with a .c extension to note what language this code is written in. The compiler created the executable hello (the name specified by the o option), and then you executed the program. Because the program is not located in one of the standard executable directories, usr/bin or /usr/local/bin, you must use the dot-slash "execute" command in the directory where the program is located.

Additional GCC language front ends exist for Pascal, Modula-2, Modula-3, Mercury, VHDL, and PL/I; these are developed by teams separate from the GCC team and are not available via YaST. The Objective-C++ component is also missing from SUSE. You can access these languages at http://gcc.gnu.org/frontends.html.



SUSE Linux 10 Unleashed
SUSE Linux 10.0 Unleashed
ISBN: 0672327260
EAN: 2147483647
Year: 2003
Pages: 332

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net