Object file (.o) vs header file for one file library?

Say my library is just a couple of functions that fit neatly into 1 file and don't require any external dependencies. Is there any advantage to compiling this library into an .o object file and distributing it that way, rather than just as a header file? I don't seem to think about anything, although I'm just a beginner.

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

+3


source to share


3 answers


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



+3


source


The difference is (single object file or multiple) that the linking mechanism of the library allows you to specify the path where they should be found automatically by the linker, while you cannot do this for single object files.

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



and distribute it that way instead of just providing it as a header file?

If you can provide the whole implementation in one header, which is preferable nonetheless.

+2


source


The only "advantage" is that you don't want to give your clients access to the source code implementation, but you just want to provide the function prototype header + blob.

If you feel good when clients see your implementation, then just a header-only library might be a good solution.

+2


source







All Articles