- In this tutorial, we will learn to install C in Windows, Mac, and Linux. Install C on Windows. We will use an open-source Integrated Development environment named Code::Blocks which bundles a compiler (named gcc offered by Free Software Foundation GNU), editor and debugger in a neat package.
- O yea im not buying it until i find out everything. But i heard there is a program for gcc that works on mac. Im trying to find it right now. Were I in your position I would expect the software to come with the machine to run on my choice of operating system. As Ed said, this should be included.
GCC, the GNU Compiler Collection. The GNU Compiler Collection includes front ends for C, C, Objective-C, Fortran, Ada, Go, and D, as well as libraries for these languages (libstdc.).GCC was originally written as the compiler for the GNU operating system.The GNU system was developed to be 100% free software, free in the sense that it respects the user's freedom.
Apple’s Xcode development system is superb for developing applications, but sometimes you just want to write C or C++ code for research or school. Composing a serious chunk of code with vi is no longer acceptable,* so users in this frame of mind are now using Eclipse, a modern IDE, that’s also free. Here’s how to get gcc without installing Apple’s Xcode and then install Eclipse for C/C++ programming.What’s the Motivation?
At work recently, my wife was chatting with a colleague who was taking his first C++ class. She taught him how to use Eclipse on a Mac, even though he resisted at first. However, later, he came back and commented that the other students were trying to manage ever increasingly complex projects with the vi editor. It was taking them four, six, or even 20 hours in some cases to complete their homework each week. He finished his, typically, in 30 minutes. That’s the power of an IDE with a modern debugger. That last item, the debugger, can’t be emphasized enough. Print statements in your code are oh, so yesteryear with a tool like this. Time is money, and efficiency reflects on you as a programmer.So if you’re a scientist, researcher or engineer who wants to write some research code, not intended as a GUI app, in Java, C, C++ or Fortran, you need to dump vi as an editor* (or Emacs or Nedit or whatever) immediately and get with this kind of IDE. Things are moving far too fast nowadays not to make this important move. (Clearly, I’m speaking to an older crowd here.**)
To be perfectly clear, Apple’s Xcode is a fabulous development system for C, C++, Objective-C and even Fortran 77***. You can build native OS X and iOS apps. But many researchers and scientists aren’t interested in Xcode. They’ve come from a Linux or other UNIX platform, like IBM’s AIX, and they just want to carry on their research in Eclipse on a Mac. This how-to is primarily for them. But, as I mentioned above, students who are taking their first programming class and own a Mac will also find this discussion useful — indeed mandatory. Remember, this is an introduction to whet your appetite and get you launched, not a complete Eclipse tutorial.
Also, this how-to for the sake of simplicity focuses on C/C++, but Eclipse can handle a myriad of languages, including, but not limited to, Java and Fortran. Let’s start with C/C++.
Getting the gcc Compiler
As an aside, when you install Apple’s Xcode (free from the Mac App Store only in Lion), gcc is automatically installed in /usr/bin. But if you have a mind to work with just Eclipse and gcc, you’ll need a way to install gcc without, if it pleases you, installing Apple’s Xcode first. (For reference, here’s a how-to on installing Xcode and gcc.)
There are at least two places I know of where you can get a gcc installer package for OS X:
- Developer Tools 4.0 Source
- The OS X High Performance Computing page at Sourceforge managed by Dr. Gaurav Khanna at the University of Mass.
If you find other sources, let us know.
Installing Eclipse
Here’s a handy reference on where to get Eclipse for the Mac. Version 3.7 (“Indigo”) installs nicely in Lion and seems to work okay, but our household hasn’t put it to a grueling acid test with OS X 10.7.1.Eclipse Download for CC++, 64-bitThe download is a …tar.gz file, so move it where you want the Eclipse directory to be because when you double click it, the package will be unzipped and untared right there. After Eclipse is installed, you’ll see it as an app, just like any other, with this icon.
It’s easy to create an alias to eclipse.app, if you wish, and place it in your /Application directory. Because the Eclipse IDE itself is written in Java, if you’re running a clean copy of Lion, you’ll need to download the Java runtime before the Eclipse app will launch. Just double-click on Eclipse, and it’ll trigger the required Java runtime download. Nothing else to do. If you’re in Snow Leopard, the Java runtime is already there.
Java runtime downloadThis first thing you’ll see when you run Eclipse is that it asks for the location of your workspace. I typically create a directory in /Users/john called Programming where I do any code development. (Which is not much these days.)
Workspace SelectionAfter you select your workspace, you’ll be confronted with an Eclipse peculiarity. A welcome page will be launched that brings a real feeling of WTF to many users. Here it is.
Welcome screenThe trick is to realize that the icon on the far right, circled in red above, takes you into the workbench. For that named workspace, this one time is the only time you’ll see that screen.
Once the IDE is launched, there are just a few details before you can build a project. Again, for simplicity, lets stay with C/C++. From the Eclipse File menu, select “New” and then C++ Project.
Eclipse - New ProjectExperienced Eclipse users know the drill. Name the project, make sure you’ve identified the location of your compiler in the Toolchain, and so on. (It may be different than the default install by Apple’s Xcode. The screen shot below shows Apple’s install of gcc, marked as MacOSX GCC, because I’d previously installed Xcode on that Mac.)
ConfigurationNewbies may want to select the Hello World project just to see how things get set up. Make sure you work your way through the setup with Next > at the bottom and don’t click the Finish button too soon. A more detailed examination of this set up and the ins-and-outs of Eclipse are beyond the scope of this getting started article. The goal here is simply an introduction for Mac users. And you’re not alone. There’s a huge body of knowledge on the Internet about Eclipse. Here’s a start.
If all went well, you’ll be in the Eclipse IDE with the Hello World program in the editor and a Make file already created. This environment is called the Eclipse workbench.
Eclipse Workbench w/ default “Hello World” codeIt may take you some start-up time to learn Eclipse, but trust me, coding the old way will soon bog you down. The Xcode and Eclipse developers learn from each other, so if you ever decide to get into OS X or iOS development, having earned your wings in Eclipse will pay huge dividends later.
__________________
* I’m qualified to say this because I’ve lived and breathed vi for a living in the past.
** Remember, a lot of people are going back to school, learning or refreshing their programming skills.
*** You can write and compile Fortran 77 in Xcode with standard output, but you can’t access the Cocoa APIs and build native apps in F77.
Installing G++ on a Mac
This section is intended to get you quickly started with C++ programming on your Mac. We'll be installing GCC 4.8.1 and GDB through a tool called Homebrew. If you want an additional guide on all of the following steps (except for installing GCC), the one by Moncef Belyamani is quite helpful. When you follow it, ignore anything about installing Ruby; that is, stop after setting up git.
Homebrew
Homebrew 'installs the stuff that you need that Apple don't'. It's like Ubuntu's apt-get, where one can install packages easily from repositories. Instead of having to download, configure, and install something yourself, all you need to do is run one command, and Homebrew will take care of the rest for you.
Pre-requisites
Homebrew requires that you have either Xcode or the Xcode command line tools installed on your Mac. Xcode is a free integrated development environment similar to Eclipse designed by Apple and mainly intended for iOS development or targeting the clang
compiler. In this class, we will focus on gcc
.
Xcode is quite a big install, so if you do not want to install it, you can get away with just installing the Xcode command line tools. See a Stackoverflow discussion for instructions on how to install the command line tools regardless of whether you have Xcode installed.
Installing Homebrew
You need xcode command line tools to install Homebrew. It is very easy to install Homebrew. Open your terminal, and run the following command:
If this doesn't seem to do anything, try killing it (CTRL C) and running it again. Or checkout homebrew website.
GCC and GDB
Installing GCC
Gcc For Mac Download
First of all, you should make sure that your Homebrew is up-to-date. Check to see if it is so with the following:
As mentioned before, installing packages with Homebrew is very easy. First, we will add the repository from which the GCC package is available, so that Homebrew knows where to find the package we want. The repository is at https://github.com/Homebrew/homebrew-versions.
We do this by using the brew-tap
command. Keep your terminal open, and run the following command. (For more information on how brew-tap works, visit the Homebrew docs):
Next, we will actually install the GCC package. Run the following command:
It might take long time before the installation is complete. If you run into any issues, you can debug with:
Gcc Compiler For Mac
When done, run the following:
The result should look like this:
USC Wireless Warning
Many people have had issues running the brew install
commands while connected to USC Wireless. If you are having trouble, you can either try using a wired connection, a different wireless connection, or do the following:
- Download a homebrew cache
- Open Finder, press CMD (command) + SHIFT + G and type
/Library/Caches/Homebrew
Extract the contents of the .zip you downloaded inside of the folder you opened in the previous step. Do not extract any of the .tar.bz2 or .tar.gz inside of the .zip folder.This should look as follows:
Run
brew install gcc48
in the Terminal as instructed above.
Using G++
To compile with the newly installed G++ compiler, use g++-4.8
.
Gcc For Macos High Sierra
(Advanced) Aliasing g++
If you prefer calling g++ directly, you can also create a bash alias, as follows:
Put these two lines at the end of the file ~/.bashrc
, and run:source ~/.bashrc
For more information on bash alias, take a look at the GNU Docs.
Installing GDB
Here also we use Homebrew. The following instruction has been taken from GDB on OS X Mavericks and Xcode 5 guide. To install, run the following brew command.
Check if it's installed:
The result should be gdb version 7 or higher.
Download Gcc For Mac
Codesigning gdb
gdb is not going to debug yet. You'll get an error message like 'please check gdb is codesigned'. You need to create a certificate and sign gdb. By doing so you're telling the operating system that gdb is authorized to attach to other processes for debugging purposes. The following instructions have been taken from this Code Signing guide.
- Open application 'Keychain Access' (/Applications/Utilities/Keychain Access.app)
In Keychain Access, select the 'login' keychain in the 'Keychains' list in the upper left hand corner of the window.
Open the menu item in /Keychain Access/Certificate Assistant/Create a Certificate...
Choose a name ('lldb_codesign' in the example, but you can use anything you want), set 'Identity Type' to 'Self Signed Root', and set 'Certificate Type' to 'Code Signing'. Click 'Create'.
Click continue, continue and done.
Click on the “My Certificates” category on the left side and double click on the new “lldb_codesign” certificate.
Open the context menu for 'Trust' (click the triangle) and change the following:
When using this certificate: Always Trust
Now close this window, and enter your login password to confirm this change.
Option-drag (this meaning holding the option key down and dragging) the new 'lldb_codesign' certificate from the login keychain to the System keychain in the Keychains pane of the main Keychain Access window to make a copy of this certificate in the System keychain. You'll have to authorize a few more times, set it to be 'Always trusted' when asked.
Switch to the 'System' keychain and drag a copy of the 'lldb_codesign' you just made onto the Desktop.
Switch to Terminal and then run the following command (copy paste it!):
sudo security add-trust -d -r trustRoot -p basic -p codeSign -k /Library/Keychains/System.keychain ~/Desktop/lldb_codesign.cer
Then right click on the 'lldb_codesign' certificate in the 'System' keychain (not 'Login') and select 'delete' to delete it from the 'System' keychain.
Then reboot your system/computer.
Finally you can sign gdb:
codesign -s lldb_codesign /usr/local/bin/gdb
If this command doesn't work...then panic! Just kidding, be sure that you have gdb installed and that gdb is actually installed in /usr/local/bin. You may want to try 'which gdb' in your Terminal to figure out where it is.
Finally, remove the lldb_codesign.cer file that's sitting on your desktop, and gdb should be working at this point. :)