• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 276
  • Last Modified:

for mliberi

Q.10218423
0
ahoffmann
Asked:
ahoffmann
  • 2
1 Solution
 
mliberiCommented:
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 */
func1
func2
func3

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.

Procedure

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.
0
 
ahoffmannAuthor Commented:
give me a email address, and I have a goody too ;-)
0
 
mliberiCommented:
mliberi@hotmail.com

you you like unix utilities you can also visit my site at URL http://www.geocities.com/SiliconValley/Horizon/5745
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now