12

What are the general differences between compiling a program as a static library vs. including the source code into the program?

i.e. A program with functions that is compiled as a static library (.lib) and linked into the program vs A program with functions that is included as a source file in the main program.

Static libraries more suitable for release when releasing closed source programs? Faster compilation? etc..

Mat
  • 202,337
  • 40
  • 393
  • 406
user3647412
  • 129
  • 1
  • 5
  • Depending how good your debugger is - it may have trouble stepping into the static library – M.M May 17 '14 at 11:37

4 Answers4

7

I'd advocate inclusion of source code because:

  1. Static library is more architecture depending than source code. You will need to compile it again and again.
  2. Optionally, source code allows more optimization than linking with a pre-compiled library.
  3. When examining the program it is always better to have the possibility to see the source code than just a prototype.
Marian
  • 7,402
  • 2
  • 22
  • 34
2

As an already compiled executable file, basically there is no difference between them. I think the point is your purpose of software engineering.

If you are a component developer, static link libraries can keep your valuable source code secret. If you are making source code as business, it is probably very important.

Meanwhile, if you want people use your source code on any platform, you may want to release your modules as source code, as open source developers are doing.

0

The compilation would be faster when using a library, as you won't need to compile its code, but only to link it. But the most important gain turns around software architecture : making a library will offer you a way to write code that will be easier to reuse in future application. From Wikipedia :

The value of a library is the reuse of the behavior. When a program invokes a library, it gains the behavior implemented inside that library without having to implement that behavior itself. Libraries encourage the sharing of code in a modular fashion, and ease the distribution of the code.

slaadvak
  • 4,611
  • 1
  • 24
  • 34
  • The question is different. It's not about how you should write code, it is about whether you should put the source code directly into your project. If you go with the latter, you can write code in exactly the same manner as you would write a library (therefore maintaining the benefits you have just mentioned). Think about it this way: what if I take some open-source library and just smash it into my project? – Aleks-Daniel Jakimenko-A. Mar 15 '16 at 03:33
-1

Its generally a good idea to isolate piece of your software and convert it to a .lib or .dll file(or which ever suits your OS and your Executable Formats){No Repetition}.

Also, including the source code directly into your source file, makes the application bulky and, updating only small portion of code would require a rebuild of your executable and if you want your users to download and use the app, your users will not be happy with downloading the whole application file again.

Thirdly, as user3647458 said, if you are making money selling component code, then .lib file is what you would like to give your libraries off as(possibly for free).

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78