views:

218

answers:

3

Hi All,

I have a bunch of projects that all could share a "common" static library of classes.

What confuses me is if I make a static library out of these classes and link against it in my projects that I still need the headers of the classes in the static library in my main projects.

What is the benefit of the static library then?

How do companies like Adobe deal with this?

+8  A: 

Static libraries allow you to create a library and use that library in many projects.

The need for header files:

Since the project using the library is programmed and compiled independent of the library, that program needs to know the declaration of the things you're using. Otherwise how would your compiler know you're writing valid code?

A compiler only takes source code as input and produces output. It does not deal with compiled object files or static libraries on input.

The need for linking in the library:

So having the headers allows you to write valid code in your project, but when it comes to link time you'll need to provide the definition which is contained inside the static library.

The linker takes all object files (compiled code) and also all static libraries and produces an executable or binary.

More info about static libraries (benefits, comparing dynamic, etc...):

Amongst other things, it is nice to separate your project into libraries so that you don't end up with 1 huge monolithic project.

You do not need to distribute the source code (typically in the .cpp files) this way.

If you were to simply include all the .cpp files in every project that used the common library then you would have to compile the .cpp files each time.

An advantage of static libraries over dynamic libraries is that you can always be sure that your programs will be self contained and that they are using the correct version of the library (since they are compiled into the executable itself). You will also have a slight speed advantage over dynamic linking.

Disadvantages of static libraries over dynamic libraries include that your file sizes will be bigger because each executable needs their own copy, and that you can't swap out a different version of the library since it's not dynamically loaded.

Re your question: How do companies deal with this:

A typical company will make use of both static and dynamic libraries extensively.

Brian R. Bondy
@Brian - Got it. Do you have a reference for Dynamic libraries on OS X?
MLS
Another benefit to either kind of library: reduced compilation time.
WhirlWind
http://developer.apple.com/Mac/library/documentation/DeveloperTools/Conceptual/DynamicLibraries And, if you're talking OS X, you probably want to look into building a framework, which is (basically) a way of packaging together a library and the corresponding headers
David Gelhar
A: 

The typical way you make use of a static library is to have a target in your Makefile (or whatever build system you use) that installs the headers into an appropriate location at the same time that it installs the library.

So, your static library ends up in /usr/local/lib, and the headers go into /usr/local/include or wherever.

Mark Bessey
A: 

Also, when compared with linking against object files, linking against static library may result is a smaller final executable. The reason for this is, if you don't call any of the functions from a particular object file (included in the static library), the linker will not include the code for those functions in you final executable. See http://stackoverflow.com/questions/2631558/extraneous-library-linkage

Jasmeet