| C++ User's Guide |
Using Libraries
Libraries provide a way to share code among several applications and a way to reduce the complexity of very large applications. The Sun WorkShop C++ compiler gives you access to a variety of libraries. This chapter explains how to use these libraries.
5.1 The C Libraries
The Solaris operating environment comes with several libraries installed in
/usr/lib. Most of these libraries have a C interface. Of these, thelibc,libm, andlibwlibraries are linked by theCCdriver by default. The librarylibthreadis linked if you use the-mtoption. To link any other system library, use the appropriate-loption at link time. For example, to link thelibdemanglelibrary, pass-ldemangleon theCCcommand line at link time:
example%CC text.c -ldemangleThe Sun WorkShop 6 C++ compiler has its own runtime support libraries. All C++ applications are linked to these libraries by the
CCdriver. The C++ compiler also comes with several other useful libraries, as explained in the following section.5.2 Libraries Provided With the C++ Compiler
Several libraries are shipped with the C++ compiler. Some of these libraries are available only in compatibility mode (
-compat=4), some are available only in the standard mode (-compat=5), and some are available in both modes. Thelibgcandlibdemanglelibraries have a C interface and can be linked to an application in either mode.The following table lists the libraries that are shipped with the C++ compiler and the modes in which they are available.
5.2.1 C++ Library Descriptions
A brief description of each of these libraries follows.
libCrun: This library contains the runtime support needed by the compiler in the standard mode (-compat=5). It provides support fornew/delete, exceptions, and RTTI.libCstd: This is the C++ standard library. In particular, it includesiostreams. If you have existing sources that use the classiciostreamsand you want to make use of the standardiostreams, you have to modify your sources to conform to the new interface. See the C++ Standard Library Reference manual for details.libiostream: This is the classic iostreams library built with-compat=5. If you have existing sources that use the classic iostreams and you want to compile these sources with the standard mode(-compat=5), you can uselibiostreamwithout modifying your sources. Use-library=iostreamto get this library.libC: This is the library needed in compatibility mode (-compat=4). It contains the C++ runtime support as well as the classic iostreams.libcomplex: This library provides complex arithmetic in compatibility mode (-compat=4). In the standard mode, the complex arithmetic functionality is available inlibCstd.librwtool:(Tools.h++7) This is Rogue Wave'sTools.h++version 7 library.libgc:This is the garbage collection library (a component of Sun WorkShop Memory Monitor). You can access documentation about this library by launching the Memory Monitor or by pointing your web browser to:
file:install-directory/SUNWspro/docs/index.html- Replace install-directory with the path to your Sun WorkShop installation directory. In a default installation, install-directory is
/opt.libdemangle:This library is used for demangling C++ mangled names.5.2.2 Default C++ Libraries
Some of the C++ libraries are linked by default by the
CCdriver, while others need to be linked explicitly. In the standard mode, the following libraries are linked by default by theCCdriver:
-lCstd -lCrun -lm -lw -lcx -lcIn compatibility mode (
-compat), the following libraries are linked by default:See Section 3.2.41 -library=l[,...l] for more information.
5.3 Related Library Options
The
CCdriver provides several options to help you use libraries.
- Use the
-loption to specify a library to be linked.- Use the
-Loption to specify a directory to be searched for the library.- Use the
-libraryoption to specify the following libraries that are shipped with the Sun C++ compiler:A library that is specified using both
-libraryand-staticliboptions will be linked statically. Some examples:
- The following command links the
Tools.h++version 7 andlibiostreamlibraries dynamically.
example%CC test.cc -library=rwtools7,iostream- The following command links the
libgclibrary statically.
example%CC test.cc -library=gc -staticlib=gc- The following command compiles
test.ccin compatibility mode and linkslibCstatically. BecauselibCis linked by default in compatibility mode, you are not required to specify this library using the-libraryoption.
example%CC test.cc -compat=4 -staticlib=libC- The following command excludes the libraries
libCrunandlibCstd, which would otherwise be included by default.
example%CC test.cc -library=no%Crun,no%CstdBy default,
CClinks various sets of system libraries depending on the command line options. If you specify-xnolib(or-nolib),CClinks only those libraries that are specified explicitly with the-loption on the command line. (When-xnolibor-nolibis used, the-libraryoption is ignored, if present.)The
-Roption allows you to build dynamic library search paths into the executable file. At execution time, the runtime linker searches these paths for the shared libraries needed by the application. TheCCdriver passes-R/opt/SUNWspro/libtoldby default (if the compiler is installed in the standard location). You can use-norunpathto disable building the default path for shared libraries into the executable.5.4 Using Class Libraries
Generally, two steps are involved in using a class library:
5.4.1 The
iostreamLibraryThe Sun Workshop 6 C++ compiler provides two implementations of iostreams:
- Classic iostreams. This term refers to the iostreams library shipped with the C++ 4.0, 4.0.1, 4.1, and 4.2 compilers, and earlier with the
cfront-based 3.0.1 compiler. There is no standard for this library, but a lot of existing code uses it. This library is part oflibCin compatibility mode and is also available inlibiostreamin the standard mode.- Standard iostreams. This is part of the C++ standard library,
libCstd, and is available only in standard mode. It is neither binary- nor source-compatible with the classic iostreams library.If you have existing C++ sources, your code might look like the following example, which uses classic iostreams.
// file prog1.cc#include <iostream.h>int main() {cout << "Hello, world!" << endl;return 0;}The following command compiles in compatibility mode and links
prog1.ccinto an executable program calledprog1. The classic iostream library is part oflibC, which is linked by default in compatibility mode.
example%CC -compat prog1.cc -o prog1The next example uses standard iostreams.
// file prog2.cc#include <iostream>int main() {std::cout << "Hello, world!" << std::endl;return 0;}The following command compiles and links
prog2.ccinto an executable program calledprog2. The program is compiled in standard mode andlibCstd, which includes the standard iostream library, is linked by default.
example%CC prog2.cc -o prog25.4.2 The
complexLibraryThe standard library provides a templatized complex library that is similar to the complex library provided with the C++ 4.2 compiler. If you compile in standard mode, you must use
<complex>instead of<complex.h>. You cannot use<complex>in compatibility mode.In compatibility mode, you must explicitly ask for the complex library when linking. In standard mode, the complex library is included in libCstd, and is linked by default.
There is no
complex.hheader for standard mode. In C++ 4.2, "complex" is the name of a class, but in standard C++, "complex" is the name of a template. It is not possible to provide typedefs that allow old code to work unchanged. Therefore, code written for 4.2 that uses complex numbers will need some straightforward editing to work with the standard library. For example, the following code was written for 4.2 and will compile in compatibility mode.
// file ex1.cc (compatibility mode)#include <iostream.h>#include <complex.h>int main(){complex x(3,3), y(4,4);complex z = x * y;cout << "x=" << x << ", y=" << y << ", z=" << z << endl;}The following example compiles and links
ex1.ccin compatibility mode, and then executes the program.
example%CC -compat ex1.cc -library=complexexample%a.outx=(3, 3), y=(4, 4), z=(0, 24)Here is
ex1.ccrewritten asex2.ccto compile in standard mode:
The following example compiles and links the rewritten
ex2.ccin standard mode, and then executes the program.
%CC ex2.cc%a.outx=(3,3), y=(4,4), z=(0,24)5.4.3 Linking C++ Libraries
The following table shows the compiler options for linking the C++ libraries. See Section 3.2.41 -library=l[,...l] for more information.
5.5 Statically Linking Standard Libraries
The
CCdriver links in shared versions of several libraries by default, includinglibcandlibm, by passing a-llib option for each of the default libraries to the linker.(See Section 5.2.2 "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
-libraryoption along with the-staticliboption to link a C++ library statically. This alternative is much easier than the one described earlier. For example:
example%CC test.c -staticlib=CrunIn this example, the
-libraryoption is not explicitly included in the command. In this case the-libraryoption is not necessary because the default setting for-libraryis%none,Cstd,Crunin standard mode (the default mode).Alternately, you can use the
-xnolibcompiler option. With the-xnoliboption, the driver does not pass any-loptions told; you must pass these options yourself. The following example shows how you would link statically withlibCrun, and dynamically withlibw,libm, andlibcin the Solaris 2.6, Solaris 7, or Solaris 8 environment:
example%CC test.c -xnolib -lCstd -Bstatic -lCrun \-Bdynamic -lm -lw -lcx -lcThe order of the
-loptions is important. The-lCstd, -lCrun,-lm,-lw, and-lcxoptions appear before-lc.
Note The-lcxoption does not exist on the IA platform.
Some
CCoptions link to other libraries. These library links are also suppressed by-xnolib. For example, using the-mtoption causes theCCdriver to pass-lthreadtold. However, if you use both-mtand-xnolib, theCCdriver does not pass-lthreadtold. See Section 3.2.114 "-xnolib"" for more information. See Linker and Libraries Guide for more information aboutld.5.6 Using Shared Libraries
The following shared libraries are included with the C++ compiler:
libCrun.so.1libC.so.5libcomplex.so.5librwtool.so.2libgc.so.1libgc_dbg.so.1The occurrence of each shared object linked with the program is recorded in the resulting executable (
a.outfile); this information is used byld.soto perform dynamic link editing at runtime. Because the work of incorporating the library code into an address space is deferred, the runtime behavior of the program using a shared library is sensitive to an environment change--that is, moving a library from one directory to another. For example, if your program is linked withlibcomplex.so.5in/opt/SUNWspro/release/lib, and thelibcomplex.so.5library is later moved into/opt2/SUNWspro/release/lib, the following message is displayed when you run the binary code:
ld.so: libcomplex.so.5: not foundYou can still run the old binary code without recompiling it by setting the environment variable
LD_LIBRARY_PATHto the new library directory.In a C shell:
example%setenv LD_LIBRARY_PATH \/opt2/SUNWspro/release/lib:${LD_LIBRARY_PATH}In a Bourne shell:
example$LD_LIBRARY_PATH=\/opt2/SUNWspro/release/lib:${LD_LIBRARY_PATH}example$export LD_LIBRARY_PATH
Note release is specific for each release of Sun WorkShop.
The
LD_LIBRARY_PATHhas a list of directories, usually separated by colons. When you run a C++ program, the dynamic loader searches the directories inLD_LIBRARY_PATHbefore it searches the default directories.Use the
lddcommand as shown in the following example to see which libraries are linked dynamically in your executable:
example%ldd a.outThis step should rarely be necessary, because the shared libraries are seldom moved.
Note When shared libraries are opened withdlopen,RTLD_GLOBALmust be used for exceptions to work.
See Linker and Libraries Guide for more information on using shared libraries.
5.7 Replacing the C++ Standard Library
Replacing the standard library that is distributed with the compiler is risky, and good results are not guaranteed. If you want to use a different version of the C++ standard library for reasons of performance, features, or compatibility with other systems, WorkShop 6 C++ provides the means to do so. The basic operation is to disable the standard headers and library supplied with the compiler, and to specify the directories where the new header files and library are found, as well as the name of the library itself.
5.7.1 What Can be Replaced
You can replace most of the standard library and its associated headers. The replaced library is
libCstd, and the associated headers are listed in the following table:
The replaceable part of the library consists of what is loosely known as "STL", plus the string classes, the iostream classes, and their helper classes. Because these classes and headers are interdependent, replacing just a portion of them is unlikely to work. You should replace all of the headers and all of
libCstdif you replace any part.The standard headers
<exception>,<new>, and<typeinfo>are tied tightly to the compiler itself and tolibCrun, and cannot reliably be replaced. The librarylibCruncontains many "helper" functions that the compiler depends on, and cannot be replaced.The 17 standard headers inherited from C (
<stdlib.h>,<stdio.h>,<string.h>, and so forth) are tied tightly to the Solaris operating environment and the basic Solaris runtime librarylibc, and cannot reliably be replaced. The C++ versions of those headers (<cstdlib>,<cstdio>,<cstring>, and so forth) are tied tightly to the basic C versions and cannot reliably be replaced.5.7.2 Installing the Replacement Library
To install the replacement library, you must first decide on the locations for the replacement headers and on the replacement for
libCstd. For purposes of discussion, assume the headers are placed in/opt/mycstd/includeand the library is placed in/opt/mycstd/lib. Assume the library is calledlibmyCstd.a. (It is often convenient if the library name starts with "lib".)5.7.3 Using the Replacement Library
On each compilation, use the
-Ioption to point to the location where the headers are installed. In addition, use the-library=no%Cstdoption to prevent finding the compiler's own versions of thelibCstdheaders. For example:
example%CC -I/opt/mycstd/include -library=no%Cstd ...(compile)During compiling, the
-library=no%Cstdoption prevents searching the directory where the compiler's own version of these headers is located.On each program or library link, use the
-library=no%Cstdoption to prevent finding the compiler's ownlibCstd, the-Loption to point to the directory where the replacement library is, and the-loption to specify the replacement library. Example:
example%CC -library=no%Cstd -L/opt/mycstd/lib -lmyCstd ...(link)Alternatively, you can use the full path name of the library directly, and omit using the
-Land-loptions. For example:
example%CC -library=no%Cstd /opt/mycstd/lib/libmyCstd.a ...(link)During linking, the
-library=no%Cstdoption prevents linking the compiler's own version oflibCstd.5.7.4 Standard Header Implementation
C has 17 standard headers (
<stdio.h>,<string.h>,<stdlib.h>, and others). These headers are delivered as part of the Solaris operating environment, in the directory/usr/include. C++ has those same headers, with the added requirement that the various declared names appear in both the global namespace and in namespacestd. On versions of the Solaris operating environment prior to Solaris 8, the C++ compiler supplies its own versions of these headers instead of replacing those in the/usr/includedirectory.C++ also has a second version of each of the C standard headers (
<cstdio>,<cstring>, and<cstdlib>, and others) with the various declared names appearing only in namespacestd. Finally, C++ adds 32 of its own standard headers (<string>,<utility>,<iostream>, and others).The obvious implementation of the standard headers would use the name found in C++ source code as the name of a text file to be included. For example, the standard headers
<string>(or<string.h>) would refer to a file namedstring(orstring.h) in some directory. That obvious implementation has the following drawbacks:
- You cannot search for just header files or create a
makefilerule for the header files if they do not have file name suffixes.- If you put
-I/usr/includeon the compiler command line, you do not get the correct version of the standard C headers on Solaris 2.6 and Solaris 7 operating environments because/usr/includeis searched before the compiler's own include directory is searched.- If you have a directory or executable program named
string, it might erroneously be found instead of the standard header file.- On versions of the Solaris operating environment prior to Solaris 8, the default dependencies for makefiles when
.KEEP_STATEis enabled can result in attempts to replace standard headers with an executable program. (A file without a suffix is assumed by default to be a program to be built.)To solve these problems, the compiler
includedirectory contains a file with the same name as the header, along with a symbolic link to it that has the unique suffix.SUNWCCh(SUNWis the prefix for all compiler-related packages,CCis the C++ compiler, andhis the usual suffix for header files). When you specify<string>, the compiler rewrites it to<string.SUNWCCh>and searches for that name. The suffixed name will be found only in the compiler's ownincludedirectory. If the file so found is a symbolic link (which it normally is), the compiler dereferences the link exactly once and uses the result (stringin this case) as the file name for error messages and debugger references. The compiler uses the suffixed name when emitting file dependency information.The name rewriting occurs only for the two forms of the 17 standard C headers and the 32 standard C++ headers, only when they appear in angle brackets and without any path specified. If you use quotes instead of angle brackets, specify any path components, or specify some other header, no rewriting occurs.
The following table illustrates common situations.
If the compiler does not find header
.SUNWCCh, the compiler restarts the search looking for the name as provided in the#includedirective. For example, given the directive#include<string>, the compiler attempts to find a file namedstring.SUNWCCh. If that search fails, the compiler looks for a file namedstring.5.7.4.1 Replacing Standard C++ Headers
Because of the search algorithm described in Section 5.7.4 "Standard Header Implementation"," you do not need to supply
SUNWCChversions of the replacement headers described in Section 5.7.2 "Installing the Replacement Library"." But you might run into some of the described problems. If so, the recommended solution is to add symbolic links having the suffix.SUNWCChfor each of the unsuffixed headers. That is, for fileutility, you would run the command
example%ln -s utility utility.SUNWCChWhen the compiler looks first for
utility.SUNWCCh, it will find it, and not be confused by any other file or directory calledutility.5.7.4.2 Replacing Standard C Headers
Replacing the standard C headers is not supported. If you nevertheless wish to provide your own versions of standard headers, the recommended procedure is as follows:
- Put all the replacement headers in one directory.
- Create a
.SUNWCChsymbolic link to each of the replacement headers in that directory.- Cause the directory that contains the replacement headers to be searched by using the
-Idirectives on each invocation of the compiler.For example, suppose you have replacements for
<stdio.h>and<cstdio>. Put the filesstdio.handcstdioin directory/myproject/myhdr. In that directory, run these commands:
example%ln -s stdio.h stdio.h.SUNWCChexample%ln -s cstdio cstdio.SUNWCChUse the option
-I/myproject/mydiron every compilation.Caveats:
- If you replace any C headers, you must replace them in pairs. For example, if you replace
<time.h>, you should also replace<ctime>.- Replacement headers must have the same effects as the versions being replaced. That is, the various runtime libraries such as
libCrun,libC,libCstd,libc, andlibrwtoolare built using the definitions in the standard headers. If your replacements do not match, your program is unlikely to work.
|
Sun Microsystems, Inc. Copyright information. All rights reserved. Feedback |
Library | Contents | Previous | Next | Index |