Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

static library, but I still need headers?

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?

like image 306
MLS Avatar asked Apr 10 '10 02:04

MLS


People also ask

Does a static library need headers?

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.

Do you always need a header file?

The creation of header files are needed generally while writing large C programs so that the modules can share the function definitions, prototypes etc. Function and type declarations, global variables, structure declarations and in some cases, inline functions; definitions which need to be centralized in one file.

Do you always need a header file C++?

The answer is that C++ doesn't "need" this. If you mark everything inline (which is automatic anyway for member functions defined in a class definition), then there is no need for the separation. You can just define everything in the header files.

Are libraries and header files same?

No. Header File is the file where all the headers name are mentioned that going to be used or consumed in the main code file. On other hand Library is the file where the implementation code of each header is written down which is mentioned in the Header file.


1 Answers

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.

like image 110
Brian R. Bondy Avatar answered Sep 18 '22 15:09

Brian R. Bondy