Using and Redistributing Sun Studio Libraries in an ApplicationThe Sun Studio software suite provides a number of libraries that can be incorporated into an application to provide functionality and reduce development time. These libraries are redistributable, meaning that they can be freely distributed along with the applications that depend on them. The full list of Sun Studio 12 redistributable libraries and files can be found at http://docs.sun.com/source/820-4155/runtime.libraries.html This article presents the best practices for redistributing these libraries and for maintaining applications that depend on them. The ProblemThere are two ways that an application can be linked to a library:
Dynamic linking is preferred, especially for the libraries supplied by the compiler, as well as for user libraries that are required by the application. This enables better management of updates to the library code. ExampleThis code example uses the vector class from the C++ Standard Library Example 1 — Example of Code Using the C++ Standard LibraryBy default the compiler will use the C++ Standard Library, libCstd, that is provided as part of Solaris. The library is installed in /usr/lib as part of the OS, and hence does not need to be packaged separately. Use the ldd utility to determine which libraries the application will link to, as shown below. Example 2 — Using The Default Standard Library
The developer can decide to use the alternative stlport4 library, which provides better standards conformance, and often better performance, than the default library. This library is not shipped as part of Solaris, but is part of the Sun Studio distribution. The next example shows the same code compiled with the flag -library=stlport4, which tells the compiler to use the stlport4 library instead of the default libCstd. The output from ldd shows that the application links in the library located in the directory that is part of the Sun Studio distribution. Example 3 — Using stlport4
Running this program on another system without the Sun Studio software installed, the linker will not be able to locate the file libstlport.so.1. This situation is shown in the next example with the output from ldd on a system without Sun Studio installed. Example 4 — Example of being unable to locate a library
One workaround for this situation that is too frequently adopted is to set the environment variable LD_LIBRARY_PATH to point to the directory containing the missing library. Although this does work, it is not a recommended fix because it is fragile and requires the user's environment to be correctly set. For example, the library libstlport.so.1 might have been copied into the current directory and LD_LIBRARY_PATH set to "." (dot). This is shown in the next example. But if the application is invoked from any directory other than the current directory, the application will not be able to locate the stlport4 library. Example 5 — The workaround of setting LD_LIBRARY_PATH is not recommended
The LD_LIBRARY_PATH environment variable is useful for special testing, but is not a scalable or maintainable solution for deployed programs. Rod Evans provides a detailed discussion in his blog entry "LD_LIBRARY_PATH - just say no". The Right Way to Distribute Shared LibrariesAvoid using the LD_LIBRARY_PATH environment variable by packaging the application binary along with any additional libraries in a directory structure as shown in the next example. Example 6 — Suggested directory structure for application
In the example, the stlport.so.1 library would be copied into the /lib subdirectory. The compiler flag -library=stlport4 will enable linking the stlport4 library rather than the default library at build time. Compiling with the -R dir option, the linker will locate the library in the application's /lib subdirectory at runtime. Although you could specify an absolute directory path to search for the library, this would restrict the installation to one specific location in the file system, again requiring use of LD_LIBRARY_PATH as an ugly workaround. The better approach is to use the token $ORIGIN with the -R option to tell the application to look in a path relative to the location of the executable. The $ORIGIN token may need special treatment to avoid being interpreted by the shell. This is shown in the next example. Example 7 — Specifying a relative runtime path for a library% CC -library=stlport4 -R'$ORIGIN/../lib' v.cc In a Makefile, an extra $ escape is needed to avoid $ORIGIN being interpreted as a Make variable. This is shown in the next example. Example 8 — Specifying a relative runtime path in a Makefilev: v.cc CC -library=stlport4 -R \$$ORIGIN/../lib v.cc -o v On the target machine this results in the application locating the library in the application's /lib directory, as shown in the next example. Example 9 — Locating a library in a relative directory path
ConclusionsUsing the $ORIGIN token with the -R option to locate the libraries on a path relative to the executable is recommended for the following reasons:
AuthorsSteve Clamage has been at Sun since 1994. He is currently technical lead for the Sun Studio C++ compiler. He has been chair of the ANSI C++ Committee since 1995. Darryl Gove is a senior staff engineer in Compiler Performance Engineering at Sun Microsystems Inc., analyzing and optimizing the performance of applications on current and future UltraSPARC systems. Darryl is also the author of Solaris Application Programming (Prentice Hall 2008). Read Darryl's blog here. |