Learn from IBM experts about using the GCC compiler on AIX®. The authors explain why you should use GCC compiler, which compiler options are specific to pSeries, what you need to know about shared libraries, and common gotchas and solutions.[Updated "Installing GCC on AIX" section with new product levels and links.]

Anita Govindjee (agovindj@us.ibm.com), IBM Systems and Technology Group, IBM

Anita Govindjee is a Solutions Engagement Manager in the IBM ISV Solutions Enablement group. Anita has more than ten years' experience working on UNIX platforms, including AIX, Linux, Solaris, and HPUX, doing software development in C, C++, and Java. She holds a B.S. degree from the University of Illinois, Urbana-Champaign, and an M.S. degree from Stanford University, both in Computer Science. You can reach Anita at agovindj@us.ibm.com.



David Edelsohn (edelsohn@us.ibm.com), IBM Research Group, IBM

David Edelsohn works in the IBM Research Division on operating systems and compilers, including PowerPC support for GCC. David has worked with Unix platforms for over fifteen years, developing Open Source software on AIX, Linux, BSD, Solaris, and Ultrix. David holds an A.B. degree from the University of California, Berkeley, a M.Sc. from the California Institute of Technology, and a Ph.D. from Syracuse University, all in Physics. You can reach David at edelsohn@us.ibm.com.



Ramesh Chitor (ramesh.chitor@gmail.com), IBM Systems and Technology Group, IBM

Ramesh Chitor works as a Solutions Relationship Manager in the IBM Systems ISV Enablement organization within IBM Systems and Technology Group. Ramesh has primary responsibilities in ISV applications, tools, Software Group initiatives, databases, and other middleware support for solution providers.



12 August 2008 (First published 12 January 2005)

Also available in Russian

GCC Basics

The GCC compiler is a part of the Free Software Foundation's GNU Project. GCC is developed through an open source environment, as are all the GNU tools, and it supports many platforms including AIX. GCC stands for GNU Compiler Collection, and supports many languages such as C, C++, Objective C, Fortran, and Java. In this article, we will be discussing GCC versions 3.3.4 and all versions of 3.4 on AIX versions 5.1, 5.2 and 5.3.

Comparing GCC compiler to XL C/C++ compiler

XL C/C++ Enterprise Edition V7.0 for AIX is the follow-on release to VisualAge® C++ Professional V6.0 for AIX. In this section, we describe why you may want to use the GCC compiler on AIX versus using the XL C/C++ Compiler. The XL compiler has great support for the latest international and industry programming language standards. The XL C/C++ compiler comes with new built-in functions for floating-point division, new pragmas and additional new compiler options. A key feature of IBM XL C/C++ Advanced Edition V7.0 for AIX is further compatibility with GCC. In order to help with porting of code that was originally written for GCC, a subset of features related to GNU C/C++ is supported by XL C/C++. Importantly, the XL C/C++ compiler is optimized for PowerPC systems, including POWER5.

But there are some situations when using the GCC compiler might be more appealing. When applications are written using standard APIs and compiled using GNU gcc or g++ compilers porting them across multiple platforms is much easier to accomplish. GCC has multiple language front-ends facilitating parsing multiple languages. GCC is a portable compiler and runs on most platforms available today, and supports most commercial 64-bit CPUs. GCC is a native compiler which can also cross-compile any program, producing executable files for a different system from the one used by GCC itself. This allows software to be compiled for embedded systems which are not capable of running a compiler. GCC is written in C with a strong focus on portability, and can compile itself, so it can be adapted to new systems easily.Finally GCC gives you freedom to enhance existing GCC and other GNU software developed by others.

As developers try to support their applications on many platforms, using different compilers on different platforms can cause numerous headaches. If, instead, you use the GCC compiler, which is supported on a great variety of platforms, it reduces the headaches in porting applications from one platform to another. GCC can make your job easier by providing a common compiler environment across platforms. g++ is now closer to full conformance to the ISO/ANSI C++ standard.

