Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 14
I installed gcc from the apt-get package system and found a website that showed me how to write and run my first c program. The program is just this: Code: ...
  1. #1
    Just Joined!
    Join Date
    Oct 2006
    Location
    Denmark, Løgstør
    Posts
    42

    first program in c not working

    I installed gcc from the apt-get package system and found a website that showed me how to write and run my first c program. The program is just this:

    Code:
    #include <stdio.h>
    
    main()
    {
    printf("Hello, world!\n");
    return 0;
    }
    which I doubt has many errors, but here's my problem. Once I start up a gnome-terminal and writes:

    $ gcc hello.c

    it prompts this:

    Code:
    hello.c:1:19: error: stdio.h: No such file or directory
    hello.c: In function ‘main’:
    hello.c:5: warning: incompatible implicit declaration of built-in function ‘printf’
    gcc is located at /usr/bin/gcc if that is of any help..
    Last edited by devils casper; 08-13-2007 at 01:44 PM. Reason: [code] tags

  2. #2
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Hi,

    You have to install libc6-dev.

    Regards

  3. #3
    Linux Engineer Zelmo's Avatar
    Join Date
    Jan 2006
    Location
    Riverton, UT, USA
    Posts
    1,001
    Check that you have the libc-dev or libc6-dev package (or something similar) installed.
    Stand up and be counted as a Linux user!

  4. #4
    Super Moderator devils casper's Avatar
    Join Date
    Jun 2006
    Location
    Chandigarh, India
    Posts
    24,316
    Execute this
    Code:
    sudo apt-get install build-essential
    It will install all necessary packages including libc6-dev and linux-libc-dev.
    It is amazing what you can accomplish if you do not care who gets the credit.
    New Users: Read This First

  5. #5
    Just Joined!
    Join Date
    Oct 2006
    Location
    Denmark, Løgstør
    Posts
    42
    I installed the libc6-dev, and now it does not prompt an error message when I write "gcc hello.c", now it does nothing, which is, gives no output.

  6. #6
    Just Joined!
    Join Date
    Oct 2006
    Location
    Denmark, Løgstør
    Posts
    42
    I also installed build-essentials, but the result is still the same. Could there be something wrong with the program?

  7. #7
    Blackfooted Penguin daark.child's Avatar
    Join Date
    Apr 2006
    Location
    West Yorks
    Posts
    4,344
    There should be a file called a.out in the directory where you are compiling your apps. You can run it by doing
    Code:
    ./a.out
    If you want your program to have the same name as your file (or any other name for that matter) instead of a.out, you need to do
    Code:
    $gcc -o someprog someprog.c
    Your main function should also have a return type, so your code should be something like
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        printf("Hello, world!\n");
        return 0;
    }

  8. #8
    Just Joined!
    Join Date
    Oct 2006
    Location
    Denmark, Løgstør
    Posts
    42
    ./a.out prints "hello, world". Will this file always show my output and is it necessary to access the output through it?

  9. #9
    Linux User infoshirish's Avatar
    Join Date
    May 2007
    Location
    Pune, India
    Posts
    397
    Hey,
    No need to include <stdio.h> in linux. Your program should be like this.

    # cat hello.c
    main ()
    {
    printf("Hello World");
    }
    # make hello
    # cc hello.c -o hello
    Or
    #./hello.c
    Will also throw your output.
    Regards,

    who |grep -i blonde |
    date; cd~; unzip; touch;
    strip; finger; mount; gasp;
    yes; uptime; umount;
    sleep

    Newbie clicks
    http://www.linuxforums.org/forum/lin...ead-first.html

  10. #10
    Linux Guru techieMoe's Avatar
    Join Date
    Aug 2004
    Location
    Texas
    Posts
    9,496
    Quote Originally Posted by infoshirish View Post
    Hey,
    No need to include <stdio.h> in linux. Your program should be like this.

    # cat hello.c
    main ()
    {
    printf("Hello World");
    }
    # make hello
    # cc hello.c -o hello
    Or
    #./hello.c
    Will also throw your output.
    I would not recommend this procedure. ANSI standard C requires includes and return values. You're cutting corners by not returning a status at the end of your program and that's just bad practice.

    In my work I've never seen a standard C program that used the above method, so although it may work in theory, I cannot recommend that a new programmer learn a non-standard method that will only lead to confusion later on.
    Registered Linux user #270181
    TechieMoe's Tech Rants

Page 1 of 2 1 2 LastLast

Posting Permissions

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