Developer Guide and Reference

ID 767251
Date 10/31/2024
Public
Document Table of Contents

Create Libraries

This topic contains information about shared and static libraries.

For more information about compiler options mentioned in this topic, find the links to their full descriptions in the Alphabetical Options List.

Shared Libraries

Shared libraries, also referred to as dynamic libraries, are linked differently than static libraries. At compile time, the linker ensures that all the necessary symbols are either linked into the executable or can be linked at runtime from the shared library.

Executables compiled using 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.

Create a Shared Library

Linux

To create a shared library from a Fortran source file, process the files using the ifx commands:

  • You must specify the -shared option to create the .so file. 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 -o filename option to name the shared library.

  • 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 omit the -o filename 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.

  • With option -shared, you can specify multiple object (.o) files that were compiled with option -fpic.

For example, you can create a shared library file with a single command:

ifx -shared -fpic octagon.f90 

The -shared and -fpic options are required to create a shared library. The name of the source file is octagon.f90. Because the -o option was omitted, the name of the shared library file is octagon.so.

Note the following:

  • The -shared and -fpic options are required to create a shared library.

  • The name of the object file is octagon.o. With -shared you can specify multiple object (.o) files where all are compiled with -fpic to include them in one shared library

To see a list of options that may be helpful, see Linking or Linker Options.

Windows

Use the following options to create shared libraries on Windows:

Option

Description

/dll

Produces a DLL.

/MD, /MDd

Compiles and links with the dynamic, multi-thread run time library. The d in /MDd indicates the debug version.

/libdir:none

Disables embedding default libraries in object files.

For more information about creating and using Fortran DLLs, see Use Intel® Fortran to Create and Build Windows-Based Applications.

Install Shared Libraries on Linux or Windows

After a 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 (for example, when you are testing), 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 or libtool.

Static Libraries

Executables generated using static libraries are no different than executables generated from individual source or object files. Because static libraries are not required at runtime, you do not need to include them when you distribute your executable. Linking to a static library is generally faster than linking to individual object files.

When building objects for a static library from the ifx command line, include option c to suppress linking. Without this option, the linker will run and generate an error because the object is not a complete program.

Build a Static Library

Linux

  1. Use the -c option to generate object files from the source files:

    ifx -c my_source1.f90 my_source2.f90 my_source3.f90
  2. Use the usual GNU ar tool to create the library file from the object files:

    ar rc my_lib.a my_source1.o my_source2.o my_source3.o

    If using the -flto or -ipo option during the compile step, you must use the LLVM tool llvm-ar to create the library file from the object files. To install llvm-ar, see https://llvm.org/docs/CommandGuide/llvm-ar.html.

    llvm-ar rc my_lib.a my_source1.o my_source2.o my_source3.o
  3. Compile and link your project with your new library:

    ifx main.f90 my_lib.a

If your library file and source files are in different directories, use the -Ldir option to indicate where your library is located. For example:

ifx -L/for/libs main.f90 my_lib.a

Windows

To build a static library from the integrated development environment (IDE), select the Fortran Static Library project type.

Use the following options to create static libraries on Windows:

Option

Description

/MT, /MTd

Compiles and links with the static, multi-thread run time library. The d in /MTd indicates the debug version.

/libdir:none

Disables embedding default libraries in object files.

See Also