On a personal computer using Visual Studio,
you will create a .c file, e.g. hello.c (as in the previous example). You will then compile it to produce a .exe
file (e.g., hello.exe) and perhaps a .obj file (hello.obj). Consult your compiler documentation (each one
is different).
·
In Visual Studio, do a File|New
and you will see the following dialog...
·
Here, you must first select Win32 Console Application. Then press the ... button that appears to the
right of the Location
·
Now choose the location. You may need to create a folder on your file
system. In this case, I brought up the
Windows NT explorer and created the new folder Ece11-s98. Whatever you do, you should place the project
in a place such that you can easily find it later.
·
Now that you have specified a Location,
i.e. a folder where you want your project created, you should see the
following...
·
Now, specify the name of your
project. Visual Studio will create a
folder underneath the one you specified for Location by that name. This will also become the name of your
executable... Finally, click on the OK
button to record your selection.
·
It is very important that you do
each of these initial steps in the exact order that I have described
above. That is, first specify that you
want a Console Application, then specify the location, then specify the project
name. Do not forget any of these steps.
In other words, pay attention!!!
·
As a result, you will see the
following in Visual Studio
Notice that the Workspace window now has three
tabs. It has a ClassView tab, a FileView
tab, and an InfoView tab. Select (point the mouse to) the FileView tab.
After
selecting the FileView tab, you should see the following. Notice what appears in the Workspace
window...
·
Now we need to create a source
file and include it in the project. If
we do the correct sequence of steps, Visual Studio will automatically include
the new file into the project.
·
Do a File|New and select Text
File and then specify a File name.
Do not accept the default choice, Active
Server Page (or whatever happens to be the default). Do not select C/C++ Header File. Do not select C++ Source File. Select Text File. Do not forget to type in the file name, i.e.
hello.c (or whatever you wish to call it), but
it must end with .c !!! The file
suffix tells Visual Studio what type of
file this is so it knows which
compiler to invoke on it. Finally, click OK to select your
choice.
As
a result of your efforts, you should see the following.
·
I usually move the windows around
at this point, adjusting the sizes.
·
Type in the program.
·
If you click on the + by Hello
files under Workspace, you will see a list of all of the files in your project
· Build your program by selecting the Build|Build menu option
·
Run it (Build | Execute)
Now
let's look at the files:
Hello.dsw: this
is your WORKSPACE
Hello.dsp: this
is the project build file
These
two files, along with hello.c
are worth saving.
·
The program we just built is
HELLO.EXE under the Debug directory
·
Because it is a CONSOLE
application, we can run it directly from our Windows NT command (or Windows 95
Dos) prompt.
·
When I typed hello below at the Dos prompt, Windows ran the program hello.exe. Below you can see the results of this run.
Compiling
Under UNIX
To
compile a C program under UNIX, one may do it in the following ways:
% cc hello.c
This
compiles the file and creates an executable named a.out
% cc hello.c -o hello
This
compiles the file but renames the executable hello
% cc -c hello.c
This
compiles the file but does not link
it, thus producing an object module
which may be linked later on. That file,
by default, is hello.o
% cc hello.o -o hello
This
links the object file hello.o to
create the executable hello
Under
UNIX, suffixes are important (i.e., .c versus .o). They tell the compiler what type of file they
are, i.e. a C program file versus an object module.
% cc foo.o bar.o -o fubar
This
links two separately compiled modules into an executable named fubar.
The
compiling process -- overview
Compiling
process -- translation phases (ANSI)
1. Physical source file characters are mapped to
the source character set (including new-line characters and end-of-file
indicators) if necessary. Trigraph sequences are replaced by
corresponding single-character internal representations.
2. Each instance of a new-line character and an
immediately preceding backslash character (\)
is deleted, splicing physical source
lines to form logical source lines.
3. The source file is decomposed into
preprocessing tokens and sequences of white-space characters (including
comments). A source file shall not end in a partial preprocessing token or comment. Each comment is replaced by one space
character. New-line characters are retained.
4. Preprocessing directives are executed and
macro invocations are expanded. A #include
preprocessing directive causes the named header or source file to be processed
from phase 1 through phase 4 recursively.
5. Each source character set member and escape
sequence in character constants and string literals is converted to a member of
the execution character set.
6. Adjacent character string literal tokens are
concatenated and adjacent wide string literal tokens are concatenated.
7. White-space characters separating tokens are
no longer significant. Each
preprocessing token is converted into a token.
The resulting tokens are syntactically
and semantically analyzed and
translated.
8. All external object and function references
are resolved. Library components are
linked to satisfy external references to functions and objects not defined in the current
translation. All such translation output
is collected into a program image which contains information needed for
execution in its execution environment.