Please note that the default AIX operating system make does not understand the GCC make files. If you are attempting to load GCC compiler on AIX, you need to have GNU make file. You can print make -v to see what you need to build on GCC.

AIX 5L Binary Compatibility

IBM provides binary compatibility amongst AIX 5.1, AIX 5.2 and AIX 5.3 versions of the operating system. Therefore applications running on AIX 5.1 or on AIX 5.2 will run on 5.3 as-is if they follow the criteria listed in the IBM’s AIX 5L binary compatibility statement at http://www-1.ibm.com/servers/aix/os/compatibility. With that said, ISVs vary wildly on the processes they use to add support of a new OS release level. Many of the top ISVs run their applications through some form of testing. In most cases they will run them through a subset of their final testing procedures prior to adding support. Many other ISVs, however, review our binary compatibility details and add support based on their applications compliance with our statement. In either case, there is no need to recompile the application to get them to AIX 5.3.

Installing GCC on AIX

Installing GCC on AIX is easy if you have the binaries available for the version you want to install on your AIX system. The binaries can be ftp'd to your AIX system from the following sites, as indicated in the Resources section:

  • Bull AIX Freeware
  • Hudson Valley Community College Open Source Software
  • AIX 5L and 6 Open Source packages
  • IBM AIX Toolbox for Linux applications

After downloading the appropriate binary format file, chmod the downloaded file to executable mode, and then run SMIT to install. Support for AIX 5.2 was added in GCC version 3.3.

The IBM AIX Toolbox for Linux applications web site has GCC 3.3.2 binaries available as this time for AIX 5.1 and 5.2. The BULL Freeware site also has GCC version 3.3.2 binaries available for AIX 5.2.

If you require a newer GCC version, you need to first install an available GCC binary on your system. Once you have GCC installed, you can use it to compile the newer GCC version on your AIX system. See Resources to find the complete set of GCC installation instructions and Host/Target-specific installation notes for GCC. It is important that you follow the platform-specific instructions carefully. Read through the instructions carefully before starting the build and install process.

You might find GCC build reports useful to ensure you successfully installed GCC 4.3.x (see Resources). At this time, there are build reports for AIX 5.3.

Make sure that you install a version of GCC that corresponds to the AIX release installed on the system. GCC installs private copies of some header files that must integrate properly with AIX system header files for GCC to function correctly, otherwise running the compiler can produce error messages about header files. The header files should be rebuilt if they do not match. You can delete the header file cache to build a new release of GCC from sources with an old version, but GCC should not be operated without the header file cache.


GCC compiler options

There's a wide variety of compiler options available on GCC, ranging from optimizing code, setting or suppressing ISO/ANSI standard code compilation, to debugging options, template code compilation options. GCC also provides some compiler options specific to the pSeries (formerly known as RS/6000) and PowerPC platforms.

A full description of the options available for POWER and PowerPC targets can be found at http://gcc.gnu.org/onlinedocs/gcc-3.4.3/gcc/RS_002f6000-and-PowerPC-Options.html#RS_002f6000-and-PowerPC-Options. Here we will go through a few of the options that we consider to be most relevant.

Of the options that set the processor type, such as -mcpu and -mtune, it is best to use the defaults that GCC provides. On AIX 4.3 and AIX 5.1, the default is -mcpu=common -- this code will work on all members of the pSeries (RS/6000) processor families. On AIX 5.2 and higher, the default is -mcpu=powerpc -- this code will work on pSeries PowerPC processors. GCC assumes a recent processor type associated with the AIX release as a processor model for scheduling purposes.

You should use -mcpu=power, -mcpu=power2, -mcpu=powerpc, -mcpu=power4, etc. to optimize for a specific processor or class of processors. Do not use the -mpower, -mpowerpc, -mpower2, or -mpowerpc64 options. Although they are available, they are not intended for end users. Using -mpower2 or other options in isolation may cause unpredictable results because the compiler may not be in a self-consistent state for that target processor.

