Mac OS X: Install GCC Compiler with Xcode

I have Mac OS X Mountain Lion. I need to compile a few apps and Perl modules. I already installed Xcode from app store but I’m unable to find gcc compiler or make command. How do I install gcc on Mac OS X 10.8.x?

Tutorial details
Difficulty Easy (rss)
Root privileges Yes
Requirements Mac OS X Mountain Lion
Time Depends on the internet speed
Xcode includes command line development tools such as gcc and friends.

Step #1: Install Xcode on a Apple Mac OS X

First, make sure Xcode is installed. If it is not installed on OS X, visit app store and install Xcode.

Fig.01: Make sure Xcode developer tools are install OS X

Step #2: Install gcc/LLVM compiler on OS X

Once installed, open Xcode and visit:

Xcode menu > Preferences > Downloads > choose "Command line tools" > Click "Install" button:

Fig.02: Installing gcc compiler on Mac OS X

Xcode will download package and install copies of the core command line tools and system headers into system folders, including the LLVM compiler, linker, and build tools.

Step #3: Verification

Open a terminal app and type the following commands:
$ gcc --version
$ whereis gcc
$ whereis make

Sample outputs:

Fig.03: Verify gcc compiler installation on Mountain Lion OS X

Testing sample “Hello world” C program

Create a text file called a.c as follows using a text editor such as vi or cat command:

/* a.c - demo for os x */
#include<stdio.h>
int main(void){
 printf("Hello world\n");
 return 0;
}

To compile, enter:
$ make a
Run it as follows:
$ ./a
Sample outputs:

Fig.04: Compiling and running sample “Hello world” C program on Mountain Lion 10.8.4

See also

And, there you have it, the gcc version 4.2.1 installed and working correctly on the latest version of Mac OS X 10.8.4. In Apple’s version of GCC, both cc and gcc are actually symbolic links to the llvm-gcc compiler. Similarly, c++ and g++ are links to llvm-g++. For more information and examples see the following man pages:

$ man gcc $ man make


🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 28 comments so far... add one

CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
28 comments… add one
  • meow Jun 9, 2013 @ 14:19

    There is no need to install huge xcode consumes gigabytes of disk space (if you are not a apple developer), just go to the apple’s developer page and download the “command line tool for xcode” package and install it, that would be just sufficient.

  • ydk2 Jun 22, 2013 @ 7:39

    Isn’t GCC is LLVM with GCC support and many automake, m4 etc… project is not enable to build.

  • Mustafa Jul 30, 2013 @ 21:51

    thank u very very very much you are really clever

  • Reva Sep 18, 2013 @ 2:55

    THANK YOU SO MUCH!!! Everything else I tried didn’t seem to work. Finally this did!!!!

  • john Sep 27, 2013 @ 22:59

    Thank you very much. It worked.

  • Asaf Nov 6, 2013 @ 16:09

    I installed the Xcode Command Line Tools, and when I try to find gcc in the terminal
    tell me this:

    new-host:~ asaffeldman$ $gcc –version
    -bash: –version: command not found

    ?? help

    • Martin Dec 13, 2013 @ 15:50

      try:

      gcc --version

      with 2 dashes and no $ in front of it
      hope it helps!

  • Mohamed Magdy Nov 30, 2013 @ 8:39

    I anderstand

  • Simo Dec 18, 2013 @ 13:54

    Clear , efficient , thanks !

  • ydk2 Jan 15, 2014 @ 14:34

    I have GCC 4.8.1 with x86, x86_64 and PPC on Lion with ObjC, C/C++11, Fortan, Ada Go support.
    And now I can compile PHP, WxWidgets, QT and many other source without problems like on Apple Clang vesion. In xcode too.
    Gcc builded with llvm-gcc-4.2.

  • viky Mar 8, 2014 @ 13:02

    i m doing the c programming on terminal my keyword is not highlited by compiler,
    strstr function is not working why plz resolve my problm

  • Daniyah Jun 4, 2014 @ 10:38

    hi
    when I typed make a I got this message:
    make: *** No rule to make target `a’. Stop.

    please help me.
    Thanks.

    • Ryan Jan 24, 2015 @ 20:25

      I got the same error when I didn’t spell the filename with the exact case.

  • vanessa Jul 14, 2014 @ 23:04

    Hi , I installed xcode and command line tools just as you said but when I try the command gcc –version I get this error
    Error: could not stat active Xcode path ‘/Volumes/Xcode/Xcode.app/Contents/Developer’. (No such file or directory)

    Is my xCode in the wrong place? It’s installed and under Applications, please help

  • Shubham Jain Jul 22, 2014 @ 2:41
    Ld /Users/shubhamjain/Library/Developer/Xcode/DerivedData/Production_rates-eqoyfionzpuxskflgywwzthhilri/Build/Products/Debug/Production\ rates normal x86_64
        cd "/Users/shubhamjain/Documents/Xcode/Production rates"
        export MACOSX_DEPLOYMENT_TARGET=10.9
        /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk -L/Users/shubhamjain/Library/Developer/Xcode/DerivedData/Production_rates-eqoyfionzpuxskflgywwzthhilri/Build/Products/Debug -F/Users/shubhamjain/Library/Developer/Xcode/DerivedData/Production_rates-eqoyfionzpuxskflgywwzthhilri/Build/Products/Debug -filelist /Users/shubhamjain/Library/Developer/Xcode/DerivedData/Production_rates-eqoyfionzpuxskflgywwzthhilri/Build/Intermediates/Production\ rates.build/Debug/Production\ rates.build/Objects-normal/x86_64/Production\ rates.LinkFileList -mmacosx-version-min=10.9 -stdlib=libc++ -Xlinker -dependency_info -Xlinker /Users/shubhamjain/Library/Developer/Xcode/DerivedData/Production_rates-eqoyfionzpuxskflgywwzthhilri/Build/Intermediates/Production\ rates.build/Debug/Production\ rates.build/Objects-normal/x86_64/Production\ rates_dependency_info.dat -o /Users/shubhamjain/Library/Developer/Xcode/DerivedData/Production_rates-eqoyfionzpuxskflgywwzthhilri/Build/Products/Debug/Production\ rates
    
    Undefined symbols for architecture x86_64:
      "_main", referenced from:
         implicit entry/start for main executable
    ld: symbol(s) not found for architecture x86_64
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    

    I AM GETTING THIS ERROR IN XCODE WHILE I AM BUILDING A PROGRAM IN C++

  • gina4eva Sep 5, 2014 @ 18:14

    I don’t have the Command Line option in my preference. Help!

  • billy Sep 19, 2014 @ 13:58
    Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
    Apple LLVM version 6.0 (clang-600.0.51) (based on LLVM 3.5svn)
    Target: x86_64-apple-darwin13.4.0
    Thread model: posix
    
  • fred Feb 12, 2015 @ 23:02

    I have xcode. I see gcc on my mac: Macintosh/Developer/usr/bin/gcc-4.0
    But as soon as I type gcc whatever in the terminal because I want to compile a file called hpgl2eagle1.c to be able to use it, it outputs an error: gcc command not found.

  • Zahra Mar 3, 2015 @ 18:57

    Does it work on Mac air?

  • jane May 30, 2015 @ 10:50

    i can’t find command line tools on my xcode. >< my xcode version is 6.3.2

    • DP Jun 1, 2015 @ 7:45

      Open the Terminal app and cd to /Applications/Xcode.app/ path:

      cd /Applications/Xcode.app/

  • Brian Jul 29, 2015 @ 15:04

    HELP; In the downloads, I don’t see command line tools.

  • Nam Nov 22, 2015 @ 4:35

    Please help

    i have the x-code and when i typed gcc –version it gave
    ‘Configured with: –prefix=/Applications/Xcode.app/Contents/Developer/usr –with-gxx-include-dir=/usr/include/c++/4.2.1
    Apple LLVM version 7.0.0 (clang-700.1.76)
    Target: x86_64-apple-darwin14.5.0
    Thread model: posix’
    and when i type ls -l /usr/bin/gcc
    ‘-rwxr-xr-x 1 root wheel 14160 Sep 28 2014 /usr/bin/gcc’
    so i cant go beyond this. Can someone please help?

  • Via Feb 14, 2016 @ 16:22

    Hi nixCraft,
    this advice helped a lot.
    thank you very much

  • Manny Mar 5, 2016 @ 1:38

    After you type:
    /*
    $ where is make
    /usr/bin/make
    Manny -MBP:~ Manny$ []
    */
    the 3rd line comes out again….I would like to know what do you type next to start creating your source code?

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Problem posting comment? Email me @ webmaster@cyberciti.biz