Intel® Fortran Compiler Classic and Intel® Fortran Compiler Developer Guide and Reference

ID 767251
Date 9/08/2022
Public

A newer version of this document is available. Customers should click here to go to the newest version.

Document Table of Contents

Create Shared Libraries

This topic applies to Linux* and macOS.

Shared libraries, also referred to as dynamic libraries, are linked differently than static libraries. At compile time, the linker insures that all the necessary symbols are either linked into the executable, or can be linked at runtime from the shared library. Executables compiled from shared libraries are smaller, but the shared libraries must be included with the executable to function correctly. When multiple programs use the same shared library, only one copy of the library is required in memory.

To create a shared library from a Fortran source file, process the files using the ifort command:

  • You must specify the -shared option (Linux*) or the -dynamiclib option (macOS) to create the .so or .dylib file. On Linux* and macOS operating systems using either IA-32 architecture or Intel® 64 architecture, you must also specify option -fpic for the compilation of each object file you want to include in the shared library.

  • You can specify the -ooutput option to name the output file.

  • If you omit the -c option, you will create a shared library (.so file) directly from the command line in a single step.

  • If you also omit the -ooutput option, the file name of the first Fortran file on the command line is used to create the file name of the .so file. You can specify additional options associated with shared library creation.

  • If you specify the -c option, you will create an object file (.o file) that you can name with the -o option. To create a shared library, process the .o file with ld , specifying certain options associated with shared library creation.

Create a Shared Library

There are several ways to create a shared library.

You can create a shared library file with a single ifort command:

ifort -shared -fpic octagon.f90 (Linux*)
ifort -dynamiclib octagon.f90 (macOS)

The -shared or -dynamiclib option is required to create a shared library. The name of the source file is octagon.f90. You can specify multiple source files and object files.

Because the -o option was omitted, the name of the shared library file is octagon.so (Linux*) or octagon.dylib (macOS).

You can use the -static-intel option to force the linker to use the static versions of the Intel-supplied libraries.

You can also create a shared library file with a combination of ifort and ld (Linux*) or libtool (macOS) commands:

First, create the .o file, such as octagon.o in the following example:

ifort -c -fpic octagon.f90

The file octagon.o is then used as input to the ld (Linux*) or libtool (macOS) command to create the shared library. The following example shows the command to create a shared library named octagon.so on a Linux* operating system:

ld -shared octagon.o \
     -lifport -lifcoremt -limf -lm -lcxa \
     -lpthread -lirc -lunwind -lc -lirc_s

Note the following:

  • When you use ld, you need to list all Intel® Fortran libraries. It is easier and safer to use the ifort command. On macOS, you would use libtool.

  • The -shared option is required to create a shared library. On macOS, use the -dynamiclib option, and also specify the following: -arch_only i386, -noall_load, -weak_references_mismatches non-weak.

  • The name of the object file is octagon.o. You can specify multiple object (.o) files.

  • The -lifport option and subsequent options are the standard list of libraries that the ifort command would have otherwise passed to ld or libtool. When you create a shared library, all symbols must be resolved.

It is recommended that you review the output of the -dryrun command to find the names of all the libraries used, to be able to specify them correctly.

If you are using the ifort command to link, you can use the -Qoption command to pass options to the ld linker. (You cannot use -Qoption on the ld command line.)

For more information on relevant compiler options, see the Compiler Options reference.

See also the ld(1) reference page.

Shared Library Restrictions

When creating a shared library with ld, be aware of the following restrictions:

  • Shared libraries must not be linked with archive libraries.

    When creating a shared library, you can only depend on other shared libraries for resolving external references. If you need to reference a routine that currently resides in an archive library, put that routine in a separate shared library or include it in the shared library being created. You can specify multiple object (.o) files when creating a shared library.

    To put a routine in a separate shared library, obtain the source or object file for that routine, recompile if necessary, and create a separate shared library. You can specify an object file when recompiling with the ifort command or when creating the shared library with the ld command.

    To include a routine in the shared library being created, put the routine (source or object file) with other source files that make up the shared library and recompile if necessary.

    Now create the shared library, and specify the file containing that routine either during recompilation or when creating the shared library. You can specify an object file when recompiling with the ifort command or when creating the shared library with the ld or libtool command.

  • When creating shared libraries, all symbols must be defined (resolved).

    Because all symbols must be defined to ld when you create a shared library, you must specify the shared libraries on the ld command line, including all standard Intel® Fortran libraries. The list of standard Intel® Fortran libraries can be specified by using the -lstring option.

Install Shared Libraries

Once the shared library is created, it must be installed for private or system-wide use before you run a program that refers to it:

  • To install a private shared library (when you are testing, for example), set the environment variable LD_LIBRARY_PATH, as described in ld(1). For macOS, set the environment variable DYLD_LIBRARY_PATH.

  • To install a system-wide shared library, place the shared library file in one of the standard directory paths used by ld or libtool.