4

I'm working with a MCU in C and I have an already-made library I want to use to build my own application. Where's the difference between:

  1. compiling the library files in a .lib and then link my application (which uses that library) to that .lib, and compile it to my target
  2. just creating one single project in which I import all the files (.c and .h) and compile it to my target

This is a very basic and general question, but I would like to clear it up..

stef
  • 737
  • 4
  • 13
  • 31
  • I understand that you are asking the difference between static and dynamic libraries. See [this answer](http://stackoverflow.com/questions/3651944/difference-between-static-and-dynamic-library-in-unix/3651970#3651970) for instance. – Simon Feb 15 '11 at 22:39

2 Answers2

5

EDIT: Answer is assuming you are asking from a software engineering point-of-view.

Having a library file separate from your application code represents a best practice approach. Aside from static and dynamic linking of library files and applications, your library file should house logically / semantically related code. This structuring "componentizes" your code into reusable pieces of software, which can be fed into other libraries and applications. This practice promotes loose coupling and is a preferred method of design and implementation of software.

With a single project approach, you will still have the same object files that are compiled when needed (as in the separate library approach). Only those files altered will have their object files re-generated accordingly. However, you will still end up with an entirely new library / application file.

As an example benefit of using the first approach (number 1), you can layer a separate "test" library to unit-test your standalone library, all while not having to re-include or re-build your standalone library. You exercise the standalone library while making the modifications to your test library (e.g., adding, modifying, deleting unit-tests).

Hope this helps!

Michael
  • 786
  • 8
  • 18
  • One more question: I implement some functions in a library. Then use that compiled library in my application together with (linked to) a new version of some of the functions implemented in the library, this new version is going to replace the older one in the library, isn't it? – stef Feb 16 '11 at 15:18
  • Before answering, does the new library serve as a replacement for the older library or is the new library another "component" that provides different / newer functionality? If the newer library is just an updated version of the older library, then you should only need the newer library (assuming all dependent applications are aware of the change (i.g., static vs. dynamic builds)). If the new library is additional new / different functionality to be included in the application, then both libraries would be included. Just logically separate out components into corresponding libraries. – Michael Feb 17 '11 at 16:51
2

Linking in the library can reduces build time...especially if there are a large number of source files to compile. This is probably not a big deal if you have a good build system though. This method also give you a nice modularized component that can be reused with a higher level of confidence on other projects or even future builds because you know it is not changing.

Building with the library source all the time may make debugging easier as you can step through and set breakpoints in the library source. Even if you assume the library code has no issues, it can still be helpful to set breakpoints in the library source when debugging the rest of your application.

semaj
  • 1,555
  • 1
  • 12
  • 25