Professional Web Applications Themes

using the terminal to compile/run C - Mac Programming

I'm a total beginner, trying out some sample C code. I was wondering how to use the Terminal to do this (after using Project Builder to build). How do you use the different commands such as "cc" and "./a.out" If I try "cc projectname.c" or "cc main.c" I get file not found. (The file is saved in a folder in my Home directory) Or if I try the one used from this link (http://cocoadevcentral.com/articles/000054.php): "The better way to run the program is to use the Terminal. Launch Terminal.app and type ./projectname/build/projectname if you created the project in your home directory. ...

  1. #1

    Default using the terminal to compile/run C

    I'm a total beginner, trying out some sample C code. I was wondering
    how to use the Terminal to do this (after using Project Builder to
    build). How do you use the different commands such as "cc" and
    "./a.out"

    If I try "cc projectname.c" or "cc main.c" I get file not found. (The
    file is saved in a folder in my Home directory)

    Or if I try the one used from this link
    (http://cocoadevcentral.com/articles/000054.php):

    "The better way to run the program is to use the Terminal. Launch
    Terminal.app and type ./projectname/build/projectname if you created
    the project in your home directory. Doing so will run the program in
    Terminal and allow you to interact with it."

    I get command not found, and I'm not sure if I'm using that command
    correctly if I have the file inside a folder in Home.

    If you would be so kind, please explain as simply as possible, or if
    you know of a good resource to explain Terminal commands?

    Also, do people recommend using the Terminal to run programs, which I
    heard in several places, or to just use Project Builder?

    Thanks for any help.
    ak Guest

  2. #2

    Default Re: using the terminal to compile/run C

    ak <com> wrote:
     

    Then you need to cd into that folder, or else say cc theFolder/main.c
    (assuming theFolder is the name of the folder and main.c is a c file).
    m.


    --
    matt neuburg, phd = com, http://www.tidbits.com/matt/
    AppleScript: The Definitive Guide
    http://www.amazon.com/exec/obidos/ASIN/0596005571/somethingsbymatt
    Read TidBITS! It's free and smart. http://www.tidbits.com
    matt Guest

  3. #3

    Default Re: using the terminal to compile/run C

    com (matt neuburg) wrote in message news:<1g6slkh.y5u6g9c6qlvkN%com>...
     

    I think I tried that, although I probably didn't do it right. Let me
    be more specific, perhaps you or someone else might be able to help. I
    went into Project Builder, created a file in ~/Developer/multiply2,
    and typed the following code into the main.c file. First,perhaps
    someone could tell me why nothing happens when I try to compile and
    run this in Project Builder, even though the build was successful, and
    also how I would compile and run it in Terminal.


    #include <stdio.h>

    int a,b,c;

    int product (int x, int y);

    int main ()
    {
    printf("Enter a number between 1 and 100: ");
    scanf("%d", &a);

    printf("Enter another number between 1 and 100: ");
    scanf("%d", &b);

    c = product(a,b);
    printf("%d times %d = %d\n", a,b,c);

    return 0;

    }


    int product (int x, int y)
    {
    return(x*y);
    }
    ak Guest

  4. #4

    Default Re: using the terminal to compile/run C

    On 31 Dec 2003 09:56:12 -0800, ak wrote: 
     [/ref]
     

     
     
     
     

     

    What type of project were you creating? Was it a "Standard Tool"? Are
    you sure you used the "Build and Run" command, and not just "Build"?
    What happens if you use the "Run" command?

    I tried this using XCode, but it used to work pretty much the same way in
    Project Builder. When you run a command line tool, a window comes up to
    handle the standard input and output for your program.

    To compile and run on the command line, the easiest way is to cd to the
    directory containing the program and then type:

    cc multiply2.c
    ./a.out


    --
    Dave Seaman
    Judge Yohn's mistakes revealed in Mumia Abu-Jamal ruling.
    <http://www.commoncouragepress.com/index.cfm?action=book&bookid=228>
    Dave Guest

  5. #5

    Default Re: using the terminal to compile/run C

    Dave Seaman <such.host> wrote in message news:<bsv9d3$jmj$cc.purdue.edu>... 
    >[/ref]
     


    It was a Standard Tool, I hit "Build and Run" and it opens two
    windows, one to show that the Build was successful, another where the
    output should be, but it's blank.

    I tried another simple program and had the same problem. I was able to
    get both of them to work in Terminal though. I went to each directory
    and used "cc main.c" (the file from Project Builder) which created the
    ../a.out file in that folder. Then I was able to use "./a.out" in
    Terminal. I don't know if there's a better way to do it.

    Anyone know what the problem might be in Project Builder? It also
    crashes quite frequently. I suppose I ought to upgrade to Panther!
    ak Guest

  6. #6

    Default Re: using the terminal to compile/run C

    On 31 Dec 2003 18:14:07 -0800, ak wrote: [/ref][/ref]

     [/ref]
     [/ref]
     [/ref]
     [/ref]

     

    It should print a message first. Did you try typing a number to see what
    happens?
     
     

    Perhaps you need a more recent version of the Developer Tools. If you
    are running Jaguar, you should go to developer.apple.com/macosx/,
    register as a member if you haven't already (it's free) and then download
    the December 2002 Mac OS X Developer Tools.


    --
    Dave Seaman
    Judge Yohn's mistakes revealed in Mumia Abu-Jamal ruling.
    <http://www.commoncouragepress.com/index.cfm?action=book&bookid=228>
    Dave Guest

  7. #7

    Default Re: using the terminal to compile/run C

    In article <bsv9d3$jmj$cc.purdue.edu>, such.host
    says... 

    And if you want a different name for your executable use the -o switch:

    cc -o multiplier multiply2.c
    ./multiplier

    James Guest

Similar Threads

  1. which container for a terminal like app?
    By agelanyi in forum Macromedia Flex General Discussion
    Replies: 4
    Last Post: June 6th, 07:32 PM
  2. Terminal Services....RDT
    By JIM in forum Windows Server
    Replies: 3
    Last Post: July 20th, 06:47 PM
  3. Replies: 0
    Last Post: July 1st, 02:04 PM
  4. permissions with terminal
    By Jeff Kelleher in forum Mac Applications & Software
    Replies: 4
    Last Post: July 23rd, 08:29 AM
  5. Terminal
    By MS in forum Mac Applications & Software
    Replies: 5
    Last Post: July 10th, 02:30 AM

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
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139