Search code examples
clinkerobject-files

Are static libraries version independent?


Say you're using C11, and you need to use a library (either static or dynamic) written in C17. Can you compile the library to object files, and then just link those with the ones of your program? I mean, object files are just executable files (which are in machine-code, or binary?), except you need to link them before they're executable. Anything crazy in what I just wrote?

By the way, is an object file without any dependencies executable?


Solution

  • For one routine to be able to call another, they need to pass and receive arguments in a compatible way. Computing platforms typically have an application binary interface (ABI) that says how arguments are passed. Routines written in C, C++, FORTRAN, PL/I, or other languages can call each other as long as they use the same ABI. Routines compiled with different C standards can call each other as long as they use the same ABI.

    There are compatibility issues other than passing arguments. One version of a library might have some feature that requires specifying a length. The next version might require specifying the length and a width, or it might have the length required and the width optional. A program written for one version of the library might not be able to use another version because it is not passing the arguments the library requires, even though the way it is passing the arguments conforms to the ABI.

    If you have the source code for a library and for your own program and you compile them both using the same compiler, just with a C 2017/2018 switch for one and C 2011 for the other, they can work together if you call the library routines correctly.

    Object files are generally not executable because they are in a different format than executable files. There are different object formats, and, as far as what is theoretically possible if not actually practiced, somebody could design an object file format that is executable if it contains no dependencies or could design a program loader that reads an object file format and loads it for execution, again if it has no dependencies. In that regard, though, you could execute a source file by making a program loader compiled, linked, and loaded it.