GCC- program compilation process

In this paper, gcc compiler tool for the compilation process to record the program


Compilation (total)

When we write a simple program helloword, need to program into an executable file, you need to go through the next step is to compile.

hello.c
-----------------------------------------------
#include <stdio.h>

#define X 1

int main()
{
        int i=0;
        printf("hello world X = %d i=%d !\n",X,i);
        return 0;
}

If you are using the compiler tool is gcc, gcc hello.c will need only one output can be compiled a.out file. But the simple compilation contains several steps - pre-compile, compile, assemble, link (the link is not strictly speaking part of the build process, here to speak directly together), understanding of these steps help to deepen understanding of the process.

Precompiled

C language with a '#' at the beginning of the pre-instructions, including #ifdef, # include #define and belong to pre-compiler directive. Precompiled stage, gcc compiler directive will do a pre-treatment will be used to place the macro definition #define expanded into real value, assign #include header files to medium file, but does not check correctly after copying, to the correctness subsequent flow of the process.

gcc -E -o hello.i hello.c

As above, so that only the implementation of gcc precompiled processes need to add -E option, while general precompiled output file .i suffix. The results of this excerpt file parts, see relevant statements and header files are copied into the file after #include expand, while the main function of using the #define X is replaced by the actual places 1.

# 1 "hello.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 1 "<command-line>" 2
# 1 "hello.c"
# 1 "/usr/include/stdio.h" 1 3 4
# 27 "/usr/include/stdio.h" 3 4
# 1 "/usr/include/features.h" 1 3 4
# 367 "/usr/include/features.h" 3 4
# 1 "/usr/include/x86_64-linux-gnu/sys/cdefs.h" 1 3 4
# 410 "/usr/include/x86_64-linux-gnu/sys/cdefs.h" 3 4
# 1 "/usr/include/x86_64-linux-gnu/bits/wordsize.h" 1 3 4
# 411 "/usr/include/x86_64-linux-gnu/sys/cdefs.h" 2 3 4
# 368 "/usr/include/features.h" 2 3 4
# 391 "/usr/include/features.h" 3 4
# 1 "/usr/include/x86_64-linux-gnu/gnu/stubs.h" 1 3 4
# 10 "/usr/include/x86_64-linux-gnu/gnu/stubs.h" 3 4
# 1 "/usr/include/x86_64-linux-gnu/gnu/stubs-64.h" 1 3 4
# 11 "/usr/include/x86_64-linux-gnu/gnu/stubs.h" 2 3 4
# 392 "/usr/include/features.h" 2 3 4
# 28 "/usr/include/stdio.h" 2 3 4

...
...


extern int fprintf (FILE *__restrict __stream,
      const char *__restrict __format, ...);


extern int printf (const char *__restrict __format, ...);

...
...

# 5 "hello.c"
int main()
{
 int i=0;
 printf("hello world X = %d i=%d !\n",1,i);
 return 0;
}

Compile

In the document on the basis of the next process is to be performed, compiling and compiling the beginning of this article here is not the same thing, is compiled at the beginning of this paper is to explain several processes for short. Compiled at this document refers to the pre-compiler to compile, in which only a process. Compiler generates assembler code corresponding to the machine, likewise hello.c precompiled completed hello.i example, the use -S  compile process parameters.

gcc -S -o hello.s hello.i
        .file   "hello.c"
        .section        .rodata
.LC0:
        .string "hello world X = %d i=%d !\n"
        .text
        .globl  main
        .type   main, @function
main:
.LFB0:
        .cfi_startproc
        pushq   %rbp
        .cfi_def_cfa_offset 16
        .cfi_offset 6, -16
        movq    %rsp, %rbp
        .cfi_def_cfa_register 6
        subq    $16, %rsp
        movl    $0, -4(%rbp)
        movl    -4(%rbp), %eax
        movl    %eax, %edx
        movl    $1, %esi
        movl    $.LC0, %edi
        movl    $0, %eax
        call    printf
        movl    $0, %eax
        leave
        .cfi_def_cfa 7, 8
        ret
        .cfi_endproc
.LFE0:
        .size   main, .-main
        .ident  "GCC: (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609"
        .section        .note.GNU-stack,"",@progbits

Assemble, link

Compilation stage to do is to compile the assembly code output process is converted into binary machine code, but it does not run directly dependent on the need to link the library or file in order to get the final executable file. Assembly process using the  -c parameter, the link will take you directly link the .o files can be directly executed gcc.

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

Link lists only the example of our own hello.o, I did not see the other dependent libraries, but still be able to successfully link and can perform normal, because the system is dependent libraries in this process is loaded by default, here directly into the gcc at link -v parameter can be observed,

 gcc -v -o hello  hello.o
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 5.4.0-6ubuntu1~16.04.12' --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.12)
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/ccLdFZul.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

Use the -v output parameters above, use ctrl + f search -lc can see the parameters appear, which is linked libc library system, direct use ldd command can also see the resulting executable file hello depends library.

CryptonymAMS$: ldd hello
        linux-vdso.so.1 =>  (0x00007ffc397a6000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f4a8ed33000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f4a8f0fd000)

Hello here uses a dynamic link library linked to the system, you can use the file command to view the dynamically linked, that is, dynamic linking.

hello: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/l, for GNU/Linux 2.6.32, BuildID[sha1]=ac17e411d6f0b61087de124ee029056f09f4c990, not stripped

So far, all the gcc compiler process is completed, the executable file output.

 

supplement

gcc commonly used parameters

-E :执行到预编译流程就停止,不执行后续编译、汇编等流程
-S :只执行到编译流程,不执行后续汇编等流程
-c :只执行到汇编流程,不执行链接流程

-o :指定输出文件的名字

-I :指定头文件路径
-L :指定库文件路径
-l :指定需要链接的库(如libc.so,只需要 -lc 即可)

-static :静态链接方式(可执行文件包含执行需要的所有信息,但是文件大小会比较大)
-nostdlib :不链接标准库

-v :查看gcc版本信息,编译过程使用时用于打印编译过程的输出

More parameters can be directly or man gcc gcc --help view.

Published 19 original articles · won praise 7 · views 6927

Guess you like

Origin blog.csdn.net/G_METHOD/article/details/104163168