Find the answer to your Linux question:
Results 1 to 7 of 7
Hi, I am trying to run a very simple program with gcc. THe source code for this program looks like this: #include <stdio.h> int main() { printf("Hello World"); exit(0); } ...
  1. #1
    jcs
    jcs is offline
    Just Joined!
    Join Date
    Jul 2008
    Posts
    8

    compiling simple programs with gcc

    Hi, I am trying to run a very simple program with gcc. THe source code for this program looks like this:

    #include <stdio.h>

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

    And I saved the program as hello.c.

    But when I compile the program using bash, I get the following messages.
    charles@linux:~> gcc -o hello hello.c
    hello.c: In function ‘main’:
    hello.c:6: warning: incompatible implicit declaration of built-in function ‘exit'

    Is there something wrong with my source code or am I missing certain key libraries to run this program?

  2. #2
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    thats a warning, not an error, you should still notice you have the file you intended on building

  3. #3
    jcs
    jcs is offline
    Just Joined!
    Join Date
    Jul 2008
    Posts
    8
    Thanks.

    I am not getting any output from the program that I have written. Is there something wrong?

  4. #4
    jcs
    jcs is offline
    Just Joined!
    Join Date
    Jul 2008
    Posts
    8
    Hi I have successfully compiled the program above but i had to include the <stdlib.h> as a header file. However, even if the program compiles I am still not getting any kind of out put.

    Someone suggested that I should just type hello at the command prompt. But whenever I do that, I get the following message:

    charles@linux:~> hello
    bash: hello: command not found

    Am I missing something again?

  5. #5
    Linux Guru gogalthorp's Avatar
    Join Date
    Oct 2006
    Location
    West (by God) Virginia
    Posts
    3,105
    Have you set the permission to execute?

    chmod +x hello

    You must explicitly tell the OS that a file has permission to execute.

  6. #6
    Linux User
    Join Date
    Jun 2007
    Posts
    458
    Its not permission that is required. Because your binary file is not in any bin or sbin folder, the folders where the commands are located on UNIX and Linux. For example:

    /bin
    /usr/bin
    /usr/sbin

    You need to issue the ./hello command. This will execute the hello file in the directory you are in.
    "When you have nothing to say, say nothing."

  7. #7
    Linux Engineer rcgreen's Avatar
    Join Date
    May 2006
    Location
    the hills
    Posts
    1,114
    Code:
    #include <stdlib.h>
    This will silence the warning.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...