Building a dynamic library requires a compilation of the source files with the -pic or -PIC option and linker options -G, -ztext, and -hname. These linker options are available through the compiler command line.
You can create a dynamic library with the same files used in the static library example.
Example: Compile with -pic and other linker options:
demo% f77 -o libtestlib.so.1 -G -pic -ztext -hlibtestlib.so.1 *.f delte.f: delte: q_fixx: dropx.f: dropx: etc.f: q_fill: q_step: q_node: q_warn: evalx.f: evalx: linkz.f: linkz: markx.f: markx: point.f: point: Linking:
-G tells the linker to build a dynamic library.
-ztext warns you if it finds anything other than position-independent code, such as relocatable text.
Example: Make an executable file a.out using the dynamic library:
demo% f77 -o trylib -R`pwd` trylib.f libtestlib.so.1 trylib.f: MAIN main: demo% file trylib trylib:ELF 32-bit MSB executable SPARC Version 1, dynamically linked, not stripped demo% ldd trylib libtestlib.so.1 => /export/home/U/Tests/libtestlib.so.1 libF77.so.4 => /opt/SUNWspro/lib/libF77.so.4 libc.so.1 => /usr/lib/libc.so.1 libdl.so.1 => /usr/lib/libdl.so.1
Note that the example uses the -R option to bind into the executable the path (the current directory) to the dynamic library.
The file command shows that the executable is dynamically linked.
The ldd command shows that the executable, trylib, uses some shared libraries, including libtestlib.so.1; libf77, libdl, and libc are included by default by f77.