To compile an application in 64 bit mode, one should use the option -maix64, which enables the 64-bit AIX ABI such as: 64-bit pointers, 64-bit long type, and the infrastructure needed to support them. Specifying -maix64 implies -mpowerpc64 and -mpowerpc, while -maix32 disables the 64-bit ABI and implies -mno-powerpc64. GCC defaults to -maix32.

If you get a linker error message saying you have overflowed the available TOC (Table of Contents) space, you can reduce the amount of TOC space used by using -mminimal-toc. By default GCC uses -mfull-toc which allocates at least one TOC entry for each unique non-automatic variable reference in your program. GCC also places floating-point constants in the TOC. However, only 16,384 entries are available in the TOC, and it's possible to overflow the TOC. By specifying -mminimal-toc GCC makes only one TOC entry for every function.

-pthread compiles and links your application code with the POSIX threads library, just as you would do in VisualAge (or XL) C/C++. With VisualAge (or XL) C/C++, you can also compile and link using xlc_r or xlC_r to get thread support, but this option doesn't exist with GCC. Threaded code should therefore be linked with -pthread for GCC. The -pthread option sets flags for both the preprocessor and linker.

The -g option generates debugging information, defaulting to -gxcoff+ at level 2. The debugging information may contain some extensions intended for the GNU Debugger (GDB). To produce debugging information more compatible with AIX dbx, one may use the -gxcoff option.

GCC for AIX does not support -msoft-float fully. It will turn off use of floating point registers within GCC, but GCC support libraries and AIX libraries will continue to use floating point registers.

-mlittle-endian and -mcall-linux are not valid in the AIX configuration. The documentation mentions all options for all GCC "rs6000" targets. Not all options are available in each configuration.

To pass options to the native AIX linker directly, use -Wl, <linker option>.

G++ and GCC Compiler Options

A description of g++-specific compiler options can be found at http://gcc.gnu.org/onlinedocs/gcc-3.4.3/gcc/C_002b_002b-Dialect-Options.html#C_002b_002b-Dialect-Options. These options are the same on AIX.

Similarly, a full description of gcc-specific compiler options can be found at http://gcc.gnu.org/onlinedocs/gcc-3.4.3/gcc/C-Dialect-Options.html#C%20Dialect%20Options. These options are also the same on AIX as other UNIX systems.

The list of optimization options can be found at http://gcc.gnu.org/onlinedocs/gcc-3.4.3/gcc/Optimize-Options.html#Optimize-Options.

The most-common optimization option is -O2, which enables a number of optimizations to enhance performance of the resulting executable or library. -O3 enables more optimizations that can improve performance at the expense of additional compile time. -Os enables optimizations that increase performance, but tries to minimize code size.

Additional optimization options that are useful for computationally-intensive scientific applications include -funroll-loops and -ffast-math. -ffast-math allows the compiler allows the compiler to improve performance at the expense of exact IEEE and ISO conformance for math functions.


Shared libraries on AIX versus System V systems

First, let us look at the differences between AIX and System V systems with respect to shared libraries. We will then discuss how to create shared libraries on AIX systems using GCC.

AIX and System V have different views of shared objects. AIX generally sees shared objects as fully linked and resolved entities, where symbol references are resolved at link-time and cannot be rebound at load-time. System V sees them as being very similar to ordinary object files where resolution of all global symbols is performed at run-time by the linker. However, AIX does have the capability to do run-time linking (RTL), so that symbols may be left undefined until loadtime, when a search is performed among all loaded modules to find the necessary definitions.

A shared module in AIX can be an object file or an archive file member which can contain shared modules and/or ordinary object files. In System V, shared libraries are always ordinary files, created with special options.

In AIX, generally all of the linker's input shared objects are listed as dependents of the output file only if there is a reference to their symbols. AIX also makes use of export files to explicitly export symbols. In System V the names of all shared libraries listed on the command line are saved in the output file for possible use at load-time. However, starting in AIX 4.2 and available in all currently available releases of AIX (4.3, 5.1, and 5.2), the -brtl option causes all shared objects (except archive members) on the command line to be listed as dependent objects in the output file. For more information, see AIX Linking and Loading Mechanisms (PDF 184KB).

