3

I've used makefile to generate file. gcc -c hello.c -o hello and fixed the permission problem through: chmod a+x ./hello However, when I want to execute "hello" file. ./hello the system told me that "cannot execute binary file" Can someone help me? I am looking forward your reply badly.

1
  • 2
    Can you post the content of file ./hello please? Commented Aug 30, 2018 at 13:19

3 Answers 3

8

The -c argument to gcc produces an object file which you later on must link in order to produce an executable. You can not execute the object file you produced.

Instead, to compile and link at the same time, suitable when you only have 1 .c file, do

gcc hello.c -o hello 

Or if you want to break it down to separate compilation and linking steps, do

gcc -c hello.c -o hello.o gcc hello.o -o hello 
Sign up to request clarification or add additional context in comments.

Comments

2

Check whether the GCC compiler is installed in your system correctly or not.

gcc -v 

Compile your file:

gcc filename.cpp -o any-name 

Running your program:

./any-name 

Comments

0

As an alternative to compiling and linking at the same time you can use make:

make hello 

Note: without the .c extension.

The terminal output should be:

cc hello.c -o hello 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.