gcc编译程序过程:

预处理:gcc -E hello.c -o hello.i
编译: gcc -S hello.i -o hello.s 生成汇编程序(文本)
汇编: gcc -c hello.s -o hello.o
gcc -c hello.c
gcc -c hello.c -o hello.o
三种方法都可生成可重定位目标程序(二进制)
连接: gcc hello.o -o hello
直接编译链接成可执行目标文件: gcc hello.c -o hello
gcc警告功能
最常用-Wall选项。该选项能发现程序中一系列的常见错误警告。
gcc -Wall hello.c -o hello
e.g.文件 test.h test.c main.c
一次性编译:
gcc test.c man.c -o pro
独立编译:
gcc -Wall -c main.c -o main.o
gcc -Wall -c test.c -o test.o
gcc -Wall main.o test.o -o pro
头文件与库文件位置

使用外部库(-l选项)
gcc -Wall man.c -o man -lm
-lm表示要链接libm.so(动态库)或者libm.a(静态库)文件。


生成静态库
库其实是一个.o文件的归档。
e.g.ar rcs libhello.a hello.o ar是gun归档工具,rcs表示replace and create
使用方式:1.gcc -Wall main.c libhello.a -o man
2.gcc -Wall -L. main.o -o man -lhello
库的搜索路径
1.从左到右搜索-I、-L指定的目录
2.由环境变量指定的目录C_INCLUDE_PATH(头文件的搜索路径) LIBRARY_PATH(库的搜索路径)
3.由系统指定的目录
生成共享库
gcc -shared -fPIC hello.o -o libhello.so
shared:表示生成共享库格式
fPIC:产生位置无关码,即可以在任何内存位置加载运行。
gcc -Wall -L. main.o -o main -lhello
运行共享库
1.拷贝.so文件到系统共享路径下 一般指/usr/lib
2.更改 LD_LIBRARY_PATH
3.ldconfig 配置ld.so.conf 之后用ldconfig更新ld.so.cache
/etc/ld.so.config
查看包含什么库: ldd hello
1257

被折叠的 条评论
为什么被折叠?



