查看帮助一是man gcc,二是进入www.gnu.org,找到gcc的帮助文档(更详细)。
一、gcc编译过程hello可执行文件
二、gcc编译命令
gcc hello.o -o hello
gcc -Wall hello.o -o hello 能够提示出更多的错误
hello.c中的代码如下:
#include <stdio.h> void hello() { printf("Congratulation gcy\n"); }hello.h中的代码如下:
void hello();main.c中的代码如下:
#include <stdio.h> #include <hello.h> int main() { printf("call hello()\n"); hello(); printf("\n"); return 0; }1、使用静态链接库(*.a)
ar crv libhello.a libhello.o 生成静态链接库
gcc -c main.c -o main.o -I./
gcc -static main.o -o main -L./ libhello.a 头文件和静态库都是在当前目录下./main 执行不需要依赖其他文件,输出结果为call hello() Congratulation gcy。
gcc -shared libhello.o -o libhello.so 生成动态链接库
gcc -c main.c -o main.o -I./
gcc main.o -o main2 -L./ libhello.so 头文件和静态库都是在当前目录下把libhello.so放到/usr/lib中,或者用下面的办法,
ls /etc/*conf,vi ld.so.conf,显示内容,include /etc/ld.so.conf.d/*.conf,cd /etc/ld.so.conf.d/,vi libc.conf,再这里面增加路径。
/lib/ld-linux.so.2 (0xb7717000)
3、编译多个文件
gcc -c hello.c -o hello.o
gcc -c main.c -o main.o
gcc main.o hello.o -o main
或者 gcc main.c hello.c -o main
./main ,输出结果为call hello() Congratulation gcy。
#include <stdio.h> int main() { printf("hello\n"); return 0; }stdio.h位于/usr/include/中,里面有printf的声明extern int printf (__const char *__restrict __format, ...);此处extern可以省略不写。
静态链接:gcc -static main.c -o main
预处理时会到/usr/include下找到stdio.h,链接时到/usr/lib下找printf对应的静态库,执行之后不再需要依赖其他静态库,故文件较大。
动态链接:gcc main.c -o main2
预处理时会到/usr/include下找stdio.h,链接时到/usr/lib下找printf对应的动态库,执行之后,要依赖/usr/lib中的动态库,故文件较小。