Programming exercises for KN King's book C programming - a modern approach.
Compile *.c
files with:
export BASENAME=<filename without '.c' extension>
gcc -Wall -Wextra -std=c99 ${BASENAME}.c -o ${BASENAME}.bin
Then run the binary with:
./${BASENAME}.bin
Makefiles are introduced at 359. Compile subsequent projects with:
make
The above command should generate an executable file, e.g. program
, which can then be run as follows:
./program
Clean up the tree with:
make clean
CMakeLists.txt
files are introduced at 45607. Compile subsequent projects with:
mkdir build
cd build
cmake --build .
cmake --install .
# run the resulting program with:
./dist/bin/program
clang-format
formats *.c files (as well as other formats), is customizable, and can inherit
from existing published style guide, e.g. Google, LLVM, etc. See
https://clang.llvm.org/docs/ClangFormatStyleOptions.html.
# print warnings, don't change files
clang-format -Werror --dry-run main.c
# print main.c to stdout including changes,
# but don't change main.c itself
clang-format main.c
# change file in-place
clang-format -i main.c
valgrind
can help detect various kinds of errors. By default it runs memcheck
, which detects memory errors:
$ valgrind ./program
$ valgrind tool=memcheck ./program
$ valgrind --tool=memcheck --leak-check=full --show-leak-kinds=all ./program
To have Valgrind output the relevant line numbers, recompile the binary with -g
flag ("include debugging information")
and -O0
("disable optimization"). Note that this requires recompiling the objects as well.
See https://valgrind.org/ for more information.
# TODO
# TODO
- https://cdecl.org/ for decoding declarations
Have gcc
generate Assembler code
gcc -c -S main.c # generates main.s
Printing the shared objects (shared libraries) required by each program or shared object specified on the command line:
ldd program
List the symbols from an object file:
nm main.o