Skip to content

Instantly share code, notes, and snippets.

@Pythonista7
Last active June 30, 2021 10:49
Show Gist options
  • Save Pythonista7/70251e6dd87616cd4b87b6c134899fe2 to your computer and use it in GitHub Desktop.
Save Pythonista7/70251e6dd87616cd4b87b6c134899fe2 to your computer and use it in GitHub Desktop.
foss-event-gcc/gdb Instructions

Install info

Installation of BASH, GCC, GDB on Debian/Ubuntu

Run the following

$ sudo apt install gcc gdb build-essential codeblocks

Installation of BASH, GCC, GDB on Windows

Go to https://cygwin.com/install.html

Download setup-x86_64.exe

Install it and from that install bash shown in video below

https://youtu.be/EK5rt8LBbmo

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

============================================================

GCC

  1. cd into GCC directory from the above downloaded and extracted files.
  2. run gcc circle.c
  3. 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 :

  1. 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

Lifecycle of C program

  1. Preprocess
  2. Assemble
  3. Compile
  4. 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

Assembling

To generate assembly code form C program:

gcc -S circle.c

A new file called circle.s will be created in the pwd.

Compling

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.

Linking

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment