Contents
Introduction
What is this "gcc?"
Getting gcc
Installing gcc
Environment Variables
A Quick Test
Where to go from Here?
Important LinksIntroduction
Solaris is a great operating system to be sure, but one thing sticks out as a glaring omission especially if you're used to using Linux or one of the *BSDs. There is no compiler installed! When Sun went from SunOS to Solaris, they removed the compiler. In it's place, Sun left a "stub" as /usr/ucb/cc which, when executed - leaves a less than desirable (or helpful) error message on your screen - notice the word "optional" also:
/usr/ucb/cc: language optional software package not installedSo basically what they're saying is that you don't have a compiler installed, it's optional - but they don't tell you that it doesn't come with Solaris. You must either go out and buy Sun's Visual WorkShop product (which is now called Forte C) at $1,295.00US (for one user), or install GNU's C compiler (gcc) which is free.
Why didn't Sun include a compiler with Solaris? There are several reasons, if you think about it:
- Most people don't use compilers anyway
- Have another product to sell, plus support
- Maintain control/consistency in development environments
While these reasons might all be valid, in my opinion it hurts Sun somewhat in that a development environment for people to create more software and applications for Solaris are hindered in that a compiler doesn't come with Solaris - but at additional cost. One could argue that those interested in developing for the Solaris platform could and probably would follow what I'm about to discuss below or in a higher-budget setting, ante up the cost of Sun's product.
What is this "gcc?"
First off, for those that don't know quite what "C" is, here's a blurb from an excellent text called, "The Development of the C Language" by Dennis M. Ritchie, one of the fathers of C as it is known today:
"C came into being in the years 1969-1973, in parallel with the early development of the Unix operating system; the most creative period occurred during 1972. Another spate of changes peaked between 1977 and 1979, when portability of the Unix system was being demonstrated. In the middle of this second period, the first widely available description of the language appeared: The C Programming Language, often called the `white book' or `K&R' [Kernighan 78]. Finally, in the middle 1980s, the language was officially standardized by the ANSI X3J11 committee, which made further changes. Until the early 1980s, although compilers existed for a variety of machine architectures and operating systems, the language was almost exclusively associated with Unix; more recently, its use has spread much more widely, and today it is among the languages most commonly used throughout the computer industry."Let's assume that you know what C is, and why you're here.
The GNU (GNU's Not Unix) Project's first version of gcc was released as v1.0 on May 23, 1987 (a beta version came out in March). Along the course of it's history (in December of 1997), there was a split in the gcc branch, and EGCS was born. Basically, this was a branch to allow all manners of optimizations and experiments to be performed on the compiler while the stable branch remained. Later, in July of 1999 both projects were merged into one again bringing the best features together. It was also renamed from "GNU C Compiler" to "GNU Compiler Collection" since it now allowed for languages to be "plugged in" allowing for more than just C to be supported. Currently Ada95, C, C++, Objective C, Chill, Fortran, Java and Pascal are available. Another nice feature about gcc is that it's very portable and available on many platforms - and as such, means that you can "cross-compile" for other platforms. It's quite a powerful system, producing optimized code. You really can't beat it for the price.
We'll be discussing primarily the basic C/C++ component of gcc as it is the core component of compiling any Open Source tools that you might find out on the Internet. There are an incredible number of Open Source applications available that are written in portable, ANSI C - that compile quite nicely with gcc available. While many of these are available as Solaris packages, most are not and you are left to compile them on your own. If you're a hobbyist and just wish to see what's out there or create your own programs as a student, purchasing Sun's solution just isn't justifiable.
Specifically, we'll go through the installation of gcc v2.95.2 which is the current stable and suggested release - and also the one available from Sunfreeware.com in a Solaris package format.
Getting gcc
The quickest and easiest way to get gcc and start using it immediately is to get it in a Solaris package format. This allows you to download one file, install it - and go to work. Later, if you decided to remove it, you can do so with one command. You can also get the source code for a different version of gcc and compile it yourself, but you'll still need to install a binary package first.
For the scope of this article, we'll just go with a simple binary package install as this will be sufficient for almost all situations. Unless you plan on developing gcc itself, or want to see what it's made of - this will be the ideal solution.
The first step is to obtain the gcc package, and one place that comes to mind is Sunfreeware.com as it has the packages available for several versions of Solaris and platforms. Hit that site and look on the top right pane, where you'll see several versions and platforms of Solaris listed - pick yours. On the lowermost right pane you'll see a list of software packages. Scroll down and select "gcc-2.95.2" (available for all platforms listed, save for Solaris v2.5). On the center pane you will now see the entry for gcc called "gcc-2.95.2-sol7-sparc-local.gz" or similar, depending on the platform and version of Solaris you have chosen. Using Netscape Communicator for example, you'll want to shift-click to download that link's package.
You can also get the package by just clicking one of the following links here, which link to the package itself on Sunfreeware.com (remember to shift-click the link to download):
More versions are available on the site, so you should check there if your platform isn't listed above.
Installing gcc
Following the download procedures above, you should be left with a package named accordingly to your platform - and ending with ".local" in the filename. In order to install this package, you should be root or have root access as it installs in your /usr/local filesystem - or creates it if you don't have one. So, become root somehow by logging in as root, using the su or sudo commands or however your system is set up.
Next, issue the pkgadd (PacKaGe ADD or "package add") command to install it on the system, changing the actual package name to fit the one you downloaded, for example:
# pkgadd -d gcc-2.95.2-sol7-sparc-local (hit return)You will then be presented with the standard pkgadd response:
The following packages are available: 1 SMCgcc gcc (sparc) 2.95.2 Select package(s) you wish to process (or 'all' to process all packages). (default: all) [?,??,q]:...to which you'll want to answer with an "all" or just hit return, since that is the default value. You'll see the files being installed start to scroll by on your screen and the package will be installed! Yes, it's that simple - and why we like packages, right?
You may be presented with a few questions along the way if you haven't installed any other packages in this fashion before, including the creation of the /usr/local directory or perhaps the permissions settings on various "conflicting" files. Just answer "y" to these questions and hit return.
Once the package has completed installing, you'll be returned to the command prompt after a brief line that reads, hopefully:
. . . Installation of <SMCgcc> was successful. #You can verify this and see some information about the package with the pkginfo (PacKaGe INFOrmation or "package information") command:
# pkginfo -l SMCgcc (hit return) PKGINST: SMCgcc NAME: gcc CATEGORY: application ARCH: sparc VERSION: 2.95.2 BASEDIR: /usr/local VENDOR: Free Software Foundation PSTAMP: Steve Christensen INSTDATE: Mar 12 2001 13:26 EMAIL: steve@smc.vnet.net STATUS: completely installed FILES: 349 installed pathnames 10 shared pathnames 2 linked files 21 directories 23 executables 127203 blocks used (approx) #Yours may vary slightly, but should more or less look like the above. You're all set, and should now log out as root and "be yourself" again.
Environment Variables
You may want to define a certain set of environment variables so that gcc knows where to find things, and will come in handy when compiling other Open Source software. Many utilities rely on environment variables like configure and make as well as gcc. If you are using the GNOME environment especially, it's good to set these up.
If you're using bash as your shell, just add the following lines into your ~/.bashrc file, or modify any existing lines to include those shown. You should of course, adjust these paths to your system - but for most situations, these will be appropriate:
export PATH=/opt/gnome/bin:/usr/local/bin:/opt/netscape:/usr/ccs/bin:$PATH export LD_LIBRARY_PATH=/opt/gnome/lib:/usr/local/lib:/usr/lib:/lib export GNOME_PATH=/opt/gnome export MANPATH=/usr/local/man:/opt/gnome/man:$MANPATH export CPPFLAGS="-I/opt/gnome/include -I/usr/openwin/share/include/X11/extensions -I/usr/openwin/include"For the changes to take effect, you'll either need to log out and back in again, or start a fresh terminal window. You can also just enter "source ~/.bashrc" at the command-line to do the same thing. As a quick test, try entering the following at the command-line and you should see something like this:
$ set | grep LD_LIBRARY_PATH LD_LIBRARY_PATH=/opt/gnome/lib:/usr/local/lib:/usr/lib:/lib $These environment variables tell various utilities where files reside on your system. Important things like libraries, include files, man pages and even GNOME itself.
A Quick Test
If you want to make sure that it's working and wish to perform a quick test, why not use the old standby of a "hello world" program? What article on a language would be complete without it...
Using the text editor of your choice, create a file called "helloworld.c" in your home directory and enter the following code:
main() { printf("Hello world.\n"); }Okay, so it's not the best C program in terms of what it does or ANSI C correctness, but it will suffice to test the install. Next, on the command line we'll compile this C source code into a usable Solaris binary:
$ gcc helloworld.c -o helloworld (hit return) $If all goes well, you'll be returned to the command line with no further output. Pretty simple, no? What you're left with is a binary called "helloworld" which is ready to execute:
$ ./helloworld (hit return) Hello world. $If this is what you got, congratulations! If not, resolve any errors you get in response to the compiler (known as warnings) and try again. You've now successfully installed gcc and compiled a very simple test program.
You're now ready to move on to bigger and better things - enjoy!
Where to go from Here?
You could install additional tools and packages like gdm - the GNU debugger. If you're using the GNOME desktop, you could add glade (an interface builder) and gIDE (an IDE) and end up with an environment not much unlike this (CDE theme used, but it's GNOME):
![]()
Enough Open Source tools exist to make programming with these tools quite easy and comparable to the commercial environments out there. A future article covering these tools and more may be in store.
Important Links
The gcc home page
Using and Porting the GNU Compiler Collection (gcc manual)
Standard C++ Library v3 (libstdc reference)