Building and running a C file in xcode thats NOT a project

Discussion in 'Mac Programming' started by cameronbelt, Jan 31, 2012.

  1. macrumors newbie

    Joined:
    Jan 31, 2012
    #1
    I have a c file that i wrote on a windows computer and saved to my flash drive when i double click it opens up xcode to display but i cant compile or run it. is there any way to do this without having to go through the trouble of creating a new project and copying and pasting? by now i could have already done that but its the principle of the thing at this point. there has to be some way to do this.
    thanks
     
  2. macrumors 6502

    Joined:
    Mar 8, 2004
    #2
    Go to the Terminal app and type (sans quotes) "man gcc" and read the instructions there.
     
  3. thread starter macrumors newbie

    Joined:
    Jan 31, 2012
  4. lloyddean, Jan 31, 2012
    Last edited by a moderator: Feb 2, 2012

    macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
  5. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #5
    In that case either your gcc installation is broken, or it was the culture shock of being confronted with pages of instructions without any pictures in between.

    What exactly happened when you started terminal and entered "man gcc"?
     
  6. macrumors 68000

    Joined:
    Mar 29, 2009
    Location:
    USA
    #6
    Open Terminal and go to the directory your .c file is in

    gcc foo.c -o foo

    Then to run the program, just type:

    ./foo

    That's it!

    To edit the files, you can use nano/vi/emacs in the Terminal, or download Komodo, Textwrangler, Sublime edit, whatever you like to use.
     
  7. macrumors 6502

    Joined:
    Mar 8, 2004
    #7
    I know its mean but I literally "lol"ed at that.

    If your GCC is broken you can install it again via this package:

    https://github.com/kennethreitz/osx-gcc-installer

    If you scroll down a bit you'll see two links to per-built GCC packages (one for 10.6 and one for 10.7).
     
  8. macrumors member

    Joined:
    Jan 16, 2011
    #8

    The problem you're having is that xcode doesn't know what to do with the file; it just opened a file for viewing and has no idea how to compile it. To put the file in a context where it DOES know how to compile it, create a new xcode command-line project and then copy your code into the main.m file. After that you should be able to run it since xcode knows that for command line projects it's supposed to compile the main.m file into an executable.
     

Share This Page