Search code examples
cmacroscompilationc-preprocessorobject-files

Does C make any guarantees about object files?


I have some C code that I want to change, but I want to use #ifdefs so that it's possible to compile the old object files (the post-pre-processing code will not have changed). For certain reasons (compiling for several platforms, and certification reasons), I need to keep the object files identical (apart from any timestamps).

Does the C language spec or do any C compilers make any guarantees about recompilation? I've been doing a bit of Googling, but I haven't been able to find anything on the topic.

For example:

main.c

int main() {
    printf("Hello World!\n");
    return 0;
}

main.h (new file)

#ifdef DEBUG
    char some_buffer[1000];
    #define PRINTF(S) (sprintf(some_buffer, S))
#else
    #define PRINTF(S) (printf(S))
#endif

main.c (updated file)

#include "main.h"

int main() {
    PRINTF("Hello World!\n");
    return 0;
}

If I were to compile the updated main.c with DEBUG not defined, which now includes main.h, does the C language spec or any C compiler make any guarantees that the object file will be the same?


Solution

  • The C language makes no guarantees about object files. Not even that the toolchain will necessarily have anything like object files. Specific compilers will probably have some rules about their own object files, but they may or may not be particularly well documented.

    Anyway, if you already have a specific object file that you need, why do you care so much about being able to recreate it? If you want to build an old version of the software, can't you just check out from revision control whenever you need to build it, without having to clutter the source with every version in the same place?