2

I wrote a C program and saved it using someProgram.c and compiled it on my 64 bit machine. A "someProgram.out" file was generated. I sent the someProgram.out to a friend but it didnt execute on his machine. It reports "Can not execute binary file" error when he tried running it on ubuntu 14.04 installed in his virtual machine. How can I write the code so that it runs on machines with different architectures and also on virtual machines without compiling on all the machines i.e after compiling only on my machine?

Alan Stokes
  • 18,815
  • 3
  • 45
  • 64
Shridhar R Kulkarni
  • 6,653
  • 3
  • 37
  • 57
  • If you use a cross compiler, you can compile your program for different targets, but that still involves compiling it for every target. – melak47 Sep 27 '15 at 15:45
  • You have to use an emulator for the architecture or a C interpreter which also covers the used library functions. – too honest for this site Sep 27 '15 at 15:49
  • 1
    Compile for 32 bits and install the i386 package. http://askubuntu.com/questions/454253/how-to-run-32-bit-app-in-ubuntu-64-bit – Mark Lakata Sep 27 '15 at 15:51
  • 2
    See also https://en.wikipedia.org/wiki/Fat_binary . – Mark Lakata Sep 27 '15 at 15:52
  • Related question: http://stackoverflow.com/questions/31814963/choose-a-c-binary-according-to-the-enviroment/31815277 (I had a similar answer to Mark Lakata regarding fat binaries.) – tonysdg Sep 27 '15 at 17:12
  • 1
    The downvote on the question is creating a trouble. Stack overflow says that my questions are getting no good response from community. so they have blocked me for 5 days. Let me know how do I delete the question or any alternative solution. I am new to the community. – Shridhar R Kulkarni Apr 21 '16 at 07:46

2 Answers2

4

The "can not execute binary file" can be caused by many different things. If the architecture is truly different, then you have no hope other than recompile it (well, aside from running a simulator of the original OS and processor architecture, but that's) or rewrite the code to be some compile intermediate form, e.g. Java that can be compiled to Java bytecode that is, in itself, portable by way of having a "Java virtual machine" that executes the code.

On the other hand, if the problem is that the binary is missing some dependency - some shared library that is on your machine, but not on your friend's machine, then the solution is to install the relevant library (of the right version). Or recompile using static libraries, so that it doesn't depend on the shared library.

The purpose of compiling C+ or C++-code is to produce a binary file that is directly executable on a particular OS/processor architecture. The C and C++ languages are only portable to other OS/processor archictures as source-code, not as binary executable files.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
1

Imagine you are doing a 100 piece Jigsaw puzzle and you lose a single piece. So you steal a piece from a different puzzle, so you will have 100 pieces in yours.

The problem is that the new piece was not made for your puzzle. It won't fit. It would have worked if you had taken your 100th piece from the exact same puzzle as yours. ("it works on my machine")

Compilers produce code for the OS that you target for. If you are 64bit, then your compiler will probably default to creating a 64 bit puzzle piece. If you friend is running 32 bit, then you 64 bit puzzle piece won't fit in his puzzle.

Tell your compiler to produce the correct code for the target.

This stackoverflow question might help you down the right path. Without changing the code, if it is written well, you can change compiler flags to produce two different target images (puzzle pieces)

How do I create a single makefile for both 32- and 64-bit?

Your compiler might scream all kinds of warnings depending how you wrote your program to be not dependent on the number of bits in the OS.

If you are doing cross platform there are going to be other flags you will need to understand too. This is where you get to learn.

Community
  • 1
  • 1
Vanderdecken
  • 195
  • 1
  • 9