0

I write some programs on linux with C I want to run these programs on many remote computers, which are installed with fedora or ubuntu

I compiled the program with gcc on local machine, however the excutable file is not workable on remote machines.

for example: I use

gcc -o udp_server udp_server.c

on local machine to get a excutable binary file udp_server and then I copy it to a remote machine and run it there, the error is:

-bash: ./udp_server: /lib64/ld-linux-x86-64.so.2: bad ELF interpreter: No such file or directory


the local machine: fedora Fedora release 16 (Verne) Kernel \r on an \m (\l) 3.6.10-2.fc16.x86_64 GNU/Linux

the remote machine: Fedora release 12 (Constantine) Kernel \r on an \m (\l) 2.6.32-36.onelab.x86_64 GNU/Linux

on these remote machines, there are no gcc compiler so I hope I can make some excutable files so that they can be executed on those remote machines

so what kind of excutable files should I make, and how to make them? any recommenation tools or procedures? thanks!

user138126
  • 953
  • 3
  • 14
  • 29
  • 2
    *"how to make executable files based on source codes"* that is called [compilation](http://en.wikipedia.org/wiki/Compiler).You are not asking how to *compile* your source code, you are asking why the distributed executable files are not working on another machine. – talles Mar 08 '13 at 16:31
  • The error message you are getting is discussed here: [CentOS 64 bit bad ELF interpreter](http://stackoverflow.com/questions/8328250/centos-64-bit-bad-elf-interpreter) – sleske Mar 08 '13 at 21:22
  • You need to use a *cross-compiler* to create executables for a different machine. – luser droog Mar 28 '13 at 04:13

3 Answers3

3

To run a program written in C, you must first compile it to produce an executable file. On Linux, the C compiler is typically the "Gnu C Compiler", or gcc.

If you compile a C program on Linux, it should usually run on any other Linux computer. However, a few conditions must be met for this to work:

  • A compiled executable is compiled for a specific processor architecture. For example, if you compile for x86-x64, the program will not run on x86 or PowerPC.
  • If the program uses shared libraries, these must be installed on the target system. The C library, "libc" is installed everywhere, other libraries may not be.

As to how to compile: For a simple program, you can invoke gcc directly. For more complex programs, some build tool is advisable. There are many to choose from; two popular choices are GNU make (the traditional solution), and CMake.

To distribute the program: If it is only a single executable, you can just copy this executable around. If the program consists of multiple files (images, data files, etc.), you should package it as a software package. This allows users to install it using a package manager such as RPM or dpkg. How to do this is explained in various packaging guides for the different Linux distributions.

Finally, a piece of advice: You seem to know very little about software development in general and in C in particular. Consider reading some tutorial on programmin in C - this will answer these (and many other) questions. There are countless books and online tutorials - I can recommend "The C book", by gbdirect.

sleske
  • 81,358
  • 34
  • 189
  • 227
2

The issue you see is you are missing a dynamic library on the target machine. To see which libraries you need you need to use "ldd" program. Example (I just execute it against standard program "test" which is in every single linux distribution):

$ ldd /usr/bin/test
    linux-vdso.so.1 =>  (0x00007fff5fdfe000)
    libc.so.6 => /lib64/libc.so.6 (0x00000032d0600000)
    /lib64/ld-linux-x86-64.so.2 (0x00000032cfe00000)

On Fedora and RHEL you can find which RPM package you want to install using the following command

$ rpm -q --whatprovides /lib64/ld-linux-x86-64.so.2
glibc-2.16-28.fc18.x86_64

And then you need to install it:

$ yum -y install glibc-2.16-28.fc18.x86_64

I dont use Ubuntu / Debian, not sure how to do this. Please note that on 32bit systems libraries for 64bits are not avaiable, but on 64bit systems these libraries have usualla i686 tag and are installable.

lzap
  • 16,417
  • 12
  • 71
  • 108
0

Usually, you can execute your program on different machines as long as you keep the architecture. E.g. you cannot execute 64bit program on a 32bit machine, and also vice versa (you can workaround this by installing 32bit libs but thats maybe too difficult).

If you have different distributions, or different versions of same linux distribution, this might be problem - you need to make sure you have all the dependencies in the same major versions.

Or you can link libraries statically which is not usual in the linux world at all, but you can do this. Learn how to use GCC and then you will find out how to do that.

lzap
  • 16,417
  • 12
  • 71
  • 108
  • 1
    Not totally true. x86-64 (amd64) supports backward compatibility with x86-32 (IA32) [source](http://en.wikipedia.org/wiki/X86-64#Operating_modes) – m0skit0 Mar 08 '13 at 12:55
  • And I said that above, read it again. But you need extra 32bit libs, I tried to warn the guy as he seems to be beginner in this field... – lzap Mar 08 '13 at 12:57
  • 1
    If the executable depends on 32-bit libs then yes. If not, you do not need to install anything. Anyway, this is a minor point :) – m0skit0 Mar 08 '13 at 12:59
  • I still don't get the point, if my machine is x86-64, but the remote machine is just x86, how can I make an excutable file so that it can be excuted in the remote machine? – user138126 Mar 08 '13 at 13:03
  • Yeah indeed. User: Well you cant do that normally, but the other direction would work. There is a solution called cross-compilation, but I'd rather recommend you to start with 64bits everywhere first. – lzap Mar 08 '13 at 13:06
  • I heard that I should make rpm or something like that? is it ok? – user138126 Mar 08 '13 at 13:10