Including Linux .so-files in Fortran programs

Hello all,
I am working on some Fortran code which calls an additional function from a DLL (dynamic link library). In order for the code to work under Linux, it must be possible to read in SO (shared library) files in addition to the DLLs. The DLL and SO are generated from C++ code. The C++ code itself is not very complicated, i.e. no objects, etc.
Unfortunately, I can hardly find any information on how the SO files can be used in Fortran. I would be very grateful for any advice.

Shared object files (libraries) are usually stored in the same directory as the executable or in /usr/lib/ (Linux) or /usr/local/lib/ (Unix). When compiling your Fortran project, you just link dynamically against your library, for instance, for a code file test.f90 and a library

$ gfortran -o test test.f90 -ldummy

In addition to @interkosmos answer, if your dynamic library is outside of your current compile directory and not in one of the default searched locations, you will have to use the -L option to direct the linker to the .so

gfortran -Wall -o test test.f90 -L/path/to/lib -ldummy

Let me add a bit more even:

  • On Linux it is common to call libraries something like “libmylib.a” for static libraries and “” for shared library (DLLs in Windows parlance). Then the compile/link option -lmylib will find these libraries.
  • While it is common, it is by no means mandatory. You can still name your library mylib.a or (the extensions are conventions as well as far as I know, but do no tmess about with them, because otherwise the result will indeed be a mess).
  • The conventions mentioned above play a role with certain details of the linking process, like preferring staitc over dynamic/shared libraries or vice versa.
  • Unlike the typical situation on Windows, linkers on Linux do not require a separate import library. On Windows you would have mylib.lib as the import library and mylib.dll as the actual dynamic library.
  • Shared/dynamic libraries and static libraries on Linux are very similar at compile/link time, though not at runtime.
1 Like