GCC easy to understand tutorial - primary articles

Copyright Notice: Copyright: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/qq_42475711/article/details/85224010

GCC easy to understand tutorial - primary articles

  2018-12-17 warm weather, belongs to the winter sun. Recently in learning to use gvim, thinking to abandon reliance on the IDE also want to understand the process of compilation, but in addition to learning outside gvim variety of command must first learn to use the gcc compiler. Basic use gcc this article will record a lucid way, but also did not forget to explore expansion.

——Author:Calm

  • What is the GCC, it can do?

  • GCC, gcc, g ++ What is the relationship between the three?

  • Before we begin developing what to do?

  • gcc commonly used instructions explain?

  • What is gcc, it can do?

      GCC (GNU Compiler Collection), the GNU compiler suite, is a programming language compiler, which is formerly known as GCC (GNU C Compiler) that is GNU c compiler, although the same abbreviation but functionally significant difference. GCC intention is for the GNU operating system specifically written a compiler, GNU is dedicated to the original C code is compiled, now it has been extended to compile C, C ++, Java, Objective-C, and other programming language compiler collection. This article introduces the gcc or g ++ use.

  • GCC, gcc, g ++ What is the relationship between the three?

      gcc (GUN C Compiler) c is the GCC compiler, and g ++ (GUN C ++ Compiler) is c ++ to the GCC compiler.
      Both gcc and g ++ can be compiled c and cpp files, but there are differences. gcc when compiling cpp syntax according to c to compile default but can not link to c ++ libraries (gcc default link library c, g ++ c ++ default link library). g ++ to compile .c and .cpp files are unified by the rules of grammar to compile cpp. It is generally used to compile c gcc, c ++ ++ compiler with g. There will be time later to continue to explore in depth the difference.

  • Before we begin developing what to do?

    • Software installation tutorial:
        Direct Quguan downloading MinGW package manager (download link: https://osdn.net/projects/mingw/releases/ )

        Once you have downloaded the installation package manager and run it, you can see the following interface:

      Here Insert Picture Description

        Left Selection "Basic Setup" option, check the right side (click "Mark for Installation") components to be installed "mingw32-gcc-g ++". Suppose you also need to use words MinGW to compile Fortran, Object-C, Ada language can also hook the appropriate option. After selecting click on the upper left corner of the Installation menu option Apply changes began online download and install related components.

        After installing the best configuration environment variable, the MinGw the bin directory to the path environment variable, so dos in any directory can invoke an executable file in the bin directory, other software can also find the location of gcc. Enter cmd gcc -v view the version number.

      all test

  • gcc commonly used instructions explain?

      Using gcc compiler * .c file is not directly generated on Windows * .exe files (on Linux as * .out), in the middle also experienced a pretreatment, compiled and assembled several processes, fortunately gcc provides instruction generating an intermediate file Although the usual development time and paid little attention to these * .i * .s file generated during compilation, but because they have a certain understanding is always good.

    Here Insert Picture Description

    • [Instructions] gcc
        with gcc compiler directive * .c files can be generated * exe executable file. Here we use a simple piece of code to explain c, Demo.c within the source code as follows:

      
      	#include<stdio.h>
      	
      	int main(void)
      	{
      	    printf("nihao\n");
      	
      	    return 0;
      	}
      

        Open cmd, cmd Note that the default path is typically c drive, and Demo.c file may not be in the default path cmd, we need to use dos command cd can manipulate files in the directory where Demo.c, example:

      all test

        After executing instructions Demo.c gcc generates an executable file a.exe file in the directory, because the file name of exe not developed so the default is a.exe.

      alt text

        当然也不一定要cd进Demo.c的目录,使用gcc指令时给出Demo.c的绝对路径也可以成功编译,命令如下:

      gcc E:\WorkSpace\Test_gcc\Demo.c

    • 【指令】 -o
        指令-o(小写)用来指定生成的文件名。

      alt text

      结果如下:

      alt text

        生成的文件不一定要在.c所在目录,可以给出路径指定。

      gcc Demo.c -o ..\Demo.exe

        该指令指定将exe生成到上一级目录。

        生成exe后我们试者执行一下看看结果。

      alt text

    • 【指令】 -E(预处理(Preprocessing))
        指令 -E(大写)将执行预处理操作也即生成*.i文件,gcc编译器将对#开头的指令进行解析。我们修改Demo.c中的代码为如下:

      
      	#include <stdio.h>
      	#include <stdlib.h>
      	#include "Demo.h"
      	
      	int main()
      	{
      	    int a = N;                      //宏常量
      	    int b = 2;
      	    int c = 0;
      	    
      	    c = a + b;
      	    printf("%d\n", c);
      	    
      	    CODE                            //宏替换代码段
      	
      	    system("pause");
      	    return 0;
      	}
      

      在Demo.c同目录下编辑Test.h文件,源码如下:

      
      	#define N 1
      	
      	#define     CODE      if(c > 2)                              \
      	                                {                            \
      	                                    printf("c > 2\n");       \
      	                                }
      	
      	void DoNothing(void);           //函数声明(该函数未被调用)
      

      在Demo.c同目录下编辑Test.c文件,源码如下:

      
      	#include"Test.h"
      	
      	void DoNothing(void)
      	{
      	    ;
      	
      	    return ;
      	}
      

        从源码可知Demo.c中在预处理阶段需要把调用的头文件包含进来,替换宏常量和宏代码段。我们执行指令gcc -E Demo.c -o Demo.i并看看Demo.i文件中的代码。

      Demo.i文件内容:

      alt text

        -E 指令使gcc执行预处理,预处理时对#类指令进行处理(包含头文件、替换宏常量和宏代码段等操作),也就不难理解为何Demo.i文件会很大了(38KB),整个工程最后被揉合成一个大文件。预处理把注释都去掉了,所以反编译回来的代码是没有注释的!因为注释早就被去掉了。另外可以一提的是使用指令gcc -E Demo.c不指定输出的文件名时内容将会直接输出到Dos框中,而不会产生文件。

        我们知道c语言出现语法错误时编译器将会报错,但检查语法错误是在预处理、编译、汇编、链接、生成可执行文件中的哪个阶段执行的呢?为了方便我们编辑Demo2.c代码,有意使源码代码由于错误测试预处理阶段是否会报语法错误。

      
      	#include<stdio.h>
      	
      	int main(void)
      	{
      	    aabbccdd          //此处有语法错误
      	
      	    return 0;
      	}
      

        对Demo2.c执行预处理操作gcc -E Demo2.c

      all test

        由此可知预处理阶段不检查语法错误。

    • 【指令】 -S(编译(Compiling))
        执行-S(大写)指令将*.i文件中源码转化为汇编代码*.s文件。我们执行指令gcc -S Demo.i -o Demo.s,在当前目录下将生成Demo.s文件,用记事本打开会发现c源码已经被编译器转化为汇编代码。

      Here Insert Picture Description

        如果使用指令gcc -S Demo.i即不指定输出文件名,默认也将会在当前目录下产生文件Demo.s。

        关于前面含有语法错误的Demo2.c我们在编译阶段再试一次,看看是否能检查出语法错误。

      alt text

        对语法的检查是再编译阶段进行的。

    • 【指令】 -c(汇编(Assembling))
        执行-c(小写)指令将*.s文件中的汇编源码转化未机器能执行的二进制机器码,生成文件*.o。执行指令gcc -c Demo.s -o Demo.o。生成的Demo.o为二进制文件,用记事本打开就是一堆乱码,我这里就不贴图出来了。

    • 【指令】 gcc *.o(链接(Linking))
        经过汇编处理后生成的二进制文件Demo.o虽然已经机器码,但仍然无法运行因为少了链接操作。链接操作可执行指令gcc Demo.o -o Demo.exe

      alt text

        Links that appear on Demo.o execution error, suggesting that the function can not find the definition of DoNothing. When the value of pretreatment stage will include the header file into Demo.i file, that contains Demo.i declaration of the function, the compiler only checks the stage at the function call statement and meets function prototypes, did not go to check other * defined functions .c file.
        Link stage is the function definition should function library association to come, of course, will not find the function definition error. So the question is, why we call the standard library function printf is not being given? In fact, library functions is no exception, in the link stage needs to be associated to the function definition, it has been given in the declaration part of the standard library functions in the header file stdio.h, achieved in part of a standard library a good package, different systems next there will be differences. gcc is the default when you link library file path contains the standard library, so it can find the definition of printf function without error. The DoNothing function of our users to write their own, not packaged into a library gcc course, can not find its definition. If the Demo.c in DoNothing function call can be performed to remove the link operation a success? of course can!

        To make Demo.o can successfully generate an executable file Demo.exe, we can Test.c packaged as a static library for their calls, so there is a function definition. Executes instructions gcc -c Test.c -o Test.oto generate Test.o binaries. This is not without performing pre-processing and compilation operations Test.c, but the compiler to help us do it. Then Test.o generate static library files libTest.a, and then try to perform operations on Demo.o link again.

      alt text

        The compiler can see no error and successfully generated Demo.exe, the results have been as expected.
        The difference between the role and gcc instructions about generating static and dynamic libraries and much more other instructions, static and dynamic libraries, and so have the time to continue to write articles to explain the middle of it, I read an article written too long would be boring, I say I need a rest.

Guess you like

Origin blog.csdn.net/qq_42475711/article/details/85224010