Sign in to follow this  

C++ Librarys - When to use .lib vs including .h/.cpp

This topic is 475 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

What's the difference between distributing your library as a .lib vs just including the source files in your project? I have a fairly small library with no external dependencies (apart from STL) and wondered what the difference was? I used to think it'd be that .libs have a compilation speed advantage but dont modern compilers overcome this by not recompiling unchanged code? If they have similar performance because of that, it just seems simpler to have a user include the code as part of their project and be done with it; no setting of library directories etc and they'd only be including the header files anyway

 

Am I missing any perks of .libs? 

Share this post


Link to post
Share on other sites

Distributing reusable code as a library is usually advantageous because

 

  • it is fewer files for the consumer to wrangle (just one library file and one header at the minimum)
  • it is faster to compile, because it's already compiled, so the consumer's compiler does not need to recompile it ever
  • it means you are not exposing your source code directly, which is advantageous if you don't want consumers to have direct access to the code for whatever reason

Naturally you can eliminate these advantages if you handle the building and distribution of the library poorly (such as not collapsing your public headers to as few files as possible), and some of these advantages may not matter to you (maybe your project is open source, so keeping the code private is not a concern). Yes, consumers do need to configure library and header search paths, but that's generally assumed to be a basic cost-of-doing-business and isn't really that much harder than adding multiple external source files to a project.

 

 

Just distributing the source directly means you don't have to deal with all the various build configurations needed to ship the appropriate variants of the library for all supported platforms, so that's more advantageous to you. However you are sometimes taking that savings at the expense of advantages for the user (or at least, at the cost of creating potential disadvantages). For example, if your code doesn't compile cleanly on a client's machine because they used a stricter warning level, that's now something they have to manage or work around themselves.

 

Generally I find source-only distribution to be worthwhile if you have a single-header or maybe a single-header/single-source pair. But generally if there's more than one file involved, or any source files, I'd prefer a library and will usually build such source-only projects into a library myself before linking them with any of my projects.

Share this post


Link to post
Share on other sites

Most source bases need to be configured for each build target, so a library has that already done. When build process is somewhat complex you always prefer a pre-build binary. Of course if you do not have the pre-build binary for your compiler/operative system you go nowhere, while with source code you can always compile and eventually fix compilation problems "on the fly".

Share this post


Link to post
Share on other sites

Most source bases need to be configured for each build target, so a library has that already done. When build process is somewhat complex you always prefer a pre-build binary. Of course if you do not have the pre-build binary for your compiler/operative system you go nowhere, while with source code you can always compile and eventually fix compilation problems "on the fly".

 

I don't prefer pre-built binaries. I've had too many time-consuming bugs from the libraries being built with different build targets and compiler settings than the executables. Now I just by default want to build everything from source, because it makes my life simpler.

Share this post


Link to post
Share on other sites

as i recall:

 

a lib is one or more objs files.

 

an .obj/.lib file avoids compilation. but with compilation times near zero these days, its not much use. caveman compiles 300-400K lines in like 10-30 seconds, then takes 2+ minutes to link.

 

releasing as obj/lib, you don't have to release your source code. but you do have the issue of needing matching build settings (target and compiler options). its also possible that some optimizations won't apply to obj code (such as compile time linking and whole program optimization). i think the entire lib gets linked, and optimizations stop at the module API level.

 

releasing source, you lose source control, but avoid the issues with matching settings and optimizations.

 

commercial libraries are often released as lib and obj files, sometimes with source for an additional (large) fee.

 

non-commercial libraries are often released with full source when source control is not an issue.

Edited by Norman Barrows

Share this post


Link to post
Share on other sites
My share;
I personally like to use libs to keep my project "clean", instead of all interfaces/implementations. With the remark that the functionality should be "finished enough", otherwise making changes becomes time consuming.

Share this post


Link to post
Share on other sites

This topic is 475 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this