Re: DLLs ... [OFFTOPIC]

DAVID BALAZIC (david.balazic@uni-mb.si)
Fri, 17 Jul 1998 13:15:58 +0100 (MET)


From: Christophe Dupre <cdupre@risq.qc.ca>

>> Kurt Garloff (K.Garloff@ping.de) wrote :
>>
>> >On Thu, Jul 16, 1998 at 12:29:09PM +0530, Somnath Roy wrote:
>> >> Is there any guideline for writing DLLs in Linux ?
>> >
>> >What you want to use are shared libraries in Un*x/Linux land, probably,
>> >like /lib/libc.so ... (so = shared object)
>> >
>> >gcc -fPIC -O2 -c file1.c
>> >gcc -fPIC -O2 -c file2.c
>> >...
>> >gcc -shared -Wl,-soname,libXY.so.1 -o libXY.so.1.0 file1.o file2.o ...
>> >ln -s libXY.so.1.0 libXY.so.1
>> >ln -s libXY.so.1 libXY.so
>> >
>> >Linking to library:
>> >gcc -o executable -L/path/to/lib -lXY exec1.o exec2.o ...
>>
>> Is -fPIC neccesary ?
>
>Yes, -fPIc tells the compiler to generate Position Independant Code, that is
>code that can be dynamically linked with an application. Without that switch,
>your library may work, but there's no guaranty.
>
>> Do lib*.so files have no reloc tables ?
>
>The compiler and linker will create all necessary tables in the library file.

So if it is fully relocable (?) , why does it need to be PIC ?

>> I compiled once a shared library without -fPIC and it didn't give me no troubles.
>
>Must have been luck.
>
>> And another question :
>>
>> How to make a shared library to contain a statical library ?
>
>That's not possible AFAIK, nor necessarily a good idea. Usually, you'll want
>to link to dynamic libraries in order to reduce the disk footprint, as well as
>the memory footprint of your applications. If you need a static library, then
>you will usually link that library to the final executable, not to another
>library.
>If you REALLY think linking the static lib into another dynamic lib is
>necessary, then you may extract the object files from the static lib, and add
>them to your dynamic library. "ar" is used to create/extract object files from
>a static library.
>
>
>> Like : gcc superlib1.o superlib2.o -lmytools -shared -o libsuper.so
>>
>> I want the libmytools.a code to be part of libsuper.so ( that is : statically linked to it )
>>
>> egcs 1.03 defaults to dynamic link , even if only the static ( libmytools.a ) version is avaliable.
>
>It's not the compiler that does linking, but the linker (ld). You can force a static exec by using the "-static" flag.

OK , I'will describe what my problem was.

I ( my friend actually ) was writing a program in java that sniffs the network.
The simplest solution seemed to be the use of the pcap library.
So he wrote a that function in C and called it from java.
To be able to do that , the C code must be saved as a dynamic library,
let's call it libsniff.so.

So it is obvious that it must be compiled with the -shared switch.
Because it uses functions from libpcap I must also add -lpcap.
So the commands are :

cc sniff.c -c
cc -lpcap sniff.o -shared -o libsniff.so

The generated code ( libsniff.so ) is _dynamically_ linked to libpcap.so even
if I don't have that file ! I only have libpcap.a !

The reason I want to link it statically is the same reason other people link things
statically ( netscape for example ) , to remove the depencancy to a file
which may not be installed on the users system.

--
David Balazic , student
E-mail   : 1stein@writeme.com     |     living in  sLOVEnija
home page: http://surf.to/stein
Computer: Amiga 1200 + Quantum LPS-340AT
--

- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.rutgers.edu Please read the FAQ at http://www.altern.org/andrebalsa/doc/lkml-faq.html