Notes and Talks/UJEP/UPC/01/hello

Hello World in C

Compiler

bashwhich cc
ll `which cc` # or ll $(which cc), chain ... --> gcc

Code

main.c#include <stdio.h>

int main() {
    printf("Hello, World!\n");
    return 0;
}

// int main() {
//     printf("Hello, World!\n");
//     return 1;
// }

// void main() {
//     printf("Hello, World!\n");
// }

// void main() {
//     printf("Hello, World!\n");
//     return 0;
// }

compile, run, inspect object files

bashgcc -c main.c -o main.o # compile to object file
gcc main.o -o main      # link to executable
./main                  # run the program
bashobjdump -h main.o       # show section headers
objdump -x main.o       # show all headers (including symbol table)

objdump -d main.o           # disassemble code section
objdump -d -M intel main.o  # disassemble code section in intel syntax

ldd main    # show shared library dependencies
nm main.o   # show symbol table

strings main.o  # show printable strings
mcview main.o   # show machine code in a more readable format (requires mcview tool)
xxd    main.o   # hex dump

make🌐

MakefileCC = cc # default C compiler, links to gcc
CFLAGS = -std=c23 -Wall -Wextra # c11, c17, ...

.PHONY: all clean

# default (first) target to build the main executable
all: main

# compile main.c into an executable named main
main: main.c
	$(CC) $(CFLAGS) -o main $^

# clean up build artifacts
clean:
	rm -f main *.o a.out
bashmake
./main
# make && ./main
# make clean
# make all
# make clean all
# ./main && echo "*OK*" || echo "*FAIL*" # left to right, same priority
# make && (./main && echo "*OK*" || echo "*FAIL*")

Docs