gcc常用命令记录

gcc的使用格式

gcc [options] file.
-v Display the programs invoked by the compiler(查看gcc编译器的版本)
-o Place the output into (这里的file不能与要被编译的文件名相同,否则就重名了)
-E Preprocess only; do not compile, assemble or link(预处理不会编译、汇编、链接)
-S Compile only; do not assemble or link(只进行编译)
-c Compile and assemble, but do not link(编译和汇编但是不进行链接)

省略编译过程

编译的方式1:
gcc hello.c  输出一个a.out 然后./a.out 来执行该应用程序
方式2:
gcc -o hello  hello.c  输出hello ,然后执行./hello来执行该应用程序

编译过程

gcc -E hello.i hello.c 预处理
修正 gcc -E hello.c -o hello.i
gcc -S hello.s hello.i 编译
修正 gcc -S hello.i -o  hello.s
gcc -c hello.o hello.s 编译和汇编
修正 gcc -c hello.s -o hello.o
gcc -o hello hello.o 编译、汇编、链接

.o 文件被称为obj文件

预处理 --> 编译 -->汇编-->链接

4个步骤具体的作用

预处理

在这里插入图片描述

编译

在这里插入图片描述

汇编

在这里插入图片描述

链接

在这里插入图片描述

方式3

gcc -c -o hello.o hello.c
gcc -o hello hello.o

gcc会对.c文件默认进行预处理操作 -c指明了编译、汇编、从而得到.o文件,再通过gcc -o hello hello.o 将.o 文件进行连接

链接过程

使用gcc -v -o hello hello.o
-V 参数查看链接的详细信息

$ gcc -v -o hello hello.o
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=
/usr/lib/gcc/x86_64-linux-gnu/5/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 5.4.0-6ubuntu1~16.04.10' --with-bugurl=file://
/usr/share/doc/gcc-5/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=
/usr --program-suffix=-5 --enable-shared --enable-linker-build-id --libexecdir=
/usr/lib --without-included-gettext --enable-threads=posix --libdir=
/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=
/usr/lib/jvm/java-1.5.0-gcj-5-amd64/jre --enable-java-home --with-jvm-root-dir=
/usr/lib/jvm/java-1.5.0-gcj-5-amd64 --with-jvm-jar-dir=
/usr/lib/jvm-exports/java-1.5.0-gcj-5-amd64 --with-arch-directory=amd64 --with-ecj-jar=
/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.10)
COMPILER_PATH=
/usr/lib/gcc/x86_64-linux-gnu/5/:
/usr/lib/gcc/x86_64-linux-gnu/5/:
/usr/lib/gcc/x86_64-linux-gnu/:
/usr/lib/gcc/x86_64-linux-gnu/5/:
/usr/lib/gcc/x86_64-linux-gnu/
LIBRARY_PATH=
/usr/lib/gcc/x86_64-linux-gnu/5/:
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/:
/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib/:/lib/x86_64-linux-gnu/:/lib/../lib/:
/usr/lib/x86_64-linux-gnu/:
/usr/lib/../lib/:
/usr/lib/gcc/x86_64-linux-gnu/5/../../../:/lib/:
/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-o' 'hello' '-mtune=generic' '-march=x86-64'
 
/usr/lib/gcc/x86_64-linux-gnu/5/collect2 -plugin 
/usr/lib/gcc/x86_64-linux-gnu/5/liblto_plugin.so -plugin-opt=
/usr/lib/gcc/x86_64-linux-gnu/5/lto-wrapper -plugin-opt=-fresolution=/tmp/cc7E4fP6.res -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s --sysroot=/ --build-id --eh-frame-hdr -m elf_x86_64 --hash-style=gnu --as-needed -dynamic-linker /lib64/ld-linux-x86-64.so.2 -z relro -o hello 
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o 
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crti.o 
/usr/lib/gcc/x86_64-linux-gnu/5/crtbegin.o -L
/usr/lib/gcc/x86_64-linux-gnu/5 -L
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu -L
/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib -L/lib/x86_64-linux-gnu -L/lib/../lib -L
/usr/lib/x86_64-linux-gnu -L
/usr/lib/../lib -L
/usr/lib/gcc/x86_64-linux-gnu/5/../../.. hello.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed 
/usr/lib/gcc/x86_64-linux-gnu/5/crtend.o 
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crtn.o

分析
crt1.o、crti.o 、crtbegin.o、crend.o、crtn.o 是gcc 加入的系统标准启动文件
对于一般应用程序,这些启动是必须的

-lc :链接libc库其中libc库文件中实现了printf等函数

-nostartfiles
在这里插入图片描述

-nostdlib在这里插入图片描述
使用ldd命令查看库:

动态链接

动态链接会不会吧基本的库包含的运行文件当中,当运行的时候回去系统中调用库

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

静态链接

gcc -static -o hello_static hello.c

静态链接就是把库也包含到最后执行文件当中
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

gcc -static

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值