views:

6959

answers:

6

Guys, would you describe a few things about c++ building blocks, on unix.

I want to create an application that links against static libs and dynamic libs (.so).

Question 1: How do I create static library using gcc/g++ ?How do I make my app link against it.

Question 2: How to specify it in the makefile, linking against static and dynamic libs, assuming that both libraries have header files

Summary: I have been using makefiles and libraries for years, written by someone else. Thus every time I modified it, I simply cut-and-pasted things around, without really understanding it. Now I want to get down to the ground and understand the building/linking/Creating Makfile process in-depth.

What is a good book describing these concepts in intimate details?

Thanks

+3  A: 

Static libraries are usually archived with the ar command. Once you build all of the object files (preferably with the -fPIC switch on GCC), you can run ar like so:

ar -rs archivename.a list.o of.o objects.o

The man page describes the options.

Dynamic libraries are built usually with the -shared switch to gcc or ld and the output file name with a .so extension.

Autotools handles this with the libtool program. I'm not familiar with its use.

Linking against these libraries can be done either by listing the libraries with the -l (ell) switch (such as -lX to link to libX.so) or by specifying them directly with absolute paths (such as adding /usr/lib/libX.so to your command). Static libraries are linked by specifying -static before -l or the appropriate absolute path to the .a archive.

greyfade
+4  A: 

Bare bones Makefile for creating a static library consisting of the code in foo.cpp, bar.cpp:

PROJECT = library.a
OBJECTS = foo.o bar.o
CFLAGS  = -Wall -pedantic

all: $(PROJECT)

.cpp.o:
        g++ -c $(CFLAGS) $<

$(PROJECT): $(OBJECTS)
        libtool -o $(PROJECT) -static $(OBJECTS)

Bare bones Makefile for an app baz.cpp that static links to library.a:

PROJECT = baz
CFLAGS  = -Wall -pedantic
OBJECTS = baz.o

all: $(PROJECT)

.cpp.o:
        g++ -c $(CFLAGS) $<

$(PROJECT): $(OBJECTS) library.a
        g++ $(OBJECTS) -L. -llibrary -o $(PROJECT)

Dynamic library left, ahem, as an exercise to the reader.

Paul Beckingham
What is -c flag in gcc. What about header file?
also, does $< mean, all cpp files? why do u need this case .cpp.o: g++ -c $(CFLAGS) $< it doesn't see to be called anywhere? Thanks
-c means compile but do not link. $< is the file name without the extention. Check the man pages for gcc, make. Header files have no place in a Makefile, unless they are a dependency.
Paul Beckingham
In the first Makefile example one of the CFLAGS options is misspelt "-pendantic" should be "-pedantic"
Peter
Thanks @Peter, typo corrected.
Paul Beckingham
+1  A: 

Answer 1: To create a static library from source files foo.c and bar.c, do this:

gcc -c foo.c
gcc -c bar.c
ar rc mylibrary.a foo.o bar.o

For more information about this, read the GCC manual manual to learn how to use the compiler, and the linker via the compiler. The binutils manual should also be helpful.

Answer 2: The GNU Make manual is pretty good. To really learn about libraries and how they work, read the Linkers and Loaders book by John R. Levine.

Static libraries are pretty simple, but shared libraries can be very hairy, depending on the platform and the amount of portability you want and need. As an example, on some systems static and shared libraries must be compiled with different options to work properly (one must and the other must not be compiled with position independent code). Whole frameworks of utilities have been developed to make this easier (libtool), but they are not unproblematic themselves.

Lars Wirzenius
A: 

Since you refer to gcc, I assume you're using GNU Make. The best documentation I've found for that is the official manual, which covers everything you need to know in easy-to-understand terms.

Christoffer
A: 

When I was learning about Linux programming, Advanced Linux Programming helped me a lot. You can check Writing and Using Libraries section in this pdf. It explains quite a bit about libraries in Linux.

aks
That's a very good book, but I prefer something in conjunction with makefiles.
A: 

Hi, you may want to have a look to this, too: http://www.faqs.org/docs/Linux-HOWTO/Program-Library-HOWTO.html