Running and building Fortran programs on OS X

Version 0.95
Ron Ghosh, Epsom, October 2009 - April 2012


With the introduction of Intel processors into the Macintosh range there has been an increase in legacy scientific packages written in Fortran being ported to the OS-X system. The time-line for versions of this system is:
Tiger (10.4.4), January 2006; Leopard (10.5), October 2007, Snow Leopard (10.6) September 2009, and Lion (10.7) 2011
This note summarises system dependencies for binary programs built with Open Source Fortran Compilers (see http://hpc.sourceforge.net or http://r.research.att.com/tools ) including GNU-g77, GNU-gfortran and G95. The latter two are Fortran 95 compilers.

OS X applications contain system dependent components. These limit programs built on newer systems from functioning on older systems. In addition the extensive use of shared libraries, which are usually not copied into distributed binary packages, create problems in sharing executable programs. Typically scientific programs are derived from Linux sources and use X-window graphics. In addition to the Open source compilers the necessary C header files are found in the Xcode package distributed with each Macintosh system. Fortran packages are usually added separately. Up until the present most code has been developed as 32bit (i386 architecture). The advent of Snow Leopard this year now has had additional impact in that 64bit (x86-64) is now the default, though even today most systems actually still run a 32bit kernel. It should be noted that the "64bit" versions allow very large arrays to be used; this is rarely encountered in data analysis. Calculations are performed with 32bit precision unless explicitly set to double for both 32 and 64bit code.

Most Macintosh-Intel computers have "Intel Core 2 Duo" processors which will run 32 or 64 bit programs. A small number of early computers have a "Intel Core Duo" processor which will not allow building or execution of programs with mixed 32/64bit code (see Menu/Apple/about this Mac/Hardware).

Most of these Fortran compiler packages are downloaded in binary form as .tar.gz archives and simply copied to /usr/local... without any pre-checks. This results in name-confusions of variants if additions are made.

This note includes examples of some of the utilities which can help identify these incompatibilities.

Contents

Running programs
Examples
C programs
Fortran programs
Mixed C and Fortran programs
Commentary and Distribution solutions

Running Programs

  • find binary executable file
  • use load commands therein to find required shared libraries
  • start program
  • The first four bytes of a binary file signal the type of executable file (% file programname shows the required cpu and system information). The linker adds a prelude stub to the executable code which is used by the system loader, hence there is an in-built dependence on the creating system. When a program is started the system loader program reads the binary file and has to find shared libraries which are not stored within. As systems evolve the loader program has more functionalities. On OS X this allows system libraries to be extended as systems evolve, but the older components are still accessible. Evolution of loader features and libraries cause problems of compatibility for running current executables on older systems; on newer systems old libraries may be absent. Typically the library name and/or internally includes a version number which is checked for compatibility. Intel processors are all 32bit (i386 architecture). All recent processors from Intel, (e.g. core-duos) can operate in 64bit mode (x86-64 architecture), adding new alternatives to binary executables.

    It is very useful for newer systems to be able to share and run existing programs without requiring a rebuild for each system version. This note contains comments on the following compilers:

  • g77 the original GNU Fortran77 compiler frozen at version 3.4 (i386)
  • gfortran the current GNU Fortran95 compiler (latest version 4.5 i386 and x86_64)
  • g95 an alternative Fortran95 compiler (i386)
  • All of these compilers use part of the GNU gcc compiler suite, hence produce code which uses the C system libraries. Since a change from "libc5" to "libc6" about 1999 these gcc C libraries have shown very good backward compatibility. X-window libraries X11R6 X-window graphics too have maintained this compatibility. System libraries can contain separate sections for different machine architectures. This feature has been used for some time on the Macintosh with code for the PPC and PPC64, then i386 and x86-64.

    Examples


    Three types of programs are considered here with a summary table following.
  • C programs
  • pure Fortran calculation programs
  • mixed Fortran and C (PGPLOT graphics library examples)
  • C programs
    These programs use only the system libraries, or in the case of pgxwin_server a PGPLOT graphics client, the X11R6 library as well. Providing the shared library for the 32 or 64 bit cpus are present these appear to be OS X version independent. Because pgxwin_server communicates with X packets it will work independently of being built as i386 or x86_64 (providing the cpu is 32/64bit). The standard libraries (10.5) in /usr/lib include libgcc_s.1.0.dylib,libgcc_s.10.4.dylib,libgcc_s.10.5.dylib allowing additional compatibility for v10.4 and v10.5. More recently these routines are included in a bigger system library libSystem.B.dylib containing multiple architectures. For X-window libX11.6.dylib also contains multiple architectures.

    Fortran Programs
    The g95 compiler links all fortran subsystems into each executable, hence uses only the system libraries in /usr/lib, which are, for the most part backward compatible towards binaries from older systems. The GNU programs g77 and gfortran have followed the practice in OS X of using shareable libraries by default. The sharable libraries included in gfortran distributions are not completely consistently named, thus for Tiger the library was libgfortran.3.dylib; later for Leopard the file was initially libgfortran.2.dylib, and the subsequent version 3 was no longer compatible with that available at the time of Tiger. The x86_64 procedures are also included in the same library. These are usually found at:

    g77 		 /usr/local/lib/liblibg2c.0.dylib	(version 3.4, frozen)
    gfortran	 /usr/local/lib/gfortran.2.dylib	(version 4.2.3 - 10.4/5)
    gfortran	 /usr/local/lib/gfortran.3.dylib	(version 4.2.3 - 10.4/5)
             (alas - dependent on which distribution is obtained...)
    gfortran	 /usr/local/lib/gfortran.3.dylib	(version 4.5   - 10.6)
                        (64bit only)
    
    The most recent distribution gfortran-snwleo-intel-bin.tar.gz unpacks into a version specific directory tree (usr/local/libexec/gcc/x86_64-apple-darwin10/4.5.0/). This is the first time the potential conflict has been recognised by the package developers.

    Hence, before programs can run the appropriate fortran library should be in /usr/local/lib. Unfortunately programs built on 10.6 use modified loader commands and are NOT backward compatible with 10.5, even if the library is present. There does not seem to be a repository for the libraries - it is necessary to extract them from the distributions.

    The standard OS X tool "otool", installed with Xcode, shows which libraries are needed:

    % otool -L fprog3264_42
    fprog3264_42:
    	/usr/local/lib/libgfortran.2.dylib (compatibility version 3.0.0, current version 3.0.0)
    	/usr/local/lib/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
    	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 124.1.1)
    % file fprog3264_42
    ../tosx64/d202gin3264_42: Mach-O universal binary with 2 architectures
    ../tosx64/d202gin3264_42 (for architecture i386):	Mach-O executable i386
    ../tosx64/d202gin3264_42 (for architecture x86_64):	Mach-O 64-bit executable x86_64
    
    The file command shows that the executable was built for both 32 and 64 bit architectures (each object file has two sets of code with the same symbol names) ...
    gfortran -o fprog3264_42 -arch i386 -arch x86_64 fprog......
    
    The otool program showed that some gfortran distributions at the time of Tiger already had a version 3 library, while the version 2 library was still included in other distributions into the time of Leopard.
    Leopardsys % ./forvTigerprog
    dyld: Library not loaded: /usr/local/lib/libgfortran.3.dylib
      Referenced from: /Users/xxx/yyy/forvTigerprog
      Reason: image not found
    Trace/BPT trap
    Leopardsys % ls /usr/local/lib/libgf*
    -rwxrwxr-x  1 root  wheel   8094839 24 Feb  2008 /usr/local/lib/libgfortran.2.0.0.dylib
    lrwxr-xr-x  1 root  wheel        23 16 Aug  2008 /usr/local/lib/libgfortran.2.dylib -> libgfortran.2.0.0.dylib
    -rw-rw-r--  1 root  wheel  13836364 24 Feb  2008 /usr/local/lib/libgfortran.a
    lrwxr-xr-x  1 root  wheel        23 16 Aug  2008 /usr/local/lib/libgfortran.dylib -> libgfortran.2.0.0.dylib
    -rwxrwxr-x  1 root  wheel       779 24 Feb  2008 /usr/local/lib/libgfortran.la
    [
    
    It is posssible to include an alternative shared library when running a program using the DYLD_LIBRARY_PATH environment variable to point to its directory. This can be used to distinguish between similarly named libraries but from distinctly different distributions. For the ILL-SANS package distibution this latter route has been adopted, and the necessary libraries are included with the program files. The startup routine (tcsh "sassetup") includes setting the DY_LIBRARY_PATH to the program file directory.

    The program file thus functions on the range of systems 10.4-10.7 transparently, with no need for privileges to install libraries in /usr/local/lib.

    Mixed C and Fortran Programs (e.g. typical X-window programs

    The version problems here arise primarily with the loader stub, and the default architecture assumed by the compilers which may conflict with the system default. In a typical makefile the fortran command uses the newly added compiler. The gcc command uses the system's default gcc compiler. Most Fortran compilers default to i386, 32 bit, with the exception of the v10.6 gfortran (x86_64). Up to v10.6 the system default was i386, changed now in Snow Leopard to x86_64. Problems can be avoided in the makefile if either -arch i386 or -arch x86_64 is added explicitly to each gfortran or gcc command. As mentioned above the shared libraries have code for both architectures. Even if a compatible gcc compiler is also installed in /usr/local the PATH variable could select these compilers first, obviating the mismatch there remains the problem of the linker always using the local system loader stubs /usr/lib/crt1*.o which initialise the C run-time libraries. Unfortunately too, there is no environment variable for the gcc suite to select a specific machine architecture for building.

    [system10.5_leopard]% file /usr/lib/crt1*.o
    /usr/lib/crt1.10.5.o: Mach-O universal binary with 4 architectures
    /usr/lib/crt1.10.5.o (for architecture ppc7400):	Mach-O object ppc
    /usr/lib/crt1.10.5.o (for architecture i386):	Mach-O object i386
    /usr/lib/crt1.10.5.o (for architecture ppc64):	Mach-O 64-bit object ppc64
    /usr/lib/crt1.10.5.o (for architecture x86_64):	Mach-O 64-bit object x86_64
    /usr/lib/crt1.o:      Mach-O universal binary with 4 architectures
    /usr/lib/crt1.o (for architecture ppc):	Mach-O object ppc
    /usr/lib/crt1.o (for architecture i386):	Mach-O object i386
    /usr/lib/crt1.o (for architecture ppc64):	Mach-O 64-bit object ppc64
    /usr/lib/crt1.o (for architecture x86_64):	Mach-O 64-bit object x86_64
    
    

    Build and Running system Compatibilities - gfortran(1), gcc(2)

    Built run target 10.4run target 10.5 run target 10.6
    10.4needs (1),(2)needs (1),(2) needs (1),(2)
    10.5Load failsneeds (1),(2) needs (1),(2)
    10.6Load failsLoad fails needs (1),(2)
    (1)gfortran-build needs libgfortran.2.dylib or libgfortran.3.dylib (32 or 64) usually in /usr/local/lib
    (2)g77-build needs libg2c.0.dylib (32 bit)

    Commentary and Distribution Solutions

    Only a few calculations (very large tables) are likly to benefit from 64bit calculation mode: legacy code for C programs is a little fragile if pointers and integers have been mixed!

    Each type of compiler has pros and cons.

    The g77 is frozen, last OS-X version 3.4 (2006). It is a very fast compiler

    The gfortran compiler is part of the GNU on-going development. The newest versions (4.5) match the newest gcc compiler which is evolving rapidly with multi-threaded code-generation which runs well on multiple cpus. The compiler is a little slow to generate code, but performs rigorous syntax checks.

    The G95 project produced g95, a fully-functional Fortran95 compiler, which runs well but the development group is smaller. One advantage is that only standard system shared-libraries are used, simplifying compatibility as OS X versions are updated.

    A practical approach to simplify distribution of binary executables, where maximum performance is not of the first importance, is to link the distributable version on an older system. This might be built on a separate partition, an external disk, or a virtual machine. The alternate disk system is booted holding down the Option (ALT) key after switch-on. The older system must of course be able to run a reasonable compiler distribution, and deliver an executable which gives acceptable results.

    When exporting the binary executable files include the appropriate "dylibs" to maximise the shareability of the binaries for similar and backwards compatibility.

    Last updated 20th June 2010