Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is binary executable file portable

Tags:

c

After compiling a C program can I take the binary executable file, and run it on another system where there is no gcc loaded, such as an Ubuntu box?

like image 821
user481831 Avatar asked Nov 04 '10 20:11

user481831


2 Answers

Technically: yes, but use a static link if you need max portability


Strictly speaking you don't need gcc, but you may need various libraries. By default, language processors produce dynamically linked binaries which require extensive run-time support in the form of libraries, and compatible versions must be found on the target system that can be substituted for the ones you linked against on the development host.

This requires that the target be a similar version of the same OS, for example, linux to linux. There are more subtle issues of version skew. On windows, this is known as DLL-hell.

You can isolate yourself from many of these concerns by static linking. This will make the executable file larger and it won't share memory anymore (except with additional instances of itself) but the program will be able to survive more target version skew.

like image 191
DigitalRoss Avatar answered Sep 28 '22 07:09

DigitalRoss


gcc compiles C into machine code, this means that he code will only run on the same architecture for which it was compiled. Additionally there are typically some dependencies to other binaries (e.g. C-runtime, posix, Win32) so if you compile a program on Ubuntu it will run on Ubuntu even if it doesn't have gcc installed but it will not run on Windows or other unixes (like Solaris or HPUX).

This is where C is different from Java and C# where the code is compiled to a virtual machine code and runs on any system which has this language runtime (JVM/CLR). Other portable languages are scripting languages (Perl/Python/JavaScript) where the script can run anywhere there is an interpreter.

like image 41
Motti Avatar answered Sep 28 '22 06:09

Motti