10

While it is commonplace to combine multiple object files in a library, it is possible (at least in Linux) to combine multiple object files into another object file.

(See combine two GCC compiled .o object files into a third .o file)

As there are downsides to using libraries instead of just combined object files:

1: It's easier to work with only one type of file (object) when linking, especially if all files do the same thing.

2: When linking (At least in GCC), libraries (by default) need to be ordered and can't handle cyclic dependencies.

I want to know what advantages there are to libraries (apart from the catch 22 that they're used lots).

After searching for a while, the only explanation I get seems to be that single libraries are better than multiple object files.

5
  • 1
    First you need to clarify what you mean by "libraries". Do you mean shared objects (foo.so), or are you talking about archives (foo.a) ? gcc and gnu ld handle circular dependencies with .a files just fine, you just have to specify each .a file multiple times.
    – George
    Jun 5, 2011 at 13:15
  • @George "gnu ld handle circular dependencies with .a files just fine, you just have to specify each .a file multiple times" - that is an interesting definition of "just fine".
    – user2100815
    Jun 5, 2011 at 13:19
  • @Neil Butterworth Well, it's better than saying treating all of the symbols as being multiply defined when specifying each library file more than once. If I remember correctly, that's the 'recommended' approach for dealing with cyclic dependencies between .a files.
    – George
    Jun 5, 2011 at 13:33
  • In GCC, using -Wl,-( <LIBRARIES> -Wl,-) makes the linker repeatedly cycle through <LIBRARIES> until all required symbols that can be resolved are resolved. (IIRC)
    – Simon
    Jun 5, 2011 at 13:44

4 Answers 4

11

While it depends on the linker being used, object files are being included in the final binary in their entirety. So, if you combine several object files into one object file, then the resulting (combined) object file is included in the resultant binary.

In contrast, a library is just that, a library of object files. The linker will only pull the object files from the library that it needs to resolve all the symbolic links. If an object file (in the library) is not needed, then it is not included int the binary.

4

Generally library is better since linker can optimize out unused .o files in library.

Combining the files somehow has some advantages too:

  • If you combine all your source files into one then that tremendously increases compilation speed.
  • Sometimes in C++ the linker may optimize out .o file that you did not want to be optimized out. For example when you need side-effects of constructors of objects defined there but not used in any other translation unit.
3
  • "Combining all your source files into one [...] tremendously increases compilation speed." Really? When you have to recompile everything anyway, this may be true. When you are iterating changes on a single source file in a project with thousands of source files? Not likely.
    – andrewdski
    Jun 5, 2011 at 16:23
  • @andrewski: That was said in context of compiling a library, not a whole project of thousands source files. When you are designing the library then it is indeed easier to work with separate files. If the classes in library are tightly coupled with each other then the linker can not optimize single .o out. Merging source files is not that hard and putting them together into a single file severely speeds up compilation of that library for users of the library.
    – Öö Tiib
    Jun 5, 2011 at 23:33
  • whefs library proves by example that compilation of the amalgamation (combined source files) is significantly faster than compilation of the individual source files.
    – user
    Dec 16, 2014 at 17:42
1

One reason is that objects in a .a library will only be pulled in to satisfy undefined symbol references - so if you want to allow the possibility for the calling application to define a symbol, or use a default definition in the "library" code, it's possible to do this with a real library but not with a single .o file.

0

If you use object files, then all the code in the object file is placed in your application. If you use libraries, then only the required code is.

7
  • Ok thanks. A better question might then be why not always use library files, and just -ar every object as it's made (except for objects that contain unreferenced elements specifically for runtime linking).
    – Simon
    Jun 5, 2011 at 13:38
  • @Simon You want to use .o files for compoments that are part of your particular project (because if they are not used, why did you write them in the first place) and .a files for things that might be part of the project. For example my own utility library contains some ODBC code, but few of the applications I write use it, and I don't want it linked in with those that don't.
    – user2100815
    Jun 5, 2011 at 13:41
  • That's normal, sure. But why? Why use .o and .a if you can just use .a. Even if it's unlikely you'd have many symbols in your project that are unreferenced. (The GCC source has some legacy ones).
    – Simon
    Jun 5, 2011 at 14:20
  • @Simon sorry, I don't think I can make my explanation clearer.
    – user2100815
    Jun 5, 2011 at 14:24
  • Your explanation gives a reason for using .a and a rule for using .o. It doesn't give a reason for the rule. Basically, what would be wrong (other than it being a convention) with having your ODBC as .a and your project also as .a (The advantage being you only need to work with 1 type of file). - Not trying to be argumentative, just trying to clear this up.
    – Simon
    Jun 5, 2011 at 14:35

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.