6

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?

C. Ross
  • 31,137
  • 42
  • 147
  • 238
user481831
  • 953
  • 2
  • 7
  • 8

5 Answers5

10

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.

DigitalRoss
  • 143,651
  • 25
  • 248
  • 329
  • +1 for the best advice on this topic. Unfortunately static linking with glibc is impossible these days. If your system has eglibc, you're in better shape, but there are still libnss_* issues. If you can setup an environment to build your application against uClibc, that will work the best for distributing static binaries. – R.. GitHub STOP HELPING ICE Nov 04 '10 at 21:32
  • I hesitate to question a +1 comment, but I just statically linked hello world on CentOS 5.4 and 5.5. My a.out ballooned from 4,376 bytes to 538,241, but it worked. On what system does it now fail? – DigitalRoss Nov 04 '10 at 21:42
  • Are you sure CentOS is still using glibc? Many distributions have switched to the eglibc fork, largely because of "personality issues" with the maintainer of glibc - and at the same time they get the benefit of halfway-working static linking. – R.. GitHub STOP HELPING ICE Nov 04 '10 at 22:39
  • CentOS is pretty much a mirror of RHEL and as a server distro, it's pretty much the opposite of "leading edge". – DigitalRoss Nov 04 '10 at 23:16
5

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.

Motti
  • 110,860
  • 49
  • 189
  • 262
  • yes i understood! what if i write a shell script does it depend on the type of the shell ex: c , korn, bourn am i right – user481831 Nov 04 '10 at 21:00
  • 2
    @user481831 Every shell script specifies which script engine should run it (in the [shebang](http://en.wikipedia.org/wiki/Shebang_%28Unix%29) line) – Motti Nov 04 '10 at 21:06
3

As long as it's the same architecture and has the same run-time libraries, it should run fine. For example, compiling a program on 32-bit Linux wont run on 64 bit Windows, but if you compile a program on Ubuntu and then put it on another ubuntu machine it should run fine.

Alex
  • 4,316
  • 2
  • 24
  • 28
3

Partially true. Yes, you don't need the compiler itself, but you need the C runtime library. Anyway, both C compiler and C runtime is part of POSIX, therefore you won't find a Unix system without them.

Šimon Tóth
  • 35,456
  • 20
  • 106
  • 151
2

Depends on the platforms and libraries installed on the platform. A compiled executable will likely have dynamically linked library dependencies on, say, libc, and whatever else it was linked to during compilation.

What platforms/compilers are involved, and where do you want to move this binary to?

wkl
  • 77,184
  • 16
  • 165
  • 176