C++ User's Guide

Statically Linking Standard Libraries

The CC driver links in shared versions of several libraries by default, including libc and libm, by passing a -llib option for each of the default libraries to the linker. (See "Default C++ Libraries" for the list of default libraries for compatibility mode and standard mode.)

If you want any of these default libraries to be linked statically, you can use the -xnolib compiler option. With the -xnolib option, the driver does not pass any -l options to ld; you must pass these options yourself. The following example shows how you would link statically with libCrun, and dynamically with libw, libm, and libc in the Solaris 2.5.1, 2.6, or Solaris 7 environment:


demo% CC test.c -xnolib -lCstd -Bstatic -lCrun \
-Bdynamic -lC_mtstubs -lm -lw -lcx -lc

The order of the -l options is important. The -lCstd, -lCrun, -lm, -lw, and -lcx options appear before -lc.


Note -

The -lcx option does not exist on the x86 platform.


Some CC options link to other libraries. These library links are also suppressed by -xnolib. For example, using the -mt option causes the CC driver to pass -lthread to ld in place of -lC_mtstubs. However, if you use both -mt and -xnolib, the CC driver does not pass -lthread to ld. See "-xnolib" for more information. See Linker and Libraries Guide for more information about ld.

You can also use the -library option along with the -staticlib option to link a C++ library statically. This alternative is much easier than the one described earlier. The previous example, for instance, can be performed as:

demo% CC test.c -staticlib=Crun