Docsity

Using gcc and make for Compiling C Code: A Short Tutorial, Assignments of Data Structures and Algorithms

Toggle sidebar
bg1
Short Tutorial on gcc and make
Ben Hermens - [email protected]
Introduction
This tutorial intends to help you use gcc to compile your C code. I will also introduce makefiles to
save you lots of typing. I will keep things as basic and brief as possible. If you need more help than is
provided here, email or come to office hours.
I am writing this tutorial for the RedHat Linux machines in the ENGR labs. I will grade your programs
using those machines. You can develop your code wherever you want but before you submit it, make
sure it will compile and run without errors or warnings using gcc as I describe below.
The Terminal
Right click on the desktop and select “Open Terminal”. You will be in your home directory.
Here are a few basic commands for working in a terminal:
Command and Arguments Description
pwd Print the current directory path
ls List files and directories in the current directory
cd <directory name> Change working directory
cd .. Change to parent directory
mkdir Make a new directory
cp <src file name> <dst file name> Create a copy of <src file> and name it <dst file>
rm <file name> Remove the file
nedit A nice text editor with syntax highlighting
nedit <file name> Open a particular file
nedit <file name> & Open nedit without locking the terminal. Now you
can use the terminal to compile.
Write Some Code
With a new terminal open, follow these commands:
1. mkdir cs261
2. cd cs261
3. mkdir assign1
4. cd assign1
5. Use a web browser to save circuit.h into the cs261/assign1/ directory
1. OR - Use the following command (it should be one line)
cp /nfs/stak/a2/classes/eecs/spring2006/cs261/public_html/Assignments/circuit.h
circuit.h
6. nedit circuit.c &
7. Write your code and save the file. Start with just a main method that prints “Hello World”.
8. gcc -Wall -ansi -o circuit circuit.c // See the next section for more
9. circuit // This will run your program!
pf3
pf4

Partial preview of the text

Download Using gcc and make for Compiling C Code: A Short Tutorial and more Assignments Data Structures and Algorithms in PDF only on Docsity!

Short Tutorial on gcc and make

Ben Hermens - [email protected]

Introduction

This tutorial intends to help you use gcc to compile your C code. I will also introduce makefiles to save you lots of typing. I will keep things as basic and brief as possible. If you need more help than is provided here, email or come to office hours. I am writing this tutorial for the RedHat Linux machines in the ENGR labs. I will grade your programs using those machines. You can develop your code wherever you want but before you submit it, make sure it will compile and run without errors or warnings using gcc as I describe below.

The Terminal

Right click on the desktop and select “Open Terminal”. You will be in your home directory. Here are a few basic commands for working in a terminal: Command and Arguments Description pwd Print the current directory path ls List files and directories in the current directory cd Change working directory cd .. Change to parent directory mkdir Make a new directory cp Create a copy of and name it rm Remove the file nedit A nice text editor with syntax highlighting nedit Open a particular file nedit & Open nedit without locking the terminal. Now you can use the terminal to compile.

Write Some Code

With a new terminal open, follow these commands:

  1. mkdir cs
  2. cd cs
  3. mkdir assign
  4. cd assign
  5. Use a web browser to save circuit.h into the cs261/assign1/ directory
    1. OR - Use the following command (it should be one line) cp /nfs/stak/a2/classes/eecs/spring2006/cs261/public_html/Assignments/circuit.h circuit.h
  6. nedit circuit.c &
  7. Write your code and save the file. Start with just a main method that prints “Hello World”.
  8. gcc -Wall -ansi -o circuit circuit.c // See the next section for more
  9. circuit // This will run your program!

Compiling with gcc

gcc is a command line compiler on Unix/Linux operating systems. The most basic use is as follows:

  1. Write a program in a file such as helloWorld.c
  2. In the terminal, in the directory with the code file, type “gcc helloWorld.c”
    1. Errors and warnings will be printed to the terminal
      1. Errors are caused by syntax problems and the program cannot be compiled.
      2. Warnings mean the program can compile but the compiler thinks you might have done something wrong. Fix warnings too!
    2. If there are no errors, the files helloWorld.o and a.out are created.
      1. helloWorld.o is an intermediate file that is compiled but not linked.
      2. a.out is the program that you can run by typing “a.out”. Options for unix programs are given on the command line and have a minus sign ('-') in front of them. A few options for gcc are shown here: Option Description -Wall Show all warnings -ansi Use the ANSI standardized version of C -o File name for the output program file -c Compile but don't link. Creates *.o files only. -lm Link the math library. Use this if you #include <math.h> I will use -Wall and -ansi when I grade so you should use them too! In this example we only have 1 code file. What if the project has 3 code files: code1.c code2.c and code3.c? To compile you'd type: gcc -Wall -ansi -o prog code1.c code2.c code3.c What if the files are big and you don't want to wait for them all to recompile because you made a change to one of them? You can do the following: gcc -Wall -ansi -c code1.c gcc -Wall -ansi -c code2.c gcc -Wall -ansi -c code3.c gcc -Wall -ansi -o prog code1.o code2.o code3.o The first 3 commands compile each of the files and create code#.o files. If you change code1.c later, you only need to recompile that one file. The 4th command links the code#.o files to create the executable program “prog”. This can be a lot of typing but there's a nice solution. See the next section.

Working at Home

As I mentioned before, you can write your programs anywhere you want. You don't have to write them on the lab machines. Before you submit a program, you should compile and run it on the lab (linux) machines to make sure there aren't any surprises when you get your grade. You can access the lab machines from home using ssh. dear115-##.engr.oregonstate.edu or kec1130-##.engr.oregonstate.edu (where ## = 01, 02, ...) You can only get to these machines from within the campus network though so you'll probably have to ssh to flop.engr.oregonstate.edu and connect to the lab machine from there. If you're looking for a free C compiler to run at home, this website has a long list: http://www.thefreecountry.com/compilers/cpp.shtml My preference would be for Microsoft Visual C++ 2005 Express. You'll want to make sure the compiler is treating your files as C code and not C++ code. Many C/C++ compilers are capable of either and will likely default to C++.