With GCC, gcc -shared creates a shared library. The linker automatically exports some symbols, but you can use AIX's ld -bexpall to export more symbols. Symbols with underscores are still not exported automatically. On AIX 5.2 or AIX 5.1 at maintenance level 2, it is possible to use the new -bexpfull link option which will export all symbols and does not skip symbols that begin with an underscore (like C++ mangled names). For complete control, it's best to create an export file. If you are unfamiliar with AIX's export files, AIX Linking and Loading Mechanisms (PDF 184KB).

gcc -shared creates an AIX-style, tightly-bound shared object, as briefly described in the AIX vs SystemV section above. gcc -shared invokes the AIX linker with -bM:SRE -bnoentry. Therefore, you don't need to duplicate these two AIX options.

Using -brtl, the AIX linker will look for libraries with both the .a and .so extensions, such as libfoo.a and libfoo.so. Without -brtl, the AIX linker looks only for libfoo.a. You can create libfoo.a simply by archiving a shared object or even by renaming the shared object as libfoo.a -- AIX doesn't care, as long as the file suffix is .a. To use AIX run-time linking, you should create the shared object using gcc -shared -Wl,-G and create executables using the library by adding the -Wl,-brtl option to the link line. Technically, you can leave off the -shared option, but it does no harm and reduces confusion.

If you want to create a shared object with GCC, you only need to use gcc -shared and, possibly, the -bexpfull option or an export file referenced with -Wl,-bE:<export filename>.exp. It's that simple.

Possible issues when using GCC/G++ on AIX

Here are a few possible issues that you might discover in using GCC or G++ on AIX. For each, we have first described the issue, and then we show the solution for that issue.

  • You cannot use the GNU linker on AIX as this will cause incorrectly linked binaries. By default, the AIX linker is used when using GCC on AIX. You should only use the default AIX linker.
  • When compiling and linking with -pthread, the library search path should include -L/usr/lib/threads at the beginning of the path. Check the output of dump -Hv, Import File Strings, entry 0, to see what the paths are. The threads subdirectory should always come before /usr/lib and /lib.
  • Running the gcc -o foo ... -L/path -lmylib doesn't work. It can't open the library because it looks for a static library libmylib.a instead of libmylib.so. If the extension .so is changed to .a, it works fine -- it compiles and runs. Both archive libraries and shared libraries on AIX can have an .a extension. This will explain why you can't link with a .so and why it works with the name changed to .a.
  • When running a 64-bit C++ application using C++ shared libraries and exception handling, the application crashes. GCC 3.4.3 fixes an error that prevented C++ exception handling across shared libraries from operating correctly in 64 bit mode.
  • Linking an application with an archive of object files (normal library, not shared library) produces error messages about unresolved symbols. This can occur if a library contains object files with references to symbols outside of the library that the programmer thought the linker would ignore.

    GCC provides a wrapper around the system linker that scans all object files and non-shared libraries for constructors and destructors. This is done before the linker has an opportunity to skip unneeded object files in the library because a function or data may only be referenced in a constructor or destructor. The scan may find constructors and destructors that the application does not normally reference, requiring additional symbol definitions to satisfy the link. The alternative would miss constructors and destructors required by the application because some object files in the library appeared to be unneeded and were omitted but actually supplied required constructors and destructors. This omission would cause the application to break.

  • When trying to compile GCC on AIX, assembler error messages eventually result. To fix this, make sure that you are not using an old version of the GNU assembler which does not understand certain PowerPC mnemonics. It is preferable to use the AIX assembler as discussed in the AIX installation instructions on the GNU GCC web site.
  • Sometimes you might get this error message while linking:
    ld fatal: failed to write symbol name symbol_name in strings table for file filename

    This error most likely indicates that the disk is full or or that the ULIMIT environment variable won't allow the file to be as large as it needs to be.

  • G++ doesn't do name mangling in the same way as VisualAge (or XL) C++. This means that object files compiled with one compiler cannot be used with another.
  • The GNU assembler (GAS) doesn't support PIC. To generate PIC code, you must use some other assembler, such as the native AIX assembler `/bin/as'.
  • On AIX, compiling code of the form:
     extern int foo;
    ... foo ... 
    static int foo;

    will cause the linker to report an undefined symbol foo. Although this behavior differs from most other systems, it is not a bug because redefining an extern variable as static is undefined in ANSI C.

  • In GCC versions prior to GCC 3.4, does not pass all structures by value. GCC 3.4 now has better ABI support for structure argument passing. While this change brings GCC closer to AIX ABI compliance, these changes may create incompatibilities with code compiled by earlier releases. For more information, see http://gcc.gnu.org/gcc-3.4/powerpc-abi.html.
  • When GCC is built for a particular version of AIX, it generates some files specific to that version of the OS and its header files. This can cause problems when using that GCC build on a newer version of the OS, even just bootstrapping the compiler on the newer OS. An example of this problem occurs when building GCC on AIX 5.2 with GCC compiled for AIX 5.1 (or earlier). AIX 5.2 added support for the atoll() function, but GCC built for AIX 5.1 includes a cached copy of stdlib.h that does not include the prototype for that function. The return value of an unprototyped function defaults to "int" instead of "long long", causing GCC to incorrectly convert some strings. To bootstrap GCC on AIX 5.2, the "fixed" stdlib.h header file in GCC's cache needs to be removed so that the AIX 5.2 header file is used. The system header file can be used by the older build of GCC to bootstrap the compiler.

