Aganezor
Aganezor

Reputation: 43

Why am I not able to produce a "Hello World" executable using gcc?

I'm trying to compile my first "Hello World" application using GCC (build-on clang and stand-alone GCC-4.9.2) without any success:

OS version is OS X Yosemite 10.10.2.

I'm using the following code (main.c):

#include <stdlib.h>
#include <stdio.h>

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

Then I compile it in terminal with a command (clang shipped with XCode):

gcc -o hello -c main.c

As a result I got following error whe running compiled hello file:

MBP-Andrii:cTest andrii$ gcc -o hello -c main.c
MBP-Andrii:cTest andrii$ hello
-bash: /Users/andrii/avrTest/cTest/hello: Permission denied

If I change the permissions for hello file to 777 and a new error again:

MBP-Andrii:cTest andrii$ chmod 777 hello
MBP-Andrii:cTest andrii$ hello
Killed: 9
MBP-Andrii:cTest andrii$

The same thing happens with stand-alone GCC-4.9.2.

I guess It might be something related to the output binary format or some missing flags for compiler.

Upvotes: 0

Views: 318

Answers (2)

PICyourBrain
PICyourBrain

Reputation: 10294

Remove the -c from the command you're using to compile the application.

The -c tells the compiler to only compile and assemble but not link. You are not actually creating an application when using -c.

See the GCC Manual:

-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.

Upvotes: 3

PICyourBrain
PICyourBrain

Reputation: 10294

Try executing it by typing ./hello

Upvotes: 0

Related Questions