C, ELF and Zig

There are something we should always remember. They are basics and foundations of computing system. You have to master them to write and understand C program very well. What is the difference between deleting a segment of code and killing a person…? Cola, My FirstCat C Programming Language C is more than 50 years old, which is still very popular and good. C or Non-C. ? C is high-level and portable assembly language. Assembly languange is not portalbe. It has to be associated with ISA (Instruction Set Architecture). C is just a thin portable layer above assembly. Modern C: ANSI-C/C89/C90 –> C99 –> C11 –> C17 –> C23 C is a strictly typing and compiling language. You mindset is about CPU, memory, bits and bytes while programming in C. Compilation Each .c file is compiled separately. We can get .o files from .c files. Then all .o files are linked together and produce executable binary file. Therefore we have make -j<N>. Before compilation, there is a pre-processing step in which all pre-processing macros are executed or replaced. The pre-processing results are normally stored in .i files, which are normally invisible temperory files. gcc -E command gives us .i files. We can also ask for generate assembly files .s by gcc -S command from .c files. Compile .s file can also give us .o file. .o files are in ELF format. We can get .o files from .c, .s or objcopy. .c –> [.i –> .s – >] .o –> ELF ...

June 3, 2025

How to Write Makefile?

Make is like a chef, which use recipes to produce software. And the recipe is called Makefile. Inside makefile, there are lots of targets. # Makefile basic syntax, this line is comment. target: prerequisites # space-separated list <tab> shell-commands # command run in shell Basics Make uses file modification timestamps (mtime) to determine if a target needs rebuilding: If the target does not exist, make runs the following commands. If any prerequisite is newer than the target, make runs the command to rebuild it. If the target is newer than all prerequisites, make does nothing. If any prerequisite is missing, make tries to make missing prerequisite, and this new prerequisite must be newer than target, go to 2. Prerequisite list could be empty. E.g. phony targets. Phony targets could also have prerequisites. Make makes the target always the newest. $ make <target> # default Makefile, capital M $ make -f <makefile> # specify a makefile If target is missing in make command line, the first target in Makefile is the default target. ...

June 2, 2025

Shell Programming

Shell is a powerful Command Line Interface (CLI) in Linux. We type commands, and shell executes them in a beautiful and sophisticated way. Shell is also a super flexible programing language for Linux. It could bind commands together with conditions, loops and various expansions ($,~). Shell is actually a raw interpreting programming language in which there is no compiled middle bytecode. In this post, I use bash, which is the most popular and widely deployed shell implementation. ...

June 1, 2025