To create a shared library from a Fortran source file, process the files using the ifort command:
You must specify the -shared option to create the .so file.
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.
When building shared libraries on Itanium-based systems, you must specify the -fpic option for the compilation of each object file included in the shared library. If this option is not used, the linker will probably issue an error message like @gprel relocation against dynamic symbol.
You can create a shared library (.so) file with a single ifort command:
ifort -shared octagon.f90
The -shared 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.
You can use the -i-static option to force the linker to use the static versions of the Intel-supplied libraries.
You first must create the .o file, such as octagon.o in the following example:
ifort -c octagon.f90
The file octagon.o is then used as input to the ld command to create the shared library named octagon.so:
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.
The -shared option is required to create a shared library.
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. 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 linker. (You cannot use -Qoption on the ld command line.)
For more information on shared libraries, see Creating Libraries.
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.
Then 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 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
might 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).
To install a system-wide shared library, place the shared library file in one of the standard directory paths used by ld. See ld(1).