Page 1 of 3 123 LastLast
Results 1 to 10 of 28

Thread: Compiling/Running C Programs?

  1. #1
    Join Date
    Apr 2005
    Location
    UK
    Beans
    13

    Compiling/Running C Programs?

    Okay , after deciding to take the plunge and learn C/C++, I installed the build essential packages. After looking at a few tutorials, I create first.c in gedit and put in the following code:
    Code:
    #include <stdio.h>
    
    int main() 
    {
      printf("Hello, world\n");
      return 0;
    }
    Then I used 'gcc -o first first.c' This created the executable first. Now according to the tutorial I read (http://farside.ph.utexas.edu/teachin...es/node35.html) I have to enter first into the terminal and it should run. However, it doesn't, I just get a '$ bash: first: command not found' error. How can I run my programs after compiling and linking? Or am I compiling/linking properly in the first place? Thanks.

  2. #2
    Join Date
    Feb 2005
    Location
    israel
    Beans
    412

    Re: Compiling/Running C Programs?

    hi friend!
    it'd run
    you just have to write:
    ./first

    dot then slash

    you work this way,firstly compile the code:

    cc -c first.c

    that would produce an object file you may need to add to the library.

    then create an executable:

    cc -o first first.c

    after what run it:

    ./first


    have a nice day!
    pavel

  3. #3
    Join Date
    Apr 2005
    Location
    UK
    Beans
    13

    Re: Compiling/Running C Programs?

    Thanks! I knew it would be something simple!

  4. #4
    Join Date
    Feb 2005
    Location
    Laurel, Merryland
    Beans
    142
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Compiling/Running C Programs?

    Just a note:

    The reason you have to add the "./" to the filename is because your current working directory is not included in your path. The "./" means run the executable file first from the current directory. Excluding the current directory from the path is common on unix systems to prevent certain security problems.

  5. #5
    Join Date
    Aug 2006
    Beans
    1

    Exclamation Re: Compiling/Running C Programs?

    I dont know what to do when i put in "GCC -o hello.c" but it says "bash: gcc: command not found" What do I do?

  6. #6
    Join Date
    Aug 2005
    Location
    East Lansing, MI
    Beans
    503
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Compiling/Running C Programs?

    Quote Originally Posted by Johnhatton09 View Post
    I dont know what to do when i put in "GCC -o hello.c" but it says "bash: gcc: command not found" What do I do?
    Install the 'build-essential' package.

  7. #7
    Join Date
    Jul 2006
    Location
    Israel
    Beans
    154
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Compiling/Running C Programs?

    Code:
    sudo apt-get install build-essential
    "You've tried the rest, now experience the best!"

    Leonardo Juszkiewicz

  8. #8

    Re: Compiling/Running C Programs?

    gcc has to be lowercase.

  9. #9
    Join Date
    Sep 2007
    Beans
    13

    Re: Compiling/Running C Programs?

    i tried the same program but it gavve the following erroe
    #include <stdio.h>

    int main()
    {
    printf("Hello, world\n");
    return 0;
    }
    error: stdio.h: No such file or directory
    first.c: In function ‘main’:
    first.c:6: warning: incompatible implicit declaration of built-in function ‘printf’

  10. #10
    Join Date
    Aug 2007
    Location
    Canada
    Beans
    440
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Compiling/Running C Programs?

    Quote Originally Posted by coolyog View Post
    i tried the same program but it gavve the following erroe
    #include <stdio.h>

    int main()
    {
    printf("Hello, world\n");
    return 0;
    }
    error: stdio.h: No such file or directory
    first.c: In function ‘main’:
    first.c:6: warning: incompatible implicit declaration of built-in function ‘printf’
    Did you try first?, if not, open a terminal and type:

    > sudo apt-get install build-essential

    after that youll be able to do:

    > gcc first.c -o first

    and then:

    >./first
    Best, Gnusci

    "Never make a calculation until you know the answer." -- Wheeler, Spacetime Physics, pg 60.

Page 1 of 3 123 LastLast

Bookmarks

Posting Permissions

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