|
Operating Systems [22c:112] |
| Tutorial 1: where to start with the C language |
|
When you start a new project, even if very simple, is always a good idea to create a new
directory where to keep all the sources, header, and versions developed. In Linux, to
create a new directory you need to launch the command
mkdir project1
where project1 is the name of the directory you want to create. Then, you should
move inside that directory and start to write your code. To move between directory you
will use the command cd followed by the name of the directory in which you want to
enter, or followed by .. if you want to go back in the previous directory.
cd project1
cd .. Editors
Hard core programmers would suggest you to use Emacs, old school want you to use
vim, maybe someone still use pico. You can write code using the text editor
you prefer, don't bother yourself forcing to use something you don't know. For example,
even if I program from 16 years, now I use kwrite, the default text editor of KDE,
to write my code. If you want to use it, just type
kwrite
in a shell, and if installed, it will come out. There are also so many IDE (Integrated
Development Enviroment) out there, that are suppose to help you during the development of
your code. They offer quick access to compilation and debugging, and often help you with
the syntax of commands and functions. The best IDE I have seen in Linux is Anjuta.
You can find it following this link
Soon I will post a tutorial about the use of this IDE, but you can find a lot of useful
guide on internet about that, so you're strongly encouraged to look for them and read
them!When I say "text editor" I mean TEXT EDITOR! Even if it's technically possible, don't ever use a word processor like Word or OpenOffice to write your code. Compiler
The most popular C compiler for Linux is GCC, installed by default on all the systems. If
your one doesn't have it, you can find the last version following this link
To compile your code, you can simply type
gcc project.c
in a shell. Clearly you project.c is the name of the source file that contain the
C code. You can give the name you want to your sources, but keep the .c extension.
If there are no errors in the code (did you leave an empty line at the end of the file?)
the gcc will produce no output, and will create an executable file called
a.out. Launching that file on the prompt, you will see your software working!!!
If launching
a.out
the prompt return saying
bash: a.out: command not found
then try with
./a.out
and then visit the FAQ of this course to learn how to solve the problem. A lot of option
can be passed to GCC. Refer to the GCC manual (man gcc) to a complete list of them. Two of
them are important: -o <filename> that write the executable file inside
<filename> instead than in a.out, and -Wall that enable all the
warnings during the compilation. The warnings are not errors, but potential errors.
A very well written code should have no warnings, except in very special cases. In
addition, even if seem annoying and unuseful, warnings can make you able to identify
problems before they occurs. For example, you can compile your main.c file typing
gcc -Wall -o foo main.c
Pay attention using the name test because often the operating system decide to
execute its internal test program (situated in /usr/bin/test) instead than your one.
Makefile
A common and useful way to automatically execute some commands (as for example compiling
your software) is to use a Makefile file. This is a simple text file divided in
section, that contain the commands to executed when someone type in the console something
like
make all
For example, a very simple Makefile could contain the lines
all:
gcc -Wall -o foo main.c
in that case, to compile your software you can just type
make
to the prompt and it will do everything for you. If it may seem not so useful for small
software, it is necessary for software made by hundreds of files divided in hundreds of
directories. It may also useful to clean the directory from the old and the temporary
files created during the development, or to compile your programs with different options
(one with the debug active, one with all the warning active, one with all the
optimizations active...). A more complex Makefile could look like
all: clean
gcc *.c -o foo
clean:
rm -f *~
rm -f foo debug opt
debug:
gcc -Wall -o debug main.c
opt:
gcc -O3 -o opt main.c
With a Makefile like this during the development you can type
make debug
to produce a debug version of your code and test it. Launching
make opt
you will obtain a optimized version of your code, to gain all the speed you can
using platform specific features. Typing
make clean
you clean the working directory from all the temporary files created during the
development and the old built. In addition, typing either
make all
you will first clean the directory, and then compile all the .c files in the
working directory, producing a foo output file.
make |