20

I compile this program:

#include <stdio.h>

int main()
{
    printf("Hello World!");
    return 0;
}

With this command:

  gcc -c "hello.c" -o hello

And when I try to execute hello, I get

bash: ./hello: Permission denied

Because the permissions are

-rw-r--r-- 1 nathan nathan   856 2010-09-17 23:49 hello

For some reason??

But whatever... after changing the permissions and trying to execute again, I get

bash: ./hello: cannot execute binary file

I'm using gcc (Ubuntu 4.4.3-4ubuntu5) 4.4.3

What am I doing wrong here? It's gotta be obvious... it's just too late for me to keep using my tired eyes to try and figure out this simple problem....

P.S. I do (sometimes) work on programs more sophisticated than Hello World, but gcc is doing this across the board...

2
  • 2
    Execute: file hello and paste the result here. You should be compiling with: gcc hello.c -o hello Commented Sep 18, 2010 at 4:01
  • Always ask all warnings and debugging information (e.g. gcc -Wall -g hello.c -o hello) Commented Jan 6, 2016 at 15:53

3 Answers 3

28

Take the -c out. That's for making object files, not executables.

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

1 Comment

Just to add, the hello file your making is an object file (if that wasn't obvious). It should be named hello.o if you build with the -c option.
7

The -c flag tells it not to link, so you have an object file, not a binary executable.

In fact, if you ran this without the -o flag, you would find that the default output file would be hello.o.

For reference (and giggles), the man entry on the -c flag:

-c  Compile or assemble the source files, but do not link.  The linking stage simply is not done.
    The ultimate output is in the form of an object file for each source file.

    By default, the object file name for a source file is made by replacing the suffix .c, .i, .s,
    etc., with .o.

    Unrecognized input files, not requiring compilation or assembly, are ignored.

Comments

3

Compile with: gcc hello.c -o hello

Comments

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.