< Previous PageNext Page > Hide TOC

Overview of the C++ Runtime Environment

The C++ runtime environment has evolved over the course of Mac OS X development. Early versions of the library were shipped as a static archive file while the latest version is delivered as a dynamic shared library. The following is a summary of the history of the runtime:

The following sections provide more detailed information about the dynamic and static C++ runtimes.

Contents:

Targeting Mac OS X v10.3.8 and Earlier
Targeting Mac OS X v10.3.9 and Later


Targeting Mac OS X v10.3.8 and Earlier

In Mac OS X v10.3.8 and earlier, the standard C++ library is packaged as the static archive file libstdc++.a. This library is designed with multiple clients in mind and provides syncing of I/O streams as well as hidden symbols to alleviate namespace conflicts. Symbols in the library are marked __private_extern__ to prevent them from being exported to other code modules. Versions of this library are available in all versions of Mac OS X but programs must be built using the GCC 3.3 compiler.

Targeting Mac OS X v10.3.9 and Later

In Mac OS X v10.3.9 and later, you have the option of linking against a static or dynamic version of the C++ runtime.

The Static C++ Runtime

With the introduction of Xcode Tools 2.3, a new version of the standard C++ library is made available in the static archive file libstdc++-static.a. This new static library is closer in nature to the dynamic shared library introduced in Mac OS X v10.3.9 than to the previous static library. It conforms to the Itanium C++ ABI and requires the use of GCC 4.0 for compilation. Programs linking to the library must run in Mac OS X v10.3.9 or later. For more information, see “Deploying With the New Static Runtime.”

The Dynamic C++ Runtime

In Mac OS X v10.3.9 and later, the C++ runtime is available as a dynamic shared library libstdc++.dylib. This change in packaging brings the C++ runtime in line with the C runtime, which has always been packaged as part of the dynamic shared library libSystem.dylib. The dynamic library conforms to the Itanium C++ ABI, which is a standard for compiled C++ code that provides better link-compatibility between C++ binaries. Because it is shared, the namespace limitations present with static versions of the library are gone and symbols are no longer marked __private_extern__. For information about designing and using C++–based dynamic libraries, see Dynamic Library Programming Topics.

Note: To build programs that link to libstdc++.dylib, you must have GCC 4.0, which is provided with Xcode Tools in Mac OS X v10.4. You use this compiler along with the SDKs provided by Apple to build your binary for 10.3.9. For more information, see “Deploying Applications With the C++ Runtime.”

Advantages of a Shared C++ Runtime

Packaging the standard C++ library as a dynamic shared library is important for ensuring performance and correctness of your program. If your executable links to libstdc++.a and to several dynamic shared libraries, and one or more of those shared libraries also links to libstdc++.a, then multiple copies of the library are loaded into memory at runtime.

For correctness, all components of a program should use the exact same copy of the standard C++ library. The reason is that some data in the C++ runtime must be shared by all components or unexpected results may occur. For example, if two components both use the C++ I/O mechanism to write to standard output, the results can become garbled if they use different buffers.

For performance, having multiple copies of the same library leads to increased disk activity as each copy is read into memory. The extra copies can also lead to increased paging and cache misses due to the increased memory footprint of the application. Eliminating the duplicated libraries can reduce the footprint of your application dramatically. For example, consider the following “hello world” program compiled with libstdc++.a and GCC 3.3:

int main ()
{
    std::cout << "Hello, World!\n" << std::endl;
    return 0;
}

The size of the resulting binary on a Mac OS X v10.4 system is 650 KB. The same program compiled on the same system using GCC 4.0 and libstdc++.dylib is 17 KB.

Binary Compatibility

GCC 4.0 conforms to the Itanium C++ ABI, which is a standard for compiled C++ code. The specifications for this standard are maintained by a multi-vendor consortium and cover issues such as name mangling, class member layout, virtual method invocation protocols, exception handling, and runtime type information (RTTI) formats. You can find the latest version of this specification at http://www.codesourcery.com/cxx-abi/abi.html.

Because GCC 4.0 conforms to the Itanium C++ ABI, C++ objects are link-compatible with objects built by other Mac OS X compilers that conform to this specification. Apple guarantees that future releases of GCC for Mac OS X will also conform to the Itanium C++ ABI. This means that developers may safely ship dynamic shared libraries whose interfaces involve C++ classes, albeit with some caveats:

If you are designing a dynamic shared library for distribution, it is still your responsibility to ensure that you do not create binary compatibility problems. For example, you should not introduce member variables or virtual methods to a base class. Doing so causes a fragile base class problem and requires clients of the library to be recompiled.

For more information on binary compatibility, see “Creating Compatible Libraries.”



< Previous PageNext Page > Hide TOC


© 2005, 2006 Apple Computer, Inc. All Rights Reserved. (Last updated: 2006-06-28)


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.