3

Say my library is just a couple functions that neatly fit into 1 file and do not require any external dependencies. Is then there any advantage in compiling that library into an .o object file and distributing it that way rather than just providing it as a header file? I can't seem to think of any, though I'm just a beginner.

And if there is advantage in using an object file, is there any reason to package that single object file into an archive (.a), rather than distributing the object file by itself?

3 Answers 3

3

For a small library like this there really is no advantage in implementing it in a .o file - you have to supply a header as well anyway. For larger libraries things become less obvious - linking object code is usually faster than compiling large amounts of C++ text, which you then have to link anyway, but on the other hand header-only files are somewhat more convenient to use and to distribute.

Sign up to request clarification or add additional context in comments.

Comments

2

The only "advantage" would be if you don't want to give your clients access to your source code implementation but just want to provide a header with function prototypes + a binary object.

If you are fine with clients seeing your implementation, then a header-only library can be a good solution.

Comments

2

The difference is (single object file or multiple ones), that the library linking mechanism allows you to specify a path where these should be found automatically by the linker, while you can't do that for single object files.

Thus, it doesn't matter if your library contains only one object file or more.
Providing a library will be the correct way.

and distributing it that way rather than just providing it as a header file?

If you can provide all implementation in a single header that's the preferable choice though.

3 Comments

"Providing a library will be the correct way." - tell that to Boost, most of who's libraries are header-only.
@Neil Noticed. Header only is most preferable of course. I reffered to single object files.
"If you can provide all implementation in a single header that's the preferable choice" assuming you don't mind clients/users having access to your source code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.