for mliberi

LVL 51
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

same reply. thanks.

How to Create a Shared Library

Prerequisite Tasks

1.     Create one or more source files that are to be compiled and linked to create a shared library. These files contain the export symbols that are referenced in other source files.

For the examples in this article, two source files, shared1.c and shared2.c, are used. The shared1.c file contains the following code:

 * shared1.c: shared library source.

#include <stdio.h>

void func1 ()
  printf("func1 calledn");

void func2 ()
  printf("func2 calledn");

The shared2.c file contains the following code:

 * shared2.c: shared library source.

void func3 ()
  printf("func3 calledn");

The export symbols in these files are func1, func2, and func3.

2.     Create a main source file which references the export symbols that will be contained in the shared library.

For the examples in this article the main source file named main.c is used. The main.c file contains the following code:

 * main.c: contains references to symbols defined
 * in shared1.c and shared2.c

#include <stdio.h>

extern void func1 (), func2 (), func3 ();
main ()
  func1 ();
  func2 ();
  func3 ();

3.     Create the exports file necessary to explicitly export the symbols in the shared library that are referenced by other object modules.

For the examples in this article, an exports file named shrsub.exp is used. The shrsub.exp file contains the following code:

#! /home/sharelib/shrsub.o
*/* Above is full pathname to shared library object file */

Note:  No comments should come before the first line in an exports file.

The #! line is meaningful only in the case where the file is being used as an imports file, that is, the file is used instead of the shared library file during the linking process. In this case, the #! line identifies the name of the shared library file to be used at run time.


1.     Compile and link the two source code files to be shared. (This procedure assumes you are in the /home/sharedlib directory.) To compile and link the source files, enter the following commands:

cc -c shared1.c
cc -c shared2.c
cc -o shrsub.o shared1.o shared2.o -bE:shrsub.exp -bM:SRE -e _nostart

This creates a shared library name shrsub.o in the /home/sharedlib directory. The -bM:SRE flag marks the resultant object file shrsub.o as a shared re-entrant. Each process that uses the shared code gets a private copy of the data in its process private area.

The -e _nostart flag sets the dummy entry point _nostart in order to override the default entry point , __start.

2.     Compile and link the main source code with the shared library to create the executable file. Use the following command line:

cc -o main main.c shrsub.o -L/home/sharedlib

The program main is now executable. The func1, func2, and func3 symbols have been marked for deferred resolution. At run time, the system loader
will load the shared library in the shared library memory segment (unless the library is already loaded) and dynamically resolve the references.

The -L flag adds this directory (/home/sharedlib) to the library search path. This option tells the loader at runtime where to find shrsub.o. The environment variable LIBPATH can also be used to include additional directories to the library search path.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ahoffmannAuthor Commented:
give me a email address, and I have a goody too ;-)

you you like unix utilities you can also visit my site at URL
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Unix OS

From novice to tech pro — start learning today.