Linux Programming
System Programming
" One vision, one purpose. "
Copyright © Tony's Studio 2020 - 2022
Chapter Three - Linux Programming
3.1 GCC
3.1.1 What is GCC
GCC is short for
GNU Compiler Collection
.
3.1.2 Compile Procedure
- Write source code, e.g.
main.c
.gcc -E main.c -o main.i
: Preprocess, insert header files, macro replacement and remove comments.gcc -S main.i -o main.s
: C compiler complies C to assembly.gcc -c main.s -o main.o
: Assembler will compile assembly to binary file.gcc main.o [trivia.o ...] -o main
: Perform linkage to generate the final executable.PS: It seems that GCC need file extension to identify file type. :P
3.1.3 Extra Compile Options
parameter meaning -g
add debug info, gdb
needs this-D
add extra macro, e.g. -DDEBUG
or-DDEBUG=0
-O
-O1
-O2
-O3
optimization -W1
-W2
-W3
-Wall
warning level -I
extra include directory, e.g. -I inc
or-Iinc
to add./inc
-L
extra library directory, e.g. -L.
or-L .
to add./
3.1.4 Library
3.1.4.1 Static Library
Create static library.
ar
is for archive, huh?
1
2 gcc -c tinyxml.c -o tinyxml.o
ar cr libtinyxml.a tinyxml.oUse static library.
1 gcc -o main main.o -L. -ltinyxml
3.1.4.1 Dynamic Library
Create dynamic library.
1
2 gcc -shared -fPIC -c fmod.c -o fmod.o
gcc -shared -fPIC -o libfmod.so fmod.oUse dynamic library. We have to set library path before we run the program. Or it won’t find it. Linux doesn’t support, you know,
#pragma
things. :(
1
2
3
4
5
6
7
8
9 # Do the linkage.
gcc main.c libfmod.so -o main
# Set library path.
LD_LIBRARY_PATH=.
export LD_LIBRARY_PATH
# Run the executable.
./mainWith dynamic library, if the dynamic library changes, it doesn’t need another compile, and the output will change accordingly.
3.2 GDB
“I don’t like nude GDB at all!”
3.2.1 Launch GDB
GDB can be launched with executable name.
1
2
3
4
5 # Method 1
gdb main
# Method 2
gdb
file main
3.2.2 Run Program
Err… after you successfully launched GDB.
1
2
3 run [...args]
start [..] # will stops at main
continue # continue runningWhy bother doing this like a primitive and not using VS with WSL?
3.2.3 Break Point
Many ways to add break point using
break
, a.k.a.b
.
1
2
3
4 break [function name]
break [line number]
break [filename] : [line number]
break [*adress]
3.2.4 Watch
Ahh!!!
1 print variable
3.3 Make
3.3.1 Meet Make
make
是一个自动化的程序自动维护工具,它根据Makefile
所描述的“依赖关系”自动决定项目的那些部分需要重新编译。
make
根据目录下的Makefile
文件确定文件的依赖关系,并确定哪些需要重新生成或不生成。
make
将第一个目标文件作为最终目标文件,如果目标文件不存在,或目标文件递归依赖的目标有改动,则该目标文件需要重新生成。否则不需要生成。
.PHONY
表示其后的目标是伪目标,而不将其作为文件处理。
3.3.2 Makefile
The format of
Makefile
is as follows.
1
2 target: prerequisites
<TAB>commandHere, I’d like to give an example.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 # File structure
.
├── Makefile
├── include
│ ├── dylib.h
│ ├── fun1.h
│ └── fun2.h
├── lib
│ └── libdy.so
└── src
├── fun1.c
├── fun2.c
└── main.c
# Makefile
main: init int/main.o int/fun1.o int/fun2.o lib/libdy.so
gcc int/main.o int/fun1.o int/fun2.o lib/libdy.so -o main -I include
int/main.o: src/main.c
gcc -c src/main.c -o int/main.o -I include
int/fun1.o: src/fun1.c include/fun1.h
gcc -c src/fun1.c -o int/fun1.o -I include
int/fun2.o: src/fun2.c include/fun2.h
gcc -c src/fun2.c -o int/fun2.o -I include
# This dynamic library doesn't have definition.
# lib/libdy.so:
.PHONY: init clean
init:
mkdir -p ./int
clean:
rm -r ./int/So, why not use IDE?
CMake
is also cool.
" Do or do not. There is no try. "
Copyright © Tony's Studio 2020 - 2022