How do I compile a .c file on my Mac?

CMacos

C Problem Overview


How do I compile a .c file on my Mac?

C Solutions


Solution 1 - C

You'll need to get a compiler. The easiest way is probably to install XCode development environment from the CDs/DVDs you got with your Mac, which will give you gcc. Then you should be able compile it like

gcc -o mybinaryfile mysourcefile.c

Solution 2 - C

You will need to install the Apple Developer Tools. Once you have done that, the easiest thing is to either use the Xcode IDE or use gcc, or nowadays better cc (the clang LLVM compiler), from the command line.

According to Apple's site, the latest version of Xcode (3.2.1) only runs on Snow Leopard (10.6) so if you have an earlier version of OS X you will need to use an older version of Xcode. Your Mac should have come with a Developer Tools DVD which will contain a version that should run on your system. Also, the Apple Developer Tools site still has older versions available for download. Xcode 3.1.4 should run on Leopard (10.5).

Solution 3 - C

Just for the record in modern times,

for 2017 !

1 - Just have updated Xcode on your machine as you normally do

2 - Open terminal and

$ xcode-select --install

it will perform a short install of a minute or two.

3 - Launch Xcode. "New" "Project" ... you have to choose "Command line tool"

Note - confusingly this is under the "macOS" tab.

choose this one

Select "C" language on the next screen...

enter image description here

4- You'll be asked to save the project somewhere on your desktop. The name you give the project here is just the name of the folder that will hold the project. It does not have any importance in the actual software.

5 - You're golden! You can now enjoy c with Mac and Xcode.

you're golden

Solution 4 - C

In 2017, this will do it:

cc myfile.c

Solution 5 - C

You can use gcc, in Terminal, by doing gcc -c tat.c -o tst

however, it doesn't come installed by default. You have to install the XCode package from tour install disc or download from http://developer.apple.com

Here is where to download past developer tools from, which includes XCode 3.1, 3.0, 2.5 ...

http://connect.apple.com/cgi-bin/WebObjects/MemberSite.woa/wo/5.1.17.2.1.3.3.1.0.1.1.0.3.3.3.3.1

Solution 6 - C

Use the gcc compiler. This assumes that you have the developer tools installed.

Solution 7 - C

Ondrasej is the "most right" here, IMO.
There are also gui-er ways to do it, without resorting to Xcode. I like TryC.

> Mac OS X includes Developer Tools, a developing environment for making > Macintosh applications. However, if someone wants to study programming > using C, Xcode is too big and too complicated for beginners, to write > a small sample program. TryC is very suitable for beginners.

enter image description here > You don't need to launch a huge Xcode application, or type unfamiliar > commands in Terminal. Using TryC, you can write, compile and run a C, > C++ and Ruby program just like TextEdit. It's only available to > compile one source code file but it's enough for trying sample > programs.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDavidView Question on Stackoverflow
Solution 1 - COndraSejView Answer on Stackoverflow
Solution 2 - CAdam BatkinView Answer on Stackoverflow
Solution 3 - CFattieView Answer on Stackoverflow
Solution 4 - Caeb0View Answer on Stackoverflow
Solution 5 - CAlBlueView Answer on Stackoverflow
Solution 6 - CtvanfossonView Answer on Stackoverflow
Solution 7 - CAlex GrayView Answer on Stackoverflow