In this class, we will be using the GNU compiler gcc(1) to compile programs. This handout tells you how to do this on the CSIF systems, and then how to execute the program.
In this handout, what you type on the computer is in bold Courier typeface and what the computer outputs is in Roman Courier typeface. The “%” is the CSIF’s shell prompt.
Our 0-th homework assignment is to write a program that will print “hello, world!”, in C. We begin by creating a file — let’s call it hello.c – that contains the program. We need this to be a text file, because the input to the compiler is text; so, we can’t produce a Microsoft Word file and use it. It also has to end in “.c”. Otherwise, the compiler will not recognize it as a C program file, and it will complain. This type of file is called a “source code file” or “source file”.
You can create this file in any text editor you like. On the CSIF, the two text editors used most widely are vim(1) (or vi(1), a less advanced version of the same editor) and emacs(1), which is far more powerful and complex. If you haven’t used a text editor on Linux, I recommend you start with vim. Jonathan Vronsky has written a very brief guide to vim; look in the Handouts area to find it. To see a very good, but much more detailed, tutorial on this editor, use the command vimtutor:
% vimtutorType either “:q” or “ZZ” to exit this tutorial.
Go ahead and create your program file hello.c. Make sure it is on the CSIF system before you go to the next section.
Now it’s time to compile your program. We will be using the gcc(1) compiler. Type the following to the shell prompt:
% gcc -ansi -pedantic -Wall hello.cThe options (the words in the command that begin with “-”) mean:
If your program is in more than one file, for example the main routine is in main.c and functions are in files func1.c, func2.c, and func3.c, simply list all the source files in the compile command:
% gcc -ansi -pedantic -Wall main.c func1.c func2.c func3.cThe order in which you list the source code files does not matter.
At this point, your directory should contain a file named a.out. Look for it by running the command ls and checking that the file is one of those listed.
If you didn’t see any error messages in the previous step, the file a.out is the compiled program. Type
% a.outCongratulations! You have just compiled and run your first program on the CSIF systems.
By default, the compiler puts compiled programs in the file a.out. If you want your program to be called something else, you can do this in two ways. First, you can tell the compiler to call the output something other than a.out by using the “-o” option:
% gcc -ansi -pedantic -Wall hello.c -o helloThis causes the compiled file to be named hello rather than a.out. You can put the “-o hello” anywhere after “gcc”, but the “hello” must immediately follow the “-o”.
The second way is to rename the file a.out. Once it is created, you can do this using the mv(1) command:
% mv a.out helloThe difference between these two methods is that the first one leaves any existing a.out file alone and unchanged. The second overwrites any existing a.out file.
Typing the full compile command each time can be annoying. More importantly, when you begin writing more complex programs that occupy more than one file, it can be difficult to remember how to create a compiled program from them. To solve this problem, we use a program called make.
make executes a series of commands in a file called a Makefile (or makefile). These commands are selected based on which source files have been modified. For now, as we are only dealing with one source file, our Makefile will be very simple.
Store the following in a file named Makefile:
hello:The second line must begin with a horizontal tab (control-I) — do not put blanks before the gcc!
gcc -ansi -pedantic -Wall hello.c -o hello
Now delete the file hello if it exists:
rm hello(if you get an error message saying that file does not exist, ignore it). Then type:
% makeAs before, if your program has no errors, you should get a shell prompt right after the line with the gcc command.
gcc -ansi -pedantic -Wall hello.c -o hello
Now let’s look at a couple of other results. Without deleting the file hello, execute the make command again:
% makeThat means you already created hello, so it won’t be recreated. If you change hello.c, you will need to delete hello to get make to remake it. Later on, we’ll show you how to build a Makefile that will automatically check whether hello.c has been modified since hello was made, and if so recompile hello.c.
make: ’hello’ is up to date.
Let’s say you forget to put a horizontal tab at the beginning of the second line, and instead put blanks there. Here’s what happens:
% makeIf the first character of the second line is anything other than a horizontal tab, you will get a message similar to this. Just go into your Makefile and change the first character to a tab.
Makefile:2: *** missing separator (did you mean TAB instead of 8 spaces?).
|You can also obtain a PDF version of this.||Version of September 29, 2015 at 10:02PM|