One of the assessment questions in Chapter 1, "Porting Project Considerations," asks what programming language the application to be ported is written in. The question tries to ascertain whether the application can be compiled by any of the supported compilers on Linux. The most popular compiler in Linux and in the open-source community today is the GNU Compiler Collection (GCC). Initially, when the open-source movement started, the term GCC referred to the GNU C Compiler (gcc). Today, because of its overwhelming popularity, GCC is now an integrated distribution of compilers that include several major programming languages, including C, C++, FORTRAN, COBOL, and Java.
2.1.1. GNU gcc C Compiler
gcc supports the ANSI C version of the C standard, although support for the most recent version is not yet complete. These standards are as follows:
gcc has a number of compiler flags to allow the user to select the desired standard. If no standard is specified, the default is -std=gnu89; this will change to -std=gnu99 in some future release when the C99 support is complete.
Most of the compiler support routines used by GCC are present in libgcc, with the exception of the freestanding environment. When the -ffreestanding compiler flag is turned on, users are required to provide their own version of memcpy, memove, memset, and memcmp.
For any given input file, the filename suffix determines what kind of compilation is done:
The command-line options of the GCC family of compilers differ from other proprietary compilers. Many options for GCC compilers have multiletter names; therefore, multiple single-letter options may not be grouped: -dr is different from -d-r. In addition, many options have long names starting with -f or -W (for example, -fforce-mem, -fstrength-reduce, -Wformat, and so on). Most of these have both positive and negative forms; the negative form of -ffoo would be -fno-foo.
More than 100 command-line options can be passed to gcc. More than likely, you will use only a few. The following is a typical compile line for gcc compiler invocation:
gcc <additional flags here > -c main.c o main.o
Here are some examples of gcc options controlling the kind of output it produces:
-c compile or assemble source files but do not link. -S output is in the form of assembler code. -E output is in the form of preprocessed source code. -v print the commands executed to run the stages of compilation. --help print the description of command-line options.
2.1.2. GNU g++ (C++ ) Compiler
Starting with GNU g++ version 3.4.0, GNU g++ claims to be much closer to the ISO/ANSI C++ standards. GNU g++ ships with the standard libstdc++. C++ source files conventionally use one of these suffixes: .C, .cc, .cpp, .c++, .cp, or .cxx; preprocessed C++ files use the suffix .ii. GCC recognizes files with these names and compiles them as C++ programs even if you call the compiler the same way as for compiling C programs (usually with the name gcc).
However, C++ programs often require class libraries as well as a compiler that understands the C++ language. Under some circumstances, you might want to compile programs from standard input, or otherwise without a suffix that flags them as C++ programs. g++ is a program that calls GCC with the default language set to C++, and automatically specifies linking against the C++ library. On many systems, the executable g++ is also installed with the name c++.
When you compile C++ programs, you may specify many of the same command-line options that you use for compiling programs in any language, or command-line options meaningful for C and related languages, or options that are meaningful only for C++ programs.
You can use most of the GNU compiler options when using the g++ compiler, but some options are meaningful only for C++ programs. Table 2-1 shows a few useful g++ options.
You will use the following command to compile a typical C++ program under GNU:
g++ <additional options here> -c firstClass.C o firstClass.o
2.1.3. GNU g77 FORTRAN
GNU FORTRAN consists of a compiler, runtime libraries, and debugger support. It supports ANSI FORTRAN 77 conformance, plus popular extensions to FORTRAN, including some ANSI/ISO FORTRAN 90 features.
The libf2c library is distributed with GNU FORTRAN for the convenience of its users but is not part of GNU FORTRAN. It contains the procedures needed by FORTRAN programs while they are running.
GNU FORTRAN, or g77, is designed initially as a free replacement for, or alternative to, the UNIX f77 command and was designed to fit in well with the other GNU compilers and tools. For example, in a GNU FORTRAN installation, gcc recognizes FORTRAN source files by name, just like it does with C and C++ source files.. It knows to use the FORTRAN compiler named f771, instead of cc1 or cc1plus, to compile FORTRAN files. The non-FORTRAN-related operation of gcc is generally unaffected by installing the GNU FORTRAN version of gcc. However, without the installed version of gcc being the GNU FORTRAN version, gcc will not be able to compile and link FORTRAN programsand because g77 uses gcc to do most of the actual work, neither will g77!
Because the g77 command is essentially just a front end for the gcc command, FORTRAN users will normally use g77 rather than gcc, because g77 knows how to specify the libraries needed to link with FORTRAN programs (libf2c and lm). g77 can still compile and link programs and source files written in other languages, just like gcc.
As of the writing of this book, an effort is underway to replace FORTRAN 77, g77, with GNU FORTRAN (gfortran) or FORTRAN 95. Although gfortran is available for download and use, it is still considered to be in (public) beta testing, and has been since February 17, 1995. GNU recommends you continue to use g77 for all new development until they officially release gfortran.
2.1.4. GNU Compiler for Java
GCJ is a portable, optimizing, ahead-of-time compiler for the Java Programming Language. It can compile the following:
Applications are compiled and linked with the GCJ runtime, libgcj. Included within libgcj are the core class libraries, a garbage collector, and a bytecode interpreter. One feature of libgcj is that it can dynamically load and interpret class files, resulting in a Java application that is a mix of compiled and interpreted objects or classes.
Debugging support for GCJ applications is available using recent versions of the GNU debugger, GDB. A short tutorial on using GDB to debug GCJ-compiled applications is available at http://gcc.gnu.org/java/gdb.html.
2.1.5. GNU Cobol
In 1999, a GNU project named GCC for COBOL was started to create a COBOL 85-compliant compiler for GNU/Linux and other operating systems. At the time of this writing, the compiler was not yet ready for production use. For more information about the GNU COBOL project, refer to www.gnu.org/software/cobol/cobolforgcc.html.
Other open-source and commercially available COBOL compilers support Linux. One open-source COBOL compiler is called TinyCOBOL. For commercially available COBOL compilers, there are Acucobol-GT from Acucorp (www.acucorp.com) and Micro Focus Object Cobol Developer Suite from Micro Focus (www.microfocus.com).
2.1.6. Compilers from Other Sources
Several other compilers for Linux are available from different vendors for C++, FORTRAN, COBOL, and Java. Intel offers compilers for C++ and FORTRAN. IBM offers the XL C/C++ compilers for Linux running on POWER. Micro Focus offers a COBOL compiler for Linux.
The porting chapters for Solaris, AIX, and HP-UX cover more of the differences between the respective native platform compilers and the GNU C and C++ compilers.