techclinchxcode

How to compile C and C++ programs on Xcode

Need to learn How to compile C and C++ programs on Xcode?? …here comes. When you MAC and you want to do C and C++ programming then you need to something extra to make your Mac OS X , which is a linux  version. Here are following steps to be followed if you want to know how to compile C/C++ programs on Xcode then :

 Instructions for Mac OS 10.9 (Mavericks) with Xcode 5
  1. Install Xcode 5 from the Mac App Store. Xcode is Apple’s software development environment, a set of tools that aim to make it easier to write Mac software. After you launch the App Store application (/Applications/App Store), you’ll need to

 sign in with your Apple ID, or create an account to download anything. Xcode 5 is available for free (search for ‘xcode‘), but you’ll still need to supply your credit card info if you haven’t already used your account.

    • When you download it from the App Store, Xcode 5.0 (the current version as of December 2013) will be installed directly in your /Applications directory. However, before you can proceed with the GNU compiler install you must perform one critical additional step.
      • Launch Xcode and install any available updates. Quit Xcode.
      • Launch Terminal.app (in /Applications/Utilities, hopefully you know that)
      • Install the Command Line Tools for OS X Mavericks by typing
        xcode-select --install 

        This will open a dialog box to install the Command Line Tools (OS X Mavericks) for Xcode package. Install following the standard procedure.

  1. Download the latest precompiled GNU compilers. That’s right, precompiled compilers. Xcode includes the GNU C and C++ compilers, but the versions are sometimes a bit dated. Fortunately, the folks who run the High Performance Computing for Mac OS X website keep recent versions of the GNU compilers available on their site. What’s great is that the compilers are pre-built, meaning that all you have to do is download and extract the files to install. Below is a bit more detail on the install process.
    • Grab the latest archive of the GNU compilers from the HPC on Mac OS X site
    • In Terminal.app, change directories to where the GCC file was downloaded (usually, ~/Downloads)
    • If your browser didn’t automatically unzip the file (still zipped if the file ends with .gz), you can unzip the file by typing
      gunzip gcc-4.9-bin.tar.gz
    • To install (as described on the HPC on Mac OS X site), type
      sudo tar xvf gcc-4.9-bin.tar -C /

      Beware that using sudo can do major damage to your computer if you aren’t careful. Before hitting return, you should confirm you have typed things in exactly as listed above. In case you’d like to know what this set of commands does, sudo runs a program with administrator privileges and allows commands that follow to read/write/execute in directories normally protected from user modification. tar is an archive program and the flags xvf tell tar to extract files, give verbose output and the file name follows f. The -C flag tells the archive to be extracted to the root (/) directory, which puts the archived files in the /usr/local subdirectory, in accordance with how they are packaged in the tarfile.

    • To ensure the newly installed compilers are used by default, you’ll want to add the following line to your shell resource file. You can determine which shell you are using by typing
      echo $SHELL

      in Terminal.

      • If that returns /bin/bash (the default for Mac OS X since Panther, I believe), type the following:
        echo 'export PATH=/usr/local/bin:$PATH' >> ~/.bash_profile

        If you are using a terminal emulator other than Terminal.app or iTerm2 on your Mac (such as xterm in the Xquartz package), you may find that you need to type the following to find the newly installed compilers.

        echo 'export PATH=/usr/local/bin:$PATH' >> ~/.bashrc
      • If that returns /bin/tcsh or /bin/csh, you should type
        echo 'setenv PATH /usr/local/bin:$PATH' >> ~/.tcshrc

        for /bin/tcsh or

        echo 'setenv PATH /usr/local/bin:$PATH' >> ~/.cshrc

        for /bin/csh.

    • You should now be able to confirm the compilers are properly installed by opening a new Terminal window and typing gcc -vg++ -v or gfortran -v.
Instructions for Mac OS 10.10 (Yosemite) with Xcode 6
  1. Install Xcode 6 from the Mac App Store. Xcode is Apple’s software development environment, a set of tools that aim to make it easier to write Mac software. After you launch the App Store application (/Applications/App Store), you’ll need to sign in with your Apple ID, or create an account to download anything. Xcode 6 is available for free (search for ‘xcode‘), but you’ll still need to supply your credit card info if you haven’t already used your account.
    • When you download it from the App Store, Xcode 6.1 (the current version as of November 2014) will be installed directly in your /Applications directory. However, before you can proceed with the GNU compiler install you must perform one critical additional step.
      • Launch Xcode and install any available updates. Quit Xcode.
      • Launch Terminal.app (in /Applications/Utilities, hopefully you know that)
      • Install the Command Line Tools for OS X Yosemite by typing
        xcode-select --install 

        This will open a dialog box to install the Command Line Tools (OS X Yosemite) for Xcode package. Install following the standard procedure.

  2. Download the latest precompiled GNU compilers. That’s right, precompiled compilers. Xcode includes the GNU C and C++ compilers, but the versions are sometimes a bit dated. Fortunately, the folks who run the High Performance Computing for Mac OS X website keep recent versions of the GNU compilers available on their site. What’s great is that the compilers are pre-built, meaning that all you have to do is download and extract the files to install. Below is a bit more detail on the install process.
    • Grab the latest archive of the GNU compilers from the HPC on Mac OS X site
    • In Terminal.app, change directories to where the GCC file was downloaded (usually, ~/Downloads)
    • If your browser didn’t automatically unzip the file (still zipped if the file ends with .gz), you can unzip the file by typing
      gunzip gcc-4.9-bin.tar.gz
    • To install (as described on the HPC on Mac OS X site), type
      sudo tar xvf gcc-4.9-bin.tar -C /

      Beware that using sudo can do major damage to your computer if you aren’t careful. Before hitting return, you should confirm you have typed things in exactly as listed above. In case you’d like to know what this set of commands does, sudo runs a program with administrator privileges and allows commands that follow to read/write/execute in directories normally protected from user modification. tar is an archive program and the flags xvf tell tar to extract files, give verbose output and the file name follows f. The -C flag tells the archive to be extracted to the root (/) directory, which puts the archived files in the /usr/local subdirectory, in accordance with how they are packaged in the tarfile.

    • To ensure the newly installed compilers are used by default, you’ll want to add the following line to your shell resource file. You can determine which shell you are using by typing
      echo $SHELL

      in Terminal.

      • If that returns /bin/bash (the default for Mac OS X since Panther, I believe), type the following:
        echo 'export PATH=/usr/local/bin:$PATH' >> ~/.bash_profile

        If you are using a terminal emulator other than Terminal.app or iTerm2 on your Mac (such as xterm in the Xquartz package), you may find that you need to type the following to find the newly installed compilers.

        echo 'export PATH=/usr/local/bin:$PATH' >> ~/.bashrc
      • If that returns /bin/tcsh or /bin/csh, you should type
        echo 'setenv PATH /usr/local/bin:$PATH' >> ~/.tcshrc

        for /bin/tcsh or

        echo 'setenv PATH /usr/local/bin:$PATH' >> ~/.cshrc

        for /bin/csh.

    • You should now be able to confirm the compilers are properly installed by opening a new Terminal window and typing gcc -vg++ -v or gfortran -v.
  • Beware that using sudo can do major damage to your computer if you aren’t careful.