forked from joelfenwick/ctute
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinuxenv.tex
106 lines (85 loc) · 3.97 KB
/
linuxenv.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
\chapter{Commandline Linux environment --- gcc}
The following assumes that you have access to a Linux/Posix/BSD commandline.
This could either be a local terminal window on operating systems which support it or via an SSH program such as Putty.
We will assume you are using the \texttt{bash} shell and the \texttt{gcc} C compiler.
\section{Creating source files}
C source files can be created and edited using any plain text editor\footnote{Some tools (such as MacOS's TextEdit) can save formatting or not. Make sure you are using text only.} (eg: \texttt{nano} or \texttt{vim}).
Be aware that different operating systems have different ideas about what the end of a line looks like.
For example, compiling a file on Linux which has Windows line endings in it will lead to compile errors.
With this in mind, try to edit on the same platform you will be compiling on (or failing that, use a tool which respects the target
platform's line endings\footnote{For example, Notepad++ can be set to act in this way.}.
Some systems have case-sensitive filenames and some are case-insensitive (eg: on Windows, \texttt{bob.c} is considered to be the same file as \texttt{Bob.C}).
Regardless of your home system, you should try to use consistent cases.
For \texttt{gcc}, C source files must end in \texttt{.c} (lower case) or later on \texttt{.h}.
Put the following in a file called \texttt{test.c}:
\begin{codeblock}
int main()
{
return 5
}
\end{codeblock}
Then compile it with the command:
\begin{verbatim}
gcc test.c
\end{verbatim}
which will produce a message like:
\begin{verbatim}
test.c: In function ‘main’:
test.c:4:1: error: expected ‘;’ before ‘}’ token
}
^
\end{verbatim}
Now type:
\begin{verbatim}
echo $?
\end{verbatim}
This asks the shell to tell you what happened with the previous command (its ``exit status'').
By tradition, anything other than \texttt{0} indicates some form of error.
In this case, the compiler failed and \texttt{\$?} reflects that.
Now edit \texttt{test.c} to add a semicolon after the \texttt{5}, then recompile.
You should not see any messages.
Look in the current directory, and you should see a new file called \texttt{a.out}.
This is the default name for programs made with \texttt{gcc}. The name can be changed
by using the ``-o'' option:
\begin{verbatim}
gcc test.c -o test
\end{verbatim}
This would produce an output file named \texttt{test}.
\\
Execute your program with:
\begin{verbatim}
./a.out
\end{verbatim}
and check that the exit status was \texttt{5} as expected. When a program returns from main its ``exit status'' is the value returned.
Depending on your requirements, you may need to comply with some particular version of the C language standard.
Adding one of the following to your compile line will do this.\\
\begin{tabular}{l|l}
-ansi & C89/C90 \\
-std=c99 & C99 \\
-std=gnu99 & C99 with some gnu extras\\
\end{tabular}
\section{Linking and other compile options}
\texttt{gcc test.c} actually performs a number of phases including compiling and linking.
The distinction between these is more important when a program's source consists of multiple
files.
\begin{verbatim}
gcc -c file1.c
gcc -c file2.c
gcc -c file3.c
\end{verbatim}
Would compile each of the files (and check them for correctness) to a corresponding \texttt{.o} file.
Following that:
\begin{verbatim}
gcc file1.o file2.o file3.o
\end{verbatim}
would ``link'' i.e. combine the pieces together and check for consistency and completeness.
Adding \texttt{-Wall} and/or \texttt{-pedantic} will cause \texttt{gcc} to report more potential problems.
\emph{This is a good thing.}
\section{Other notes}
It may seem obvious, but do not ignore error or warning messages.
While warnings for other languages often mean ``your code could be written more nicely'', warnings in C often mean ``your code will not do what you think it will''.
You can save error messages by adding \verb|2>errfilename| to the end of a command.
For example:
\begin{verbatim}
gcc myfile.c 2>errors.log
\end{verbatim}