Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C object file compatibility between computers

First I want to state for the record that this question is related to school/homework.

Let’s say computers CP1 and CP2 both share the same operating system and machine language. If a C program is compiled on CP1, in order to move it to CP2, is it necessary to transfer the source code and recompile on CP2, or simply transfer the object files.

My gut answer is that the object files should suffice. The C code is translated into assembly by the compiler and assembled into machine code by the assembler. Because the architecture shares the same machine code and operating system, I don't see a problem.

But the more I think about it, the more confused I’m starting to get.

My questions are:

a) Since its referring to object files and not executables, I’m assuming there has been no linking. Would there be any problems that surface when linking on CP2?

b) Would it matter if the code used C11 standard on CP1 but the only compiler on CP2 was C99? I'm assuming this is irrelevant once the code has been compiled/assembled.

c) The question doesn't specify shared/dynamic linked libraries. So this would only really work if the program had no dependencies on .dll/.so/ .dylib files, or else these would be required on CP2 as well.

I feel like there are so many gotchas, and considering how vague the question is I now feel that it would be safer to simply recompile.

Halp!

like image 644
kbirk Avatar asked Jan 13 '13 02:01

kbirk


2 Answers

The answer is, it depends. When you compile a C program and move the object files to link on a different computer, it should work. But because of factors such as endianness or name mangling, your program might not work as intended, and even might crash when you try to run it.

C11 is not supported by a C99 compiler, but it does not matter if the source has been compiled and assembled.

As long as the source is compiled with the libraries on one machine, you don't need the libraries to link or run the file(s) on the other computer (static libraries only, dynamic libraries will have to be on the computer you run the application on). This said, you should make the program independent so you don't run into the same problems as before where the program doesn't work as intended or crashes.

You could get a compiler that supports EABI so you don't run into these problems. Compilers that support the EABI create object code that is compatible with code generated by other such compilers, thus allowing developers to link libraries generated with one compiler with object code generated with a different compiler.

I have tried to do this before, but not a whole lot, and not recently. Therefore, my information may not be 100% accurate.

like image 60
syb0rg Avatar answered Sep 28 '22 21:09

syb0rg


a) I've already heard the term "object files" being used to refer to linked binaries - even though it's kinda inaccurate. So maybe they mean "binaries". I'd say linking on a different machine could be problematic if it has a different compiler - unless object file formats are standardized, which I'm not sure about.

b) Using different standards or even compilers doesn't matter for binary code - if it's linked statically. If it relies on functions from a dynamic lib, there could be problems. Which answers c) as well: Yes, this will be a problem. The program won't start if it doesn't have all required dynamic libs in the correct version. Depends on linking mode (static vs. dynamic), again.

like image 21
lethal-guitar Avatar answered Sep 28 '22 19:09

lethal-guitar