Experiment 4 learning linux c language development environment

https://www.cse.iitb.ac.in/grc/

https://edu.csdn.net/skill/gml/gml-60af2921444345bbbd4341b354bcea4f?category=875

https://blog.csdn.net/qq_36287943/article/details/103601371

实验目的

  1.Linux系统下C语言开发环境搭建

  2.学习Linux系统环境C语言开发过程

实验内容

  1.学习Linux系统中如何查看帮助文档;

  2.在Linux系统中安装C语言编译器等开发工具(编辑器,编译器);

  3.Linux系统下单个C语言文件编译链接过程(预处理,编译,汇编,链接,执行);

  4.Linux系统下多个C语言文件编译链接(命令行方式,make方式)。

注意事项

  博文名称:“Linux系统C语言开发环境学习”

 

Options Controlling the Kind of Output

控制输出类型的选项

        Compilation can involve up to four stages: preprocessing, compilation proper, assembly and linking, always in that order. 

241 compilation n [C] 1:something that makes something harder to understand,explain,or deal with.(从高级语言变为低级语言,的确令人难以理解。编译是指将易懂的高级语言翻译为不易人读懂的低级语言)

  • the negotiations stalled when complication arose.
  • 由于出现复杂情况,谈判陷入僵局

633 involve vb -volved;-volving 1 a :to have or include(sb or sth) as a part of something

  • Does this involve me?
  • Three cars were involved in the accident.

60 assembly n pl -blies 1 [U]: the act of connecting together the parts of a toy,machine,etc.

一起连接一个机器或者玩具的多个部分的行为称为装配。

  • No assembly(is) required.[=this product is already put together]

 

 

Options Controlling the Kind of Output

控制输出类型的选项
       Compilation can involve up to four stages: preprocessing, compilation proper, assembly and linking, always in that order.  GCC is capable of preprocessing and compiling several files either into several assembler input files, or into one assembler input file;
 编译最多可以包括四个阶段:预处理、适当的编译、汇编和链接,总是按照这个顺序。GCC能够对几个文件进行预处理并编译成几个汇编器输入文件或一个汇编器输入文件;
       then each assembler input file produces an object file, and linking combines all the object files (those newly compiled, and those specified as input) into an executable file.

 然后每个汇编器输入文件生成一个目标文件,并将所有的目标文件(新编译的和指定为输入的)链接到一个可执行文件中。

       For any given input file, the file name suffix determines what kind of compilation is done:

 对于任何给定的输入文件,文件名后缀决定了要进行哪种编译:

       file.c
           C source code that must be preprocessed. 说明C源代码必须进行预处理。-

       file.i
           C source code that should not be preprocessed. C源代码,不应该进行预处理。

 

  If you only want some of the stages of compilation, you can use -x (or filename suffixes) to tell gcc where to start, and one of the options -c, -S, or -E to say where gcc is to stop.  Note that some combinations (for example, -x cpp-output -E) instruct gcc to
do nothing at all.

如果您只想要编译的某些阶段,您可以使用-x(或文件名后缀)来告诉gcc从哪里开始,并使用选项之一-c、-S或-E来告诉gcc在哪里停止。注意,有些组合(例如-x cppo -output -E)指示gcc
什么都不要做。

       -c  Compile or assemble the source files, but do not link.  The linking stage simply is not done.  The ultimate output is in the form of an object file for each source file.
 编译或组装源文件,但不要链接。连系阶段还没有完成。最终输出的形式是每个源文件的目标文件。
           By default, the object file name for a source file is made by replacing the suffix .c, .i, .s, etc., with .o.
 默认情况下,源文件的目标文件名是通过将后缀.c、.i、.s等替换为.o来生成的。


           Unrecognized input files, not requiring compilation or assembly, are ignored.

不需要编译或程序集的无法识别的输入文件将被忽略。

       -S  Stop after the stage of compilation proper; do not assemble.  The output is in the form of an assembler code file for each non-assembler input file specified.
 -S编译适当阶段后停止;不组装。输出为指定的每个非汇编程序输入文件的汇编程序代码文件的形式。

           By default, the assembler file name for a source file is made by replacing the suffix .c, .i, etc., with .s.
 默认情况下,源文件的汇编程序文件名是通过将后缀.c、.i等替换为.s来生成的。

           Input files that don't require compilation are ignored.

 不需要编译的输入文件将被忽略。

       -E  Stop after the preprocessing stage; do not run the compiler proper.  The output is in the form of preprocessed source code, which is sent to the standard output.
 -E预处理阶段结束后停止;不要正确地运行编译器。输出是预处理源代码的形式,它被发送到标准输出。

           Input files that don't require preprocessing are ignored.

 不需要预处理的输入文件会被忽略。

       -o file

 - o文件
           Place output in file file.  This applies to whatever sort of output is being produced, whether it be an executable file, an object file, an assembler file or preprocessed C code.

 将输出放在文件文件中。这适用于正在生成的任何类型的输出,无论是可执行文件、目标文件、汇编文件还是预处理的C代码。

           If -o is not specified, the default is to put an executable file in a.out, the object file for source.suffix in source.o, its assembler file in source.s, a precompiled header file in source.suffix.gch, and all preprocessed C source on standard output.

 如果没有指定-o,默认是将可执行文件放入a.out中,即源文件的目标文件。后缀的来源。O,源代码中的汇编文件。S, source.suffix中的一个预编译头文件。和所有预处理的C源对标准输出。



GCC简单编译流程
1.gcc编译流程
gcc 将hello.c源文件编译成可执行的binary文件需要经过hello.i、hello.s、hello.o、hello四个步骤,如图所示
hello.c 预处理 hello.i 编译 hello.s 汇编 hello.o 链接 hello(a.out)
1.1预处理:展开头文件和宏定义等。
gcc -E hello.c -o hello.i
1.2编译:将预处理得到的源代码转换成汇编文件(得到汇编文件)
gcc -S hello.i -o hello.s
1.3汇编:将汇编代码转成不可执行的机器码文件(得到机器码文件)
gcc -c hello.s -o hello.o
1.4链接:将不可执行的机器码文件转成可执行文件,把各种符号引用的符号定义转换成可执行文件中的合适信息,通常是虚拟地址(得到可执行文件)
gcc hello.o -o hello
我们也可以直接使用gcc hello.c -o hello 一步生成binary文件hello
 

 

1.安装C语言开发环境

1)安装C语言编译环境

2) 查看gcc版本信息

3)查看make版本信息

 

2.简单的C语言练习

1)首先通过man命令查看帮助文档

 

2)命令行模式简单C语言编译链接等操作

通过vim编辑器写一个最简单的C语言程序,如写一个输出一句话的程序。

先创建一个文件夹,然后进入该新创建的文件夹。

输入完了源程序,接下来我们进行C语言程序编译的四个阶段,预编译,编译,汇编,链接

a)预编译 (-E  Stop after the preprocessing stage;do not run the compiler proper. )

b)编译(-S  Stop after the stage of compilation proper; do not assemble. )

gcc -S #生成汇编源文件
cat test.s #显示如下
vim test.s #显示如下

c)汇编(-c  Compile or assemble the source files, but do not link.  The linking stage simply is not done. The ultimate output is in the form of an object file for each source file.)

gcc -c #编译源码并最后生成目标文件

 

通过objdump命令来反汇编该文件来分析文件内容

d)链接

 

可执行文件是二进制文件,同样需要用objdump命令来反汇编该文件分析文件内容

E)运行

 

多文件编译

在exp4文件夹下面创建一个文件夹mfc(multi-files compile)

在文件夹下创建5个文件,文件名分别如下:

 

 

完成上面的代码输入,接下来我们进行编译。

对于多文件编译,我们可以一次编译链接生成可执行文件,也可以分别编译每个C文件

最后再进行链接得到可执行文件。

 

 

请说明两种方法的区别?

第一种方法:一次编译链接直接生成可执行文件,不产生目标文件;

第二种方法:分别对每个C文件进行编译产生目标文件,之后再进行链接得到可执行文件。

接下来我们编写一个简单的Makefile,通过make工具帮助我们进行编译。在当前文件夹编写一个Makefile文件

 

 

 

 
 
 
 
 
 
 
 
 
posted @ 2022-06-02 23:27  昵称可修改  阅读(49)  评论(1)    收藏  举报