Skip to content

Compile and link to static library

Hani Andreas Ibrahim edited this page Apr 2, 2022 · 2 revisions

To avoid copying the source file f90getopt.F90 to your working directory (even if this is recommended) every time you want to use their features, it make sense to create a static library. After, you just need to link the library to your project. To do this follow the steps below

Compile and build the library

Change to the directory where the f90getopt.F90 is located and type:

gfortran -c f90getopt.F90
ar cr libf90getopt.a f90getopt.o
ranlib libf90getopt.a

You will get a static libray called libf90getopt.a and a module file f90getopt.mod.

Install the library

Move the a-file on UNIX(-like) systems to /usr/local/lib and the mod-file to /usr/local/include:

sudo cp ./libf90getopt.a /usr/local/lib/
sudo cp ./f90getopt.mod /usr/local/include/

On Windows go to the appropriate directory of your compiler-system. MinGW and Cygwin are similar to UNIX. Integrated Development Environments (IDE) have their own rules. Refer the manual of your IDE.

Compile a program with the library

To compile and link the sample program with the library can be done on Unixes with:

gfortran -o f90getopt_sample f90getopt_sample.f90 -I/usr/local/include -lf90getopt

Change paths to match the right ones on Windows.