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

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

  1. cameronbelt macrumors newbie

    Jan 31, 2012
    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.
  2. Mac_Max macrumors 6502

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

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

    lloyddean macrumors 6502a

    May 10, 2009
    Des Moines, WA
  5. gnasher729 macrumors P6


    Nov 25, 2005
    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. robvas macrumors 68030

    Mar 29, 2009
    Open Terminal and go to the directory your .c file is in

    gcc foo.c -o foo

    Then to run the program, just type:


    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. Mac_Max macrumors 6502

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

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

    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. mydogisbox macrumors member

    Jan 16, 2011

    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