[Subject Prev][Subject Next][Thread Prev][Thread Next][Subject Index][Thread Index]

Re: [LI] how to implement shared objects??



Hi

  Shared objects can actually shared libraries.

1)How to create them?

Say you have three files a1.c,a2.c,a3.c, which you want to put in shared
libraries.

$ gcc -c a1.c  //This will produce a1.o, object file
$ gcc -c a2.c
$ gcc -c a3.c
$ ld -shared -soname mylib -o mylib.so a1.o a2.o a3.o

This will produce a library named as mylib.so. -shared flag tells linker
to create shared onjects. -soname flag sets internal name field of the
library. When you link the library with -lmylib, it searches this name
field rather than the filename. As usual -o flag sets the name of the
output file. .so is conventional extension for shared objects. Optionally
version is also included in the name. Do a 'ls -al' in /usr/lib for
examples.

2)To use library.
Say you have split the source files of your library in .h and .c format.
You create shared library as described above. Now while writing the
application program, include those .h file in your program. This is
required by the compiler to find the function and variable declaration.
Compile the file as follows.

$ gcc myapplication.c -o myapplication -lmylib

Here -l is to tell the linker to link mylib. Actually mylib is additional
place to look for object code that matches the decleration in the code.

Typically libraries are placed in /usr/lib and /usr/local/lib. The include
files are placed in /usr/include and /usr/local/include.

See 'man gcc' for -I and -L flags, for how to set those directories as you
wish.

Please correct me if I am wrong.

Hope this helps.

 Bye
  Shridhar


On Tue, 11 Jan 2000, Guruprasad, Mahendrakar  V (IE10) wrote:

> 
> 
> > 	Hi Everybody,
> > 		I heard that just like DLLs in windows , we have shared
> > objects (.so files) concept in Linux.
> > 		Will please anybody  give me the information (how to write
> > and all)
> > 		about the same?
> > 		I am also interested in knowing how the .so files can be
> > loaded dynamically & if not needed should be unloaded from memory at run
> > time.
> > 
> > 	Thanks in advance
> > 	 
> > 	Regards
> > 	GURUPRASAD V.M.

--------------------------------------------------------------------
The Linux India Mailing List Archives are now available.  Please search
the archive at http://lists.linux-india.org/ before posting your question
to avoid repetition and save bandwidth.