1

So when I try to run a compiled C program on my school's "hercules" server, it runs as I would normally expect it to. However, I've got an assignment that requires the use of forks, and running programs that fork on that server is forbidden, instead, I am to run them by remotely connecting to one of several Linux machines and running it there, from command line

However, any attempt to do so gives me this error:

shell2: Exec format error. Binary file not executable.

Altogether, my command prompt looks like this:

a049403[8]% shell2
shell2: Exec format error. Binary file not executable.

I've got the shell2 file in the working directory, when I type "ls" it shows it with the * character indicating it is notionally an executable. I've tried setting its permissions to 777. It produces the same error for other C programs which I have been working with and running, and "hercules" can run the exact same file without any difficulties or complaints. My make file for this particular program looks like this:

all: shell2

Basics.o: Basics.c Basics.h
    cc -c Basics.c

doublinked.o: doublelinked.c doublelinked.h
    cc -c doublelinked.c

main.o: main.c Basics.h doublelinked.h
    cc -c main.c

shell2: main.o Basics.o doublelinked.o
    cc main.o Basics.o doublelinked.o -o shell2

clean:
    rm -f *o shell2

...and if I re-run the makefile it seems to build the program with no difficulties.

So, and reason an environment that can compile C programs would be unable to run them? Any likely workarounds?

Brandon
  • 16,382
  • 12
  • 55
  • 88
user1299656
  • 630
  • 1
  • 9
  • 15
  • 2
    What does `file yourprog /bin/sh` say on the target machine? If it doesn't say much the same thing, the trouble is what the error message says — your binary was prepared on a machine but is not executable on the machine where you're trying to run it. It is not clear what 'hercules' means or which O/S it runs, but it appears to be incompatible with the Linux machines where you try to run it. Do the compilation on the Linux machines, not on 'hercules'. – Jonathan Leffler Feb 17 '14 at 05:53
  • 1
    "hercules" running the exact same file (your words) means nothing if "hercules" isn't using the exact same executable format as your Linux machine. Delete the file, *then* `make clean`, then `make all` *on your linux box*. – WhozCraig Feb 17 '14 at 05:54
  • You should install Linux on your own laptop.... – Basile Starynkevitch Feb 17 '14 at 06:02
  • I thought I had deleted everything and recompiled it all from Linux, but I did it again and it seems to be able to execute now, so thanks. Jonathan. – user1299656 Feb 17 '14 at 14:38

2 Answers2

2

First, your Makefile is not very good. See this example and this one to improve it. Always pass -Wall -g flags (to get all warnings and debug info) to gcc when compiling some home work.

Then, a possible explanation of why your binary program don't run on the target machine could be libc version mismatch. Or perhaps one system is 32 bits and the other is 64 bits. Use file shell2 to find out.

I suggest to make clean then recompile again (e.g. with make all) on the target machine.

Learn how to use the gdb debugger.

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
1

If you're running a program compiled on a different (incompatible) architecture, you'll almost certainly run into troubles.

It's a little unclear from the question whether you're transferring and attempting to run the executable, or trying to build the executable from scratch on the new machine. It's also unclear whether your make (if you're running it on the new machine) is completely rebuilding everything.

However, given the "Exec format error", it's a safe bet that the former case is the one you're encountering, trying to run an incompatible executable file.

So, here's my advice. Transfer everything to the new machine and execute:

make clean all

to ensure everything is being rebuilt.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953