Sunday, February 8, 2009

Building C and C++ Programs

For a beginning programmer, sometimes the biggest problem is just getting a program to compile, as opposed to actually writing the program itself. That is why it is often helpful to do a little 'Hello, world!' program, just to get a feel for the system, how the compiler is invoked, and so on. So in this post, the reader will learn how to compile a 'Hello, world!' program for three different systems, in two different languages. The systems will be Linux, FreeBSD, and OpenSolaris. The first two will be similar in that they both (normally) employ gcc (GNU Compiler Collection, but formerly GNU C Compiler) as the standard system compiler. OpenSolaris is more interesting, as it makes the Sun C and C++ compilers available, as well as gcc. As for the languages, if you haven't already guessed, they will be C and C++. So let's begin with a C program, hello.c:

#include <stdio.h>

int main(void)
{
printf("Hello, world!\n");
return 0;
}



A simple program, one that lets the programmer concentrate on getting the compiler working, without having to worry about fixing source errors. Now then, the first thing to know is that on many (if not all) Unix like systems, the C compiler can generally be invoked with the command 'cc'. This may be the actual C compiler, named 'cc,' or the command may be a symlink to another compiler, such as gcc. In the latter case, the symlink is added for the sake of not breaking things. cc was the standard command for invoking the C compiler, and to take it away would, if nothing else, break tradition, not to mention some Makefiles as well. You can determine some basic information about where your C compiler resides (and if it is even installed!) by issuing the following command from the shell:

which cc


In the case of my OpenSolaris machine, this produces the following:

/usr/bin/cc


If I do an ls -l on /usr/bin/cc, I see that it is really just a symlink to another location on disk:

ls -l /usr/bin/cc
lrwxrwxrwx 1 root root 33 2009-02-08 22:52 /usr/bin/cc -> ../../opt/SunStudioExpress/bin/cc


All very interesting stuff. You could also do something like this:

cc -V


Which produces:

cc: Sun Ceres C 5.10 SunOS_i386 2008/10/22
usage: cc [ options] files.  Use 'cc -flags' for details


So in this case, at least, we know that when we use the command 'cc', we are invoking the Sun C compiler. So why don't we do that then - copy the program hello.c (above) into your favourite text editor, and save it as hello.c -- then, from the directory that you save the hello.c file, invoke the C compiler with the following command:

cc -o hello hello.c


and hit Enter. You should get your shell prompt back almost immediately. What you have done with the above command is to run the C compiler (cc), with the compiled output being called 'hello' (-o hello -- -o stands for output name. If you don't do the -o option, the program will still compile, with the executable being named a.out -- this is also a tradition in Unix like systems. If you run a.out, it will print 'Hello, world!' to the standard output. a.out stands for 'assembler output,' which is a fallback to older systems and procedures.) and the input source file to the compiler being hello.c

As you can see, there is not much to it. If you do an ls -l in the directory where you ran the compiler from, you should see the executable file, 'hello' You can run it by typing in:

./hello


The ./ part of ./hello means 'Run the program 'hello' which is found in this directory.
With OpenSolaris, you have the option to install another compiler as well, namely, gcc. You can issue the same commands to find out where gcc exists and which version you are using:
which gcc
/usr/bin/gcc

ls -l /usr/bin/gcc
lrwxrwxrwx 1 root root 14 2009-02-08 23:20 /usr/bin/gcc -> ../sfw/bin/gcc


Keep in mind that determining the version of gcc is slightly different than the way you do it for cc:
gcc -v


Note that it is a small v as opposed to a large V for cc. You can read more about the compiler command switches by doing man cc or man gcc to access the manual page for each compiler.

If you decide to use gcc, you ought to use two flags every time you compile. They are the -Wall (Warnings All) and the -Wextra (Warnings extra) flags. By using them, the compiler will give you lots of extra info with any problems your source code might have. Their use is indispensable, in my opinion. Use them like this:

gcc -Wall -Wextra -o hello hello.c


And here is the added bonus - if you can compile like this on OpenSolaris (Sun), then you can do it under Linux or FreeBSD, which come with gcc as the standard compiler.

Now then, on to compiling C++ programs. First, we need a suitable 'Hello, world!' program:

#include <iostream>

using namespace std;

int main()
{
cout << "Hello, world!" << endl;
return 0;
}
Now the interesting thing about C++ programs is that there are several standard ways of naming with a suffix. With C, a file is foo.c, such as hello.c -- but with C++ it might be hello.C (note the capital C), or hello.cc, or hello.cpp, or hello.cxx For our purposes, we will stick to hello.cc So how do we compile it? Well, if you want to go the traditional route, you would invoke the C++ compiler like this:
CC -o hello hello.cc
Note the capital CC; pretty imaginative, huh? That is simple, and easy to remember though. If you want to use the GNU C++ compiler, you would do:
g++ -o hello hello.cc
Both of these methods would produce an executable named 'hello,' which could then be invoked as with './hello' at the command prompt. If you use g++, once again, you can (and should) use the -Wall -Wextra flags. Also, you can use g++ in the same fashion on any other system which has the GNU Compiler Collection available, such as Linux, or FreeBSD.

No comments:

Post a Comment