Creating Static Libraries

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

When compiling a static library from the ifort command line, include the -c (Linux and Mac OS) or /c (Windows) compiler option to suppress linking. Without this option, the compiler generates an error because the library does not contain a main program.

To build a static library (Linux and Mac OS systems):

  1. Use -c option and, optionally, the -fpic option to generate object files from the source files: 
    ifort -fpic -c my_source1.f90 my_source2.f90 my_source3.f90

  2. Use the 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

  3. Compile and link your project with your new library: 
    ifort 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:

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

To build a static library (Windows systems):

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

 To build a static library using the command line:

 lib /out:mylib.lib my_source1.obj mysource2.out