C Programming/Using a Compiler
From Wikibooks, the open-content textbooks collection
[edit] Dev-C++
Dev C++, as mentioned before, is an Integrated Development Enviroment(IDE) for the C++ programming language, available from Bloodshed Software.
C++ is a programming language which contains within itself most of the C language, plus a few extensions - as such, most C++ compilers also compile C programs, sometimes with a few adjustments (like invoking it with a different name or commandline switch). Therefore, you can use Dev C++ for C development.
However, Dev C++ is not the compiler: It is designed to use the MinGW or Cygwin versions of GCC - both of which can be downloaded as part of the Dev C++ package, although they are completely different projects.
Dev C++ simply provides an editor, syntax highlighting, some facilities for the visualisation of code (like class and package browsing) and a graphical interface to the chosen compiler. Because Dev C++ analyses the error messages produced by the compiler and attempts to distinguish the line numbers from the errors themselves, the use of other compiler software is discouraged since the format of their error messages is likely to be different.
The latest version of Dev-C++ is a beta for version 5 - as such, it still has a significant number of bugs. However, all the features are there and it is quite usable - as such, it is still considered one of the best free software C IDEs available for Windows.
A version of Dev C++ for Linux is in the pipeline; it is not quite usable yet, however Linux users already have a wealth of IDEs available to them (for example KDevelop and Anjuta.) Also, almost all the graphical text editors, and other common editors such as emacs and vi(m), support syntax highlighting.
[edit] GCC
The GNU Compiler Collection (GCC) is a free set of compilers developed by the Free Software Foundation.
- Steps for Obtaining the GCC Compiler if You're on GNU/Linux
On GNU/Linux, Installing the GNU C Compiler can vary in method from distribution to distribution.
- For Redhat, get a GCC RPM, e.g. using Rpmfind and then install (as root) using
rpm -ivh gcc-version-release.arch.rpm - For Fedora Core, install the GCC compiler (as root) by using
yum install gcc. - For Mandrake, install the GCC compiler (as root) by using
urpmi gcc - For Debian, install the GCC compiler (as root) by using
apt-get install gcc. - For Ubuntu, install the GCC compiler (along with other necessary tools) by using
sudo aptitude install build-essential, or by using Synaptic. You do not need Universe enabled. - For Slackware, the package is available on their website - simply download, and type
installpkg gcc-xxxxx.tgz - For Gentoo, you should already have GCC installed as it will have been used when you first installed. To update it run (as root)
emerge -uav gcc - For Arch Linux, install the GCC compiler (as root) by using
pacman -Sy gcc. - For FreeBSD, NetBSD, OpenBSD, DragonFly BSD, Darwin the port of GNU gcc is available in the base system, or it could be obtained using the ports collection or pkgsrc.
- If you cannot become root, get the GCC tarball from ftp://ftp.gnu.org/ and follow the instructions in it to compile and install in your home directory. Be warned though, you need a C compiler to do that - yes, GCC itself is written in C.
- You can use some commercial C compiler/IDE.
- Steps for Obtaining the GCC Compiler if You're on Windows
There are two ways to use GCC on Windows: Cygwin and MinGW. Applications compiled with Cygwin will not run on any computer without Cygwin, so MinGW is recommended. MinGW is simpler to install, and takes less disk space.
To get Cygwin, do this:
- Go to http://www.cygwin.com and click on the "Install Cygwin Now" button in the upper right corner of the page.
- Click "run" in the window that pops up, and click "next" several times, accepting all the default settings.
- Choose any of the Download sites ("ftp.easynet.be", etc.) when that window comes up; press "next" and the Cygwin installer should start downloading.
- When the "Select Packages" window appears, scroll down to the heading "Devel" and click on the "+" by it. In the list of packages that now displays, scroll down and find the "gcc-core" package; this is the compiler. Click once on the word "Skip", and it should change to some number like "3.4" etc. (the version number), and an "X" will appear next to "gcc-core" and several other related packages that will now be downloaded.
- Click "next" and the compiler as well as the Cygwin tools should start downloading; this could take a while. While you're waiting, go to http://www.crimsoneditor.com and download that free programmer's editor; it's powerful yet easy to use for beginners.
- Once the Cygwin downloads are finished and you have clicked "next", etc. to finish the installation, double-click the Cygwin icon on your desktop to begin the Cygwin "command prompt". Your home directory will automatically be set up in the Cygwin folder, which now should be at "C:\cygwin" (the Cygwin folder is in some ways like a small unix/linux computer on your Windows machine -- not technically of course, but it may be helpful to think of it that way).
- Type "gcc" at the Cygwin prompt and press "enter"; if "gcc: no input files" or something like it appears you have succeeded and now have the gcc compiler on your computer (and congratulations -- you have also just received your first error message!).
To get MinGW, do this:
- Go to http://downloads.sourceforge.net/mingw/MinGW-5.1.4.exe and save this to your hard drive.
- Once the download is finished, open it and follow the instructions. You can also choose to install additional compilers, or the tool Make, but these aren't necessary.
- Now you need to set your PATH. Right-click on "My computer" and click "Properties". Go to the "Advanced" tab and click on "Environment variables". Go to the "System variables" section and scroll down until you see "Path". Click on it, then click "edit". Add ";C:\mingw\bin\" (without the quotes) to the end.
- To test if GCC works, open a command prompt and type "gcc". You should get the message "gcc: no input files". If you get this message, GCC is installed correctly.
The current stable (usable) version of GCC is 4.2.1 published on 2007-07-21, which supports several platforms. In fact, GCC is not only a C compiler, but a family of compilers for several languages, such as C++, Ada Java, and Fortran.