36

Our teacher is teaching use some Data Structures algorithms that are being implemented in C. He uses Visual Studio in class, but I want to be able to run simple programs that we write in Xcode.

I've tried to run the code but in Xcode 5 the options to do so are greyed out. Besides just installing Xcode, what steps should I need to take so that I can run simple C programs on my Mac?

0

5 Answers 5

42
  • Open Xcode
  • Select File->New->New Project
  • Select macOS (OS X in older versions) -> Command line tool
  • Choose C as type
  • Fill out the name and the other relevant wizards parts you need
  • click the main.c file to select it

And you should be good to go

0
22

In Xcode 6 & 7, you can do the following steps:

Creating the File

  1. Select File > New > Project > OS X
  2. Select Command Line Tool
  3. Click Next
  4. Input your Product Name and choose C as the language
  5. Click Next
  6. Choose where to save your project.
  7. Click Create

Running the Code

  1. Select Product > Run
  2. Check the console to check result.

That's it!

Check your the main.c file and you'll find a pre-filled 'Hello World' code in the editor. Good luck!

5

The above answers are all great. I'll just add my $0.02. I like using Xcode where it is basically just a text editor (not the more complex Xcode...which I need to get used to!).

So, here's what you can do:

  1. Click on Xcode
  2. Ignore the pop-up window asking about a new project
  3. At the top of your monitor, select "File"
  4. Select "New"
  5. Select "File"
  6. You can then select a C file as your option.

You can skip steps 3-5 with command + N!

You'll get a basic editor you can write your code in (nice syntax coloring, basic display, etc.).

Then, you can just compile and run your program in your terminal window (your standard gcc file and ./a.out stuff).

2

Hope my experience will help, cause I've just went from confusion like you had to enjoying coding C on my MacBook in the past month.

Need to give all the credits to the online resource where I learnt everything about setup the environment. http://c.learncodethehardway.org/book/index.html It taught me to just use the shell command through the Terminal instead of a IDE.

Here are the steps as I summarized:

  1. Install Xcode, which you've already done
  2. Install a text editor (optional): this is for you to edit your code, NOT for compiling or running. I personally like Atom now, but there are Emacs and Vim etc.
  3. Start Terminal from the Utilities folder in Applications. I use iTerm2, same function as Terminal with more features.
  4. Navigate to the folder containing the source code. Like cd ~/Documents/C/, in which there is your main.c file for example.
  5. Use your text editor to create a plain text file called Makefile, no extension. It contains:

    CFLAGS=-Wall -g
    main: main.c
    clean:
       rm -f main
    
  6. Then compile source code, from the Terminal, type and see

    $ make clean
    rm -f ex1
    $ make ex1
    cc -Wall -g    ex1.c   -o ex1
    

    make clean delete the last compiled file if you want to re-compile.

  7. Then run the compiled file, type

    ./main
    

    Then you should see the results of the code like you saw from your class. Keep in mind there very likely are some differences between C compilers in Visual C++ and GCC in Xcode. But so far I like very much coding by shell commands.

1
  • This is a way but not the best - how do you debug the code (and why use make there are several better build systems) - this might be a way if the instructor was using Unix but he his using Visual Studio so most of these steps would not be doable on Windows - just use the IDE i.e. Xcode as the instructor uses VS
    – mmmmmm
    Apr 27, 2016 at 21:57
0

I ran into this issue myself following Learning C by Dan Gookin with exercise files. I learned that in order to be able to run the .c files, a project must be created then the files must be added specifying no target membership, otherwise Xcode will compile the entire files three as one executable and the errors encounter would prevent the program to run.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .