PDA

View Full Version : Building and running a C file in xcode thats NOT a project




cameronbelt
Jan 31, 2012, 10:38 PM
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



Mac_Max
Jan 31, 2012, 10:55 PM
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

Go to the Terminal app and type (sans quotes) "man gcc" and read the instructions there.

cameronbelt
Jan 31, 2012, 11:03 PM
i didnt see any instructions

lloyddean
Jan 31, 2012, 11:57 PM
I'll point you at my reply in another of the forum discussions:

http://forums.macrumors.com/showpost.php?p=14116212&postcount=21

gnasher729
Feb 1, 2012, 02:25 AM
i didnt see any instructions

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"?

robvas
Feb 1, 2012, 08:04 AM
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.

Mac_Max
Feb 1, 2012, 10:14 AM
...or it was the culture shock of being confronted with pages of instructions without any pictures in between.

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).

mydogisbox
Feb 2, 2012, 08:47 AM
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


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.