Section 2.1. Compilers

2.1. Compilers

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[2] 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.[3]

[2] One of the questions we are often asked is this: Does Linux support the RPG language? Unfortunately, the answer is no. The best solution today is to convert RPG applications to Java applications.

[3] GCC also includes support for Objective C and Ada.

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:

  • ANSI C standard (1990 ISO C and C94/C95)

  • C99 ISO C standard (support not complete)[4].

    [4] GCC has incomplete support for C99. See

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:

  • file.c

    C source code that must be preprocessed

  • file.i

    C source code that should not be preprocessed

  • file.m

    Objective-C source code (note that you must link with the library libobjc.a to make an Objective-C program work)

  • file.mi

    Objective-C source code that should not be preprocessed

  • file.h

    C header file (not to be directly compiled or linked)

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.[5] 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.

Table 2-1. Sample g++ Options

GNU g++ Options



Check that the pointer returned by operator new is non-null before attempting to modify the storage allocated.


The C++ standard allows an implementation to omit creating a temporary object that is used only to initialize another object of the same type. Specifying this option disables that optimization and forces g++ to call the copy constructor in all cases.


If -ffor-scope is specified, the scope of variables declared in a for-init-statement is limited to the for loop itself, as specified by the C++ standard. If -fno-for-scope is specified, the scope of variables declared in a for-init-statement extends to the end of the enclosing scope, as was the case in old versions of g++ and other (traditional) implementations of C++. The default if neither flag is given is to follow the standard, but to allow and give a warning for old-style code that would otherwise be invalid or have different behavior.


Disable generation of information about every class with virtual functions for use by the C++ runtime type identifier features (dynamic_cast and typeid). If you do not use those parts of the language, you can save some space by using this flag. Note that exception handling uses the same information, but it generates it as needed.

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.[6]. 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!

[6] .f, .for, and .FOR are file extensions that are recognized as FORTRAN source and must not be preprocessed. .F, .fpp, and .FPP are file extensions that are recognized as FORTRAN source and must be preprocessed

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[7] 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[8]


GCJ is a portable, optimizing, ahead-of-time compiler for the Java Programming Language. It can compile the following:

  • Java source code directly to native machine code

  • Java source code to Java bytecode (class files)

  • Java bytecode to native machine code

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.[9] A short tutorial on using GDB to debug GCJ-compiled applications is available at

[9] The latest version as of this writing is version 6.3.

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

Other open-source and commercially available COBOL compilers support Linux. One open-source COBOL compiler is called TinyCOBOL.[10] For commercially available COBOL compilers, there are Acucobol-GT from Acucorp ( and Micro Focus Object Cobol Developer Suite from Micro Focus (


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.[11] IBM offers the XL C/C++ compilers for Linux running on POWER.[12] Micro Focus offers a COBOL compiler for Linux.[13]




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.

UNIX to Linux Porting. A Comprehensive Reference
UNIX to Linux Porting: A Comprehensive Reference
ISBN: 0131871099
EAN: 2147483647
Year: 2004
Pages: 175 © 2008-2017.
If you may any questions please contact us: