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 (Mac OS*) to create the .so or .dylib file. On Linux systems using IA-32 architecture and Intel® 64 architecture, you must also specify -fpic for the compilation of each object file you want to include in the shared library.
You can specify the -o output 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 -o output 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.
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 (Mac OS*)
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.
The -o option was omitted, so the name of the shared library file is octagon.so (Linux) or octagon.dylib (Mac OS*).
You can use the -i-static 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 (Mac OS*) 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 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 Fortran libraries. It is easier and safer to use the ifort command. On a Mac OS* system, you would use libtool.
The -shared option is required to create a shared library. On a Mac OS* system, 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 probably a good idea to look at the output of the -dryrun command to find the names of all the libraries used so you can 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.
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, either 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, making sure that you 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.
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 Mac OS* systems, 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.