C
Linking
Linking a library (.a or .so file)
To link a lib file of name libexample.a you include the flag -lexample After the name of the output file is specified.
The -L. flag specifies to search for libraries in the current directory.
You also must include the header file in your c file which contains the function prototypes e.g.
#include "example.h"Makefile
Compile commands in makefile are usually structured as following
${CC} ${CFLAGS} inputfile1.c inputfile2.c -o outputfile.o -L. -lmylibSetting PHONY targets
Phony targets unlike normal targets aren’t named after a output file and are just utility commands.
.PHONY: clean
clean:
#clean command hereAutomatic Variables
$@Gets name of rule target$<Gets first name of first prerequisit$^Gets all prerequesits$?Gets all prerequesits newer than the target
Wildcards
% is similar to * in bash. For example %.c means any file ending with .c and f% means any file starting with f
Example Makefile
# Configuration
CC=clang
CFLAGS=-Wall
LDFLAGS=
EXE=example
OBJ=example.o circles.o output.o
# Default target: link the object code into the executable
${EXE}: ${OBJ}
${CC} ${LDFLAGS} -o $@ $^
# Compile object code from source code
%.o: %.c
${CC} ${CFLAGS} -c $<
# Remove build products
.PHONY: clean
clean:
rm *.o ${EXE}
GDB
Adding debug to a object file during compilation
Add the -g flag to your clang command e.g.
clang -g program.c -o GDB Commands
Invoking GDB
gdb --tui executableRunning the program
- Use run use the
runorrcommand
Progressing the Program
- To run until breakpoint/ watch point use
continueorc - To run one line use
stepors(note:stepallows for entering into functions) - To run one line without entering functions use
nextorn
Stopping a Program
- To end a program’s execution use the
killorkcommand
Breakpoints
There are different contexts to use the break command:
break line_numberbreaks at specified line numberbreak function_namebreaks when specified function is calledbreak filename:function_namebreaks when function from a specific file is calledinfo breakpointsgets info on all breakpoints (including IDs)deletedeletes all breakpointsdelete IDdeletes breakpoint with that ID
Watchpoints
watch VARIABLEmake program pause every timeVARIABLEis updatedwatch CONDITIONpauses the program when a condition is true e.g.watch i>12
Examining Functions
print VARIABLEshows the value of the variableprint &VARIABLEprints the address of a variableprint *VARIABLEprints the value at the address of a variableprint VARIABLE[n]prints the nth element of an arrayptype VARIABLEprints the datatype of a variable
Strings
string.h
Useful String Functions
strcmp(string1, string2)- 0 if identicalstrlen(string1)strcpy(destination, source)strcat(string1, string2)
Format String Specifiers
%dor%isigned integer%uunsigned integer%ffloat%lfdouble%ccharacter%sstring%hdsigned short int%huunsigned short int%hhushort short unsigned int
Commmand Line args in C
int main(int argc, char* argv) {
printf("Hello %s", argv[1]);
}File operations C
stdio.h
Initalising the file pointer
FILE *fp = fopen("filename.txt", "w")File Open Modes
rfor read modewfor write modeafor append modebafter any of the above to do this in binary moder+,w+for read and write modea+for both reading and appending
Reading from a file
fscanf(fp, "formatted string %d %d", &var1, &var2);Writing to a file
fprintf
fprintf(fp, "formatted string %d %d", &var1, &var2);fprintf prints a formated string to a file
fwrite
fwrite(&var, sizeof(var), 1, fp);Writes only a variable to a file.
An array can be written to a file using
int array[100];
int n = sizeof(array)/sizeof(int); //calculates the length of arr
fwrite(&arr, sizeof(int), n, fp);Reading user input
scanf("%d", &var1);