0

I created a test app in C as follows:

#include<stdio.h>
int main(void){
    printf("Hello World!\n");
    return 0;
}

I compile via the following command on my MacBook Air, running OS X Yosemite:

$ cc foo.c -o foo

I then upload the "foo" binary to my CentOS VPS, CHMOD 755, then attempt to execute via the following command:

$ ./foo

This returns the following result:

-bash: ./foo: cannot execute binary file

I also attempted to compile with -m32 in case it was a 64/32 bit conflict. The same result was produced.

Why will CentOS will not execute the binary?

5
  • Does the foo has execute permission? Commented Sep 29, 2016 at 11:08
  • @Raptor Yes, as I stated permissions are set to 755. Commented Sep 29, 2016 at 11:10
  • What machine did you build on? Commented Sep 29, 2016 at 11:16
  • @JohnZwinck My MacBook Air, running OS X Yosemite. Commented Sep 29, 2016 at 11:17
  • try to execute file foo to see what is the output Commented Sep 29, 2016 at 11:19

1 Answer 1

4

You built an executable on Mac OS X, and tried to run it on Linux. This will never work, because these two operating systems are not compatible. The easiest way to fix this will be to build your program on a Linux machine, perhaps a virtual machine running on your Mac. In general, the phrase describing this situation is "cross compilation."

Sign up to request clarification or add additional context in comments.

2 Comments

Precisely the issue. I was under the impression that as long as I was compiling with g++ it would be Linux compatible?
@nicktendo: No. GCC exists on many platforms, but it builds executables for the platform it's made for, usually. The exceptions are called "cross compilation" and are not typical.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.