Skip to content

C Library

GCC 编译

# source code -> assembly code
gcc -S -o file.s file.c

# assembly code -> object code
gcc -c -o file.o file.s

# object code -> binary or library
gcc -o file file.o

Static Library

编译静态库

ar r libMYLIB.a file.o

Dynamic Library

编译动态库

gcc -shared -fPIC -o libMYLIB.so file.o

链接动态库时默认只查找标准库。在运行程序时需设置 LD_LIBRARY_PATH 来查找目录。

gcc -fPIC -shared -o libproduct.so product.c
gcc -o main main.c -L. -lproduct
export LI_LIBRARY_PATH=.
./main