[lug] q: building libraries

Aaron Crane aaron.crane at pobox.com
Mon May 22 15:07:07 MDT 2000


Chan Kar Heng <khchan at cyberdude.com> writes:
> anyone has pointers or details on how to create libraries from .c files?
> (i refer to libraries like libpthreads.so and stuff like that)
> 
> don't think i have a problem with the contents of the .c file, but what
> utilities do i use on them to make them into libraries that i've
> described? i managed to make .a files, but my .c files that call functions
> in them end up having contents of those .a files linked in. in short, i
> want to make dynamic shared libraries.
> 
> checked out info a bit on these tools... should i refer further on ar?

The ar(1) utility is only capable of building static libraries -- that is,
libraries where the code is linked statically into your executable.
(Actually, all it does is create and manipulate archives (.a files); the
linker knows how to extract code from such archives and put it in your
program.)

If you want to build shared libraries, the basic method is something like
this:

    gcc -fPIC -c shared-code.c
    gcc -shared -o libshared.so shared-code.o

I used to know a lot more of the details, but it never seemed worth
remembering them, because of...

> or libtool?

This is the one I recommend.  IMHO, it's easily the best thing if you want
to be able to build shared libraries on diverse platforms, and the command
syntax is much easier to remember than all the random system-specific gunk.
Plus, it hooks into autoconf and automake really easily, if you like that
sort of thing.

If you decide against Libtool, then the FAQ for comp.unix.programmer has
some useful material in it on building shared libraries for various
platforms.

> how about dlltool? it's available on gcc for win32 but are
> the output files usable in linux?

I've never heard of dlltool, but the name suggests that it builds either
Windows shared libraries (.DLL files) or antediluvian (pre-ELF) Linux shared
libraries.  If it's available for win32, then it's almost certainly the
former, in which case I don't think it's what you want.

-- 
Aaron Crane   <aaron.crane at pobox.com>   <URL:http://pobox.com/~aaronc/>




More information about the LUG mailing list