Run the following
$ sudo apt install gcc gdb build-essential codeblocks
Go to https://cygwin.com/install.html
Download setup-x86_64.exe
Install it and from that install bash shown in video below
later install GCC as shown in the previous video
============================================================
Download and unzip the contents of the following drive : https://drive.google.com/drive/folders/1yn_PyngJO7mmahWq5mnQp2yMaJjR1rCs?usp=sharing
============================================================
cd
into GCC directory from the above downloaded and extracted files.- run
gcc circle.c
- run
./a.out
The above will display the radius of the circle
Next , uncomment line 26 in circle.c
and compile with the command gcc circle.c -Wall
Now you should be able to see the warnings as well.
Initially clean up the executables from previous runs by running
rm *.o *.s
rm a.out
If we want to avoid overiting to the default file i.e a.out
we can use output redirection. We do this by specific filename to which the contents can be redirected to. For redirecting we do the following :
- using the switch
-o
as follows
$ gcc circle.c -Wall -o circle.x
Now we can see that there is no a.out
but we have circle.x
and we can run it using $./circle.x
- Preprocess
- Assemble
- Compile
- Link
To see how the output of preprocessing steps output looks like :
gcc -E circle.c -o circle.i
you can now open circle.i
in the text edtior to see the post-preprocessing steps output.
If you would like to preserve the comment through the preprocessing step run the following :
gcc -E -C circle.c -o circle.i
To generate assembly code form C program:
gcc -S circle.c
A new file called circle.s
will be created in the pwd
.
We will now generate the object code for the C Program.
- To just complie and not link
gcc -c circle.c
will generate a new file called circle.o
, C
has now be converted to coresponding machine code.This is in binary form but it is not the final executable.It is only instruction equivalent of the code in C
it doesnt include the code for other inbuilt useages such as printf
etc. The reason why this is not included is because stdio.h
will include the object/binary code of printf and not the code of it.If its in code form it needs to be re-complied everytime , hence linking the binary of header/library files speeds up the process. circle.o
hence contains only the executable code of the written program only.
Compare circle.o
and circle.x
ls -l circle.o circle.x
circle.x
is final executable after linking hence has a bigger file size and circle.o
is just the object code.
If we want to link , we use the object code circle.o
as input to gcc
and redirect the output to a file ,say circle.x
gcc circle.o -o circle.x
Try running the following :
gcc A01Quadratic.c -o quad.x
It will throw an error as follows :
/tmp/ccpMSKSa.o: In function `main':
A01Quadratic.c:(.text+0x16f): undefined reference to `sqrt'
A01Quadratic.c:(.text+0x1bd): undefined reference to `sqrt'
A01Quadratic.c:(.text+0x252): undefined reference to `sqrt'
collect2: error: ld returned 1 exit status
This is because math lib is not linked , hence we need to run with linking as follows :
gcc A01Quadratic.c -o quad.x -lm