Subject: Re: Shared libraries.
From: "Gerrit P. Haase" <[email-masked]>
To: Wiebe de Vries <[email-masked]>
Cc: [email-masked]
Date: Tue, 26 Mar 2002 00:12:48 +0100

Hallo Wiebe,

Am 2002-03-25 um 16:12 schriebst du:

> I'm trying to build and use shared libraries with Cygwin under Windows 2000.
> The first step, i.e. building the library, succeeds. I get a libmylib.so
> (together with libmylib.so.1 and limmylib.so.1.0.0). However, linking is a
> problem. I've written a small program which uses libmylib.so and try to link
> it as follows:

>         g++ -o testprogram.exe testprogram.o -L/usr/src/DynLib -lmylib

> This results in the error:

> /usr/lib/gcc-lib/i686-pc-cygwin/2.95.3-5/../../../../i686-pc-cygwin/bin/ld:
> cannot find -lmylib

> For information the entire output when executing make clean:

> rm -f   PrintTest.o  PrintTest.exe.exe PrintTest.d *~
> make
> make[1]: Entering directory `/usr/src/dynlibtest'
> g++ -M  -Wall  PrintTest.cpp > PrintTest.d
> g++ -M  -Wall  PrintTest.cpp | sed s/\\.o/.d/ >> PrintTest.d
> make[1]: Leaving directory `/usr/src/dynlibtest'
> make[1]: Entering directory `/usr/src/dynlibtest'
> g++ -Wall    -c -o PrintTest.o PrintTest.cpp
> g++  -o PrintTest.exe   PrintTest.o  -L/usr/src/DynLib -lmylib
> /usr/lib/gcc-lib/i686-pc-cygwin/2.95.3-5/../../../../i686-pc-cygwin/bin/ld:
> cannot find -lmylib
> collect2: ld returned 1 exit status
> make[1]: *** [PrintTest.exe] Error 1
> make[1]: Leaving directory `/usr/src/dynlibtest'
> make: *** [clean] Error 2

> When using:

>         g++ -o PrintTest.exe PrintTest.o libmylib.so

> I get the error:

>         collect2: ld terminated with signal 11 [Segmentation fault]

> Can someone tell me what I'm doing wrong?

You cannot act with windows like with linux.  On windows shared libs
are dynamic link libraries (mylib.dll), and you cannot use symlinks to
point to a dll.  Say mylib-0.1.dll is you lib and mylib.dll is the
symlink then it is not possible to link against the symlink, windows
doesn't accept this as a library.  Anyway, we don't use the .dll's to
link against, but a special library called import library.
Looks like this (Cygwin default name) libmylib.dll.a, so we can have
also libmylib.a as the static lib.
Now if you link against this lib you write: gcc -o prog -I/lib -lmylib
The linker looks at first for libmylib.dll.a, then if there is no
importlib it looks for a static lib.
I always try to build a static lib at first.
If it succeeds it is just one step to get a dynamic link library from
these objects:
gcc -shared -o cyg${module}.dll -Wl,--out-implib=lib${module}.dll.a \
                  -Wl,--export-all-symbols \
                  -Wl,--enable-auto-import \
                  -Wl,--whole-archive $old_lib \
                  -Wl,--no-whole-archive ${dependency_libs}

The last (-Wl,--no-whole-archive) is important, even if you have no
dependency_libs.  Don't miss it!
${module} is 'mylib' to stay with your example and $old_lib is libmylib.a,
the static lib.  As an alternativ you can list all the objects here instead
of a static lib which is in fact just a bundle of object files.

HTH,

Gerrit
-- 
=^..^=
Maybe Charles will find some time to finish his docu about creating dll's;)

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/