开发者

Difference between compiling a library and just use its source files in an application

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..


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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