Friday, June 26, 2009

Cygwin/MinGW: Convert a Static Library to Shared Library

As I have used MinGW and Cygwin to compile GNU/Linux software in Windows, sometimes I only get static libraries, but not a shared library. After some research, I found several ways to convert a static library to a shared library. Using GCC with the -shared parameter is one way. libtool is another tool for such a task. In this post, I will write about dllwrap because that's what I have used and known to work.

Using dllwrap

Suppose the name of the static library is libpango-1.0.a This library is just an archive of many object files. Running “ar t libpango-1.0.a” will display the contents of the archive.

ar t libpango-1.0.a

Now, we have to extract the contents of the archive into an empty folder. Create a new folder.

mkdir temp

Change to the newly created folder and extract the static library.

cd temp
ar x libpango-1.0.a

Use dllwrap to create a shared library from the object files in the following manner:

dllwrap --verbose --export-all-symbols --add-stdcall-alias \
-o /usr/bin/cygpango-1.0-0.dll --dllname cygpango-1.0-0.dll \
--output-lib /usr/lib/libpango-1.0.dll.a *.o -L/usr/lib \
-lgobject-2.0 -lgmodule-2.0 -lglib-2.0 -luser32 -lkernel32 -lintl -liconv

This creates a shared library cygpango-1.0-0.dll and an import library libpango-1.0.dll.a. To learn more about dllwrap, read the output of “dllwrap --help

Using GCC to convert a static library to shared library

You can also use GCC to do a library conversion with GCC. Here's an example. It's certainly a lot of typing:

ar x /usr/lib/libpango-1.0.a gcc -shared -o libpango-1.0-0.dll -Wl,--out-implib=libpango-1.0.dll.a \
-Wl,--export-all-symbols -Wl,--add-stdcall-alias -Wl,--enable-auto-import -Wl,--enable-runtime-pseudo-reloc \
*.o -L/usr/lib \
-lgobject-2.0 -lgmodule-2.0 -lglib-2.0 -luser32 -lkernel32 -lintl -liconv

This creates the same set of files as with dllwrap above, but with slightly different sizes. For more information, read the GCC manual and the output from ld --help

Using a2dll to Convert a Static Library

a2dll is a little tool from mingw-utils that can be used to convert a static library to a dynamic library. For example,

a2dll libmp3lame.a -o lame_enc.dll

About This Blog

KBlog logo This blog seeks to provide useful information to people, based on the author's knowledge and experience. Thanks for visiting the blog and posting your comments.

© Contents by KBlog

© Blogger template by Emporium Digital 2008

Follow by Email

Total Pageviews