[ For more information about Unix commands, see http://www.cs.kzoo.edu/CSShared/IntroUnix/basicUnixCmds.html. For links to information about vi, see http://www.cs.kzoo.edu/CSShared/IntroUnix/BashVi.html. ]
First, you can double-check that you have the GNU C Compiler and Make on your machine (which you should):
whereis gcc whereis make
Each of these will either respond with one or more full file pathnames (e.g.,
/usr/bin/gcc
) or with an error that it cannot be found.
You should have both.
At least until you get comfortable with make, you will probably want to put each project in its own directory. If you have started to create C source files all in one directory (or if you haven't started yet), change to the directory under which you want to create a new directory and then make the directory:
cd courseFiles/cs230 mkdir pp1a mv helloworld.c pp1a (if you already created helloworld.c in cs230)
So, let's say you have a directory called pp1a
and you have
a simple program
in that directory that "fits" in a single C source file, e.g.,
helloworld.c
.
Go into that directory, and run the gcc
command.
cd pp1a vi helloworld.c if it needs any editing gcc helloworld.c compiles file, creates a.out a.out runs a.out
The gcc
command will compile helloworld.c
and
create an executable file. By default, gcc
will create a
file called a.out
(the name might be something
different on a windows machine, like a.exe
). You can run
it just by typing that name, as in the example above. (If this doesn't
work, try typing ./a.out
, which means "run
a.out
in the current directory. (Unix uses .
to refer to the current directory and ..
to refer to the
directory above.))
If you want to create a more meaningful executable name (definitely a good
idea!), use the -o
option on the gcc
command:
gcc -o hello helloworld.c compiles file, creates hello executable hello runs hello executable OR ./hello
If you have a program that is spread across several files, e.g.,
file1.c
, file2.c
, and sharedHeader.h
,
then gcc
will compile them to individual machine code
files and then link them together to a single executable:
gcc -o myProg file1.c file2.c NOTE: don't list the .h file myProg runs myProg OR ./myProg
You don't list the header file(s) in the command line because the content of a header file is already read into the C source files that include it.
If you have a different directory for each project, so all the files in the
current directory are part of the current project, then you can use
*.c
to
refer to all the .c
files in the current directory:
gcc -o myProg *.c myProg