An example

Here's an example of having a great enough stack size per thread and the corresponding makefile using GCC to compile a program using pthreads.

The array is allocated on the stack, not in the data section. The problem is the stack size per thread. You need to use appropriate pthread functions to increase the default thread stack size. Without the '#ifdef _AIX' code below, this code will coredump.

 $ cat test.c 
#include <pthread.h> 
#include <stdio.h>
#define NUM_THREADS 3 
#define SIZE 100000 
void *PrintHello(void *threadid) 
{ double array[SIZE]; 
int i; 
for (i = 0; i < SIZE; i++) 
array[i] = (double) i;
pthread_exit(NULL); } 
int main() { 
pthread_attr_t tattr; 
pthread_t threads[NUM_THREADS]; 
int rc, t; 
if (rc = pthread_attr_init(&tattr)) {
         printf"ERROR: rc from pthread_attr_init() is %d\n", rc); 
        exit(-1); 
} 
#ifdef _AIX 
if(rc = pthread_attr_setstacksize(&tattr, PTHREAD_STACK_MIN + SIZE *  sizeof(double))) { 
        printf("ERROR: rc from pthread_attr_setstacksize() is %d\n", rc);
        Exit(-1);
} 
#endif 
 for (t = 0; t < NUM_THREADS; t++) { 
        printf("Creating thread %d\n", t); 
        if (rc = pthread_create(&threads[t], &tattr, PrintHello, (void *)t)) { 
              printf("ERROR: rc from pthread_create() is %d\n", rc); exit(-1); } 
}
pthread_attr_destroy(&tattr); pthread_exit(NULL); } 
$ cat Makefile 
host := $(shell uname) 
CC = gcc 
ifeq (,$(findstring CYGWIN,$(host))) # not Cygwin 
     LDFLAGS += -lpthread 
endif 
PROGRAM = test 
OBJECTS = test.o 
all: $(PROGRAM) $(PROGRAM): $(OBJECTS) 
clean: rm -f $(PROGRAM) $(OBJECTS) core tags

Resources

Learn

Get products and technologies

Comments

developerWorks: Sign in

Required fields are indicated with an asterisk (*).


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Information in your profile (your name, country/region, and company name) is displayed to the public and will accompany any content you post, unless you opt to hide your company name. You may update your IBM account at any time.

All information submitted is secure.

Choose your display name



The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

Required fields are indicated with an asterisk (*).

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


All information submitted is secure.

Dig deeper into AIX and Unix on developerWorks


static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=AIX and UNIX
ArticleID=132757
ArticleTitle=Using the GNU C/C++ compiler on AIX
publish-date=08122008