版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、如何使用gcc编译器(How to use the gcc compiler)Abstract:To understand this article, you need to have a basic understanding of the C language, and this article will show you how to use the GCC compiler. First, we describe how to compile a simple C source code using the compiler in the command line. Then, we
2、briefly describe what the compiler does and how to control the compilation process. We also give a brief overview of how debuggers are used.GCC rulesCan you imagine a private compiler that uses closed source code to compile free software? How do you know what the compiler adds to your executable fil
3、e? May add all kinds of back door and Trojan horse. Ken Thompson is a famous hacker who wrote a compiler that left a back door and a permanent trojan in theloginprogram when the compiler compiles itself. Please read his description of the masterpiece here. Fortunately, we have gcc. When you do confi
4、gure, make, and make install, GCC does a lot of hard work behind the scenes. How can we make GCC work for us? Were going to start writing a card game, but were just going to simplify the code as much as possible to demonstrate the compilers capabilities. Well start from scratch, step by step, in ord
5、er to understand the compilation process and understand what needs to be done and what order to make executable files. Well see how to compile the C program and how to use the compile options to make GCC work as we want. The steps (and the tools used) are as follows: -E (GCC), compilation (GCC), ass
6、embly (as), and ld.Begin.First of all, we should know how to invoke the compiler. Actually, its very simple. Well start with the famous first C program. Forgive me, old timers.#include Int, main ()Printf (Hello World, n);Save this file as game.c. You can compile it at the command line:GCC game.cBy d
7、efault, the C compiler will generate an executable file named a.out. You can type the following command to run it:A.outHello WorldEvery time a compiler is compiled, the new a.out overrides the original program. You have no idea which program created the a.out. We can tell GCC what we want to name th
8、e executable file by using the -o compiler option. Well call this program game, and we can use any name because C does not have a naming limit like Java.GCC -o game game.cGameHello WorldSo far, we are far from a useful program. If you feel depressed, you can think about it. We have compiled and run
9、a program. Because well add functionality to this program bit by bit, we have to make sure it works. It seems that every programmer who has just started programming wants to compile a program of 1000 lines at once, and then modify all the errors at once. Nobody, I mean, nobody can do this. You shoul
10、d start with a small program that can run, modify it, and then run it again. This will limit the amount of error you have modified at one time. Also, you know what changes have been made to make the program run, so you know where to focus. This prevents this from happening: you think that what you w
11、rite should work, and that it can be compiled, but it just doesnt work. Remember, the ability to compile a program does not mean that it is correct.Next, write a header file for our game. Header files focus data types and function declarations to one place. This ensures consistency of data structure
12、 definitions so that each part of the program can look at everything in the same way.#ifndef DECK_H#define DECK_H#define DECKSIZE 52typedef struct deck_t国际卡 decksize ;*使用的卡片数*中断处理; deck_t;# endif / * * / deck_h把这个文件保存为甲板。H.只能编译。C文件,所以我们必须修改游戏C.在游戏。C的第2行,写上#包括“甲板。H”。在第5行写上deck_t甲板;为了保证我们没有搞错,把它重新编译一次
13、。游戏。如果没有错误,就没有问题如果编译不能通过,那么就修改它直到能通过为止。预编译编译器是怎么知道deck_t类型是什么的呢?因为在预编译期间,它实际上把”甲板。H”文件复制到了”游戏。C”文件中。源代码中的预编译指示以”#”为前缀。你可以通过在GCC后加上E选项来调用预编译器。GCCEO game_precompile.txt C游戏。我game_precompile.txt WC3199 game_precompile.txt几乎有3200行的输出!其中大多数来自stdio. h包含文件,但是如果你查看这个文件的话,我们的声明也在那里。如果你不用O选项指定输出文件名的话,它就输出到控制台
14、。预编译过程通过完成三个主要任务给了代码很大的灵活性。把”包括“的文件拷贝到要编译的源文件中。用实际值替代“定义”的文本。在调用宏的地方进行宏替换。这就使你能够在整个源文件中使用符号常量(即用decksize表示一付牌中的纸牌数量),而符号常量是在一个地方定义的,如果它的值发生了变化,所有使用符号常量的地方都能自动更新。在实践中,你几乎不需要单独使用E选项,而是让它把输出传送给编译器。编译作为一个中间步骤,GCC把你的代码翻译成汇编语言。它一定要这样做,它必须通过分析你的代码搞清楚你究竟想要做什么。如果你犯了语法错误,它就会告诉你,这样编译就失败了。人们有时会把这一步误解为整个过程。但是,实际
15、上还有许多工作要GCC去做呢。汇编作为把汇编语言代码转换为目标代码。事实上目标代码并不能在CPU上运行,但它离完成已经很近了。编译器选项C把文件转换为以。C。O为扩展名的目标文件。如果我们运行GCC游戏我们就自动创建了一个名为游戏。O的文件。这里我们碰到了一个重要的问题。我们可以用任意一个。C文件创建一个目标文件。正如我们在下面所看到的,在连接步骤中我们可以把这些目标文件组合成可执行文件。让我们继续介绍我们的例子。因为我们正在编写一个纸牌游戏,我们已经把一付牌定义为deck_t,我们将编写一个洗牌函数。这个函数接受一个指向甲板类型的指针,并把一付随机的牌装入甲板类型。它使用drawn”数组跟踪
16、记录那些牌已经用过了这个具有decksize个元素的数组可以防止我们重复使用一张牌。#包括程序。”#包括#包括时间。”#包括“甲板。”静态time_t种子= 0;无效的洗牌(deck_t * pdeck)/ *跟踪什么号码已用* /int引起 decksize = 0 ;int i;/ *兰德*一次初始化/如果(0 =种子)种子=时间(空);srand(种子);为(i = 0;i 卡我 =价值;pdeck -处理= 0;返回;把这个文件保存为洗牌。C.我们在这个代码中加入了一条调试语句,以便运行时,能输出所产生的牌号。这并没有为我们的程序添加功能,但是现在到了关键时刻,我们看看究竟发生了什么。
17、因为我们的游戏还在初级阶段,我们没有别的办法确定我们的函数是否实现了我们要求的功能。使用那条printf语句,我们就能准确地知道现在究竟发生了什么,以便在开始下一阶段之前我们知道牌已经洗好了。在我们对它的工作感到满意之后,我们可以把那一行语句从代码中删掉。这种调试程序的技术看起来很粗糙,但它使用最少的语句完成了调试任务。以后我们再介绍更复杂的调试器。请注意两个问题。我们用传址方式传递参数,你可以从和(取地址)操作符看出来。这把变量的机器地址传递给了函数,因此函数自己就能改变变量的值。也可以使用全局变量编写程序,但是应该尽量少使用全局变量。指针是C的一个重要组成部分,你应该充分地理解它。我们在一
18、个新的。C文件中使用函数调用。操作系统总是寻找名为主调的的函数,并从那里开始执行。洗牌。C中没有主调的函数,因此不能编译为独立的可执行文件。我们必须把它与另一个具有主调的函数并调用shuffle”的程序组合起来。运行命令GCC并确定它创建了一个名为洗牌。O的新文件。编辑游戏。C文件,在第7行,在deck_t类型的变量甲板声明之后,加上下面这一行:洗牌(及甲板);现在,如果我们还象以前一样创建可执行文件,我们就会得到一个错误游戏。/甲氧苄啶/ ccmihnjx。阿主要功能:/甲氧苄啶/ ccmihnjx。O(文本+ 0xf):未定义洗牌”收集:LD返回退出状态1编译成功了,因为我们的语法是正确的
19、。但是连接步骤却失败了,因为我们没有告诉编译器shuffle”函数在哪里。那么,到底什么是连接?我们怎样告诉编译器到哪里寻找这个函数呢?连接连接器LD,使用下面的命令,接受前面由作为创建的目标文件并把它转换为可执行文件GCC游戏。这将把两个目标文件组合起来并创建可执行文件游戏。连接器从洗牌。O目标文件中找到洗牌函数,并把它包括进可执行文件。目标文件的真正好处在于,如果我们想再次使用那个函数,我们所要做的就是包含”甲板。H O目标文件连接到新的可执行文件中文件并把洗牌。象这样的代码重用是经常发生的。虽然我们并没有编写前面作为调试语句调用的printf函数,连接器却能从我们用#包括语句包含的文件中
20、找到它的声明并把存储在。H,C库(/ lib / libc.so。6) connect the target code in. This way we can use the functions of other people who have been able to work correctly and care only about the problems we are going to solve. Thats why header files generally contain only data and function declarations, and there is no
21、 function body. In general, you can create an object file or a library of functions for connectors to connect to executable files. Our code may cause problems because we dont put any function declarations in the header file. What can we do to make sure that everything is going well?Two other importa
22、nt optionsThe -Wall option opens all types of syntax warnings to help us determine that the code is correct and that it can be as portable as possible. When we compile our code using this option, well see the following warning:Game.c:9:, warning:, implicit, declaration, of, function, shuffleIt tells
23、 us that there is still some work to be done. We need to add a line of code to the header file to tell the compiler everything about the shuffle function so that it can do the necessary checking. Sounds like a quibble, but doing so can the function definition and implementation of separate, so that
24、we can use our function in any place, as long as contains a new header file and connect it to our target file on it. Lets add this line to deck.h.Void shuffle (deck_t *pdeck);This removes the warning message.Another common compiler option is the optimization option -O# (i.e., -O2). This tells the co
25、mpiler what level of optimization you need. The compiler has a set of tricks to make your code run a little faster. For small programs like ours, you may not notice the difference, but for large programs, it can greatly increase the speed of operation. Youll often hit it, so you should know what it
26、means.debuggingAs we all know, code passing through does not mean that it works as we have to ask. You can use the following command to verify that all the numbers are usedGame sort - N less | |And check to see if there is any omission. What should we do if there is a problem? How can we find bugs a
27、t the bottom?You can use the debugger to check your code. Most distributions offer the famous debugger: gdb. If you have a lot of command line options that you dont know what to do, you can use KDE, a very good front-end tool for KDbg. There are also some other front-end tools that are very similar.
28、 To start debugging, you can select File-Executable and find your game program. When you press the F5 button or select Execution- to run from the menu, you can see the output in another window. Whats going on? We cant see anything at that window. Dont worry, KDbg has no problem. The problem is that
29、we dont add any debug information to the executable, so KDbg cant tell us whats going on inside. Compiler option -g allows you to add the necessary debugging information to the target file. You have to use this option to compile the target file (extension.O), so the command line becomes:GCC, -g, -c,
30、 shuffle.c, game.cGCC, -g, -o, game, game.o, shuffle.oThis puts the hook into executable files so that GDB and KDbg can point out the running. Debugging is a very important technique, and its worth your time to learn how to use it. The way a debugger helps programmers is to set breakpoints in the so
31、urce code. Now you can right-click the line that calls the shuffle function and try to set the breakpoint. There is a small red circle on the side of the line. Now, when you press the F5 key, the program stops executing on that line. Press F8 to jump into the shuffle function. Well, we can see the c
32、ode in shuffle.c now! We can control the program step by step,And see what really happened. If you pause the cursor on the local variable, you will see the contents of the variable. Excellent! This is much better than that printf statement, isnt it?SummaryThis article introduces the methods of compi
33、ling and debugging C program in general. We discussed the steps the compiler went through, and what options should be passed to GCC in order for the compiler to do the work. We outlined the connection to the shared function library, and finally introduced the debugger. Its a lot of work to really understand what youre doing, but I hope this article will get you started right. You can find more information in GCC, a
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 企业管理-财务内部监督管理制度
- 浙江省台州市椒江区2025-2026学年招生统一考试模拟化学试题试卷含解析
- 北京市昌平区昌平五中学2026年初三下学期一轮模拟数学试题试卷含解析
- 湖北省武汉市新洲区2026年初三暑假末结业考试物理试题含解析
- 山东省日照岚山区五校联考2025-2026学年初三下学期第二次阶段(期中)考试题含解析
- 2026年天津市红桥区复兴中学初三下学期寒假收心模拟考试数学试题试卷含解析
- 江西省2026届中考全国统考预测密卷(1)物理试题试卷含解析
- 胆管癌术后康复评估方法
- 信息化绩效考核制度
- 审计署考核制度
- 实习护士第三方协议书
- 《云南教育强省建设规划纲要(2024-2035年)》解读培训
- 评审专家聘任协议书
- 民宿委托经营管理协议合同书
- 2024-2025学年鲁教版(五四学制)(2024)初中英语六年级下册(全册)知识点归纳
- 2025全国市场监督管理法律知识竞赛测试题库(含答案解析)
- 金融企业呆账核销管理办法(2024年)
- 设备验证培训
- 2025年湖北省八市高三(3月)联考政治试卷(含答案详解)
- 《趣味学方言》课件
- GB/T 19973.2-2025医疗产品灭菌微生物学方法第2部分:用于灭菌过程的定义、确认和维护的无菌试验
评论
0/150
提交评论