首页 如何使用gcc编译器(How to use the gcc compiler)

如何使用gcc编译器(How to use the gcc compiler)

举报
开通vip

如何使用gcc编译器(How to use the gcc compiler)如何使用gcc编译器(How to use the gcc compiler) 如何使用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 ho...

如何使用gcc编译器(How to use the gcc compiler)
如何使用gcc编译器(How to use the gcc compiler) 如何使用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 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 rules Can 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 file? 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 the'login'program when the compiler compiles itself. Please read his description of the masterpiece here. Fortunately, we have gcc. When you do configure, make, and make install, GCC does a lot of hard work behind the scenes. How can we make GCC work for us? We're going to start writing a card game, but we're just going to simplify the code as much as possible to demonstrate the compiler's capabilities. We'll start from scratch, step by step, in order to understand the compilation process and understand what needs to be done and what order to make executable files. We'll 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), assembly (as), and ld. Begin... First of all, we should know how to invoke the compiler. Actually, it's very simple. We'll 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.c By default, the C compiler will generate an executable file named a.out. You can type the following command to run it: A.out Hello World Every 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 the executable file by using the -o compiler option. We'll call this program game, and we can use any name because C does not have a naming limit like Java. GCC -o game game.c Game Hello World So far, we are far from a useful program. If you feel depressed, you can think about it. We have compiled and run a program. Because we'll 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 should 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 write should work, and that it can be compiled, but it just doesn't 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 definitions so that each part of the program can look at everything in the same way. #ifndef DECK_H #define DECK_H #define DECKSIZE 52 typedef struct deck_t { 国际卡[ decksize ]; *使用的卡片数* 中断处理; } deck_t; # endif / * * / deck_h 把这个文件保存为甲板。H.只能编译。C文件,所以我们必须修改游戏C.在游戏。C的第2行,写上#包括“甲板。H”。在第5行写上deck_t甲板;为了保证我们没有搞错,把它重新编译一次。 游戏。 如果没有错误,就没有问题如果编译不能通过,那么就修改它直到能通过为止。 预编译 编译器是怎么知道deck_t类型是什么的呢,因为在预编译期间,它 实际上把”甲板。H”文件复制到了”游戏。C”文件中。源代码中的预编译指示以”#”为前缀。你可以通过在GCC后加上E选项来调用预编译器。 GCC?E?O game_precompile.txt C游戏。 我game_precompile.txt WC 3199 game_precompile.txt 几乎有3200行的输出~其中大多数来自stdio. h包含文件,但是如果你查看这个文件的话,我们的声明也在那里。如果你不用O选项指定输出文件名的话,它就输出到控制台。预编译过程通过完成三个主要任务给了代码很大的灵活性。 把”包括“的文件拷贝到要编译的源文件中。 用实际值替代“定义”的文本。 在调用宏的地方进行宏替换。 这就使你能够在整个源文件中使用符号常量(即用decksize表示一付牌中的纸牌数量),而符号常量是在一个地方定义的,如果它的值发生了变化,所有使用符号常量的地方都能自动更新。在实践中,你几乎不需要单独使用E选项,而是让它把输出传送给编译器。 编译 作为一个中间步骤,GCC把你的代码翻译成汇编语言。它一定要这样做,它必须通过分析你的代码搞清楚你究竟想要做什么。如果你犯了语法错误,它就会告诉你,这样编译就失败了。人们有时会把这一步 误解为整个过程。但是,实际上还有许多工作要GCC去做呢。 汇编 作为把汇编语言代码转换为目标代码。事实上目标代码并不能在CPU上运行,但它离完成已经很近了。编译器选项C把文件转换为以。C。O为扩展名的目标文件。如果我们运行 GCC游戏 我们就自动创建了一个名为游戏。O的文件。这里我们碰到了一个重要的问题。我们可以用任意一个。C文件创建一个目标文件。正如我们在下面所看到的,在连接步骤中我们可以把这些目标文件组合成可执行文件。让我们继续介绍我们的例子。因为我们正在编写一个纸牌游戏,我们已经把一付牌定义为deck_t,我们将编写一个洗牌函数。这个函数接受一个指向甲板类型的指针,并把一付随机的牌装入甲板类型。它使用'drawn”数组跟踪 记录 混凝土 养护记录下载土方回填监理旁站记录免费下载集备记录下载集备记录下载集备记录下载 那些牌已经用过了这个具有decksize个元素的数组可以防止我们重复使用一张牌。 #包括<<程序。” #包括< stdio. h > #包括<<时间。” #包括“甲板。” 静态time_t种子= 0; 无效的洗牌(deck_t * pdeck) { / *跟踪什么号码已用* / int引起[ decksize ] = { 0 }; int i; / *兰德*一次初始化/ 如果(0 =种子) { 种子=时间(空); srand(种子); } 为(i = 0;i < decksize;i++) { int值= 1; 做 { 价值= rand() % decksize; } 而(绘制[值]~= 0); / *标记值如使用* 绘制[值],1; *调试语句* printf(“为我”,值); pdeck ->卡[我] =价值; } pdeck ->处理= 0; 返回; } 把这个文件保存为洗牌。C.我们在这个代码中加入了一条调试语句,以便运行时,能输出所产生的牌号。这并没有为我们的程序添加功能,但是现在到了关键时刻,我们看看究竟发生了什么。因为我们的游戏还在初级阶段,我们没有别的 办法 鲁班奖评选办法下载鲁班奖评选办法下载鲁班奖评选办法下载企业年金办法下载企业年金办法下载 确定我们的函数是否实现了我们 要求 对教师党员的评价套管和固井爆破片与爆破装置仓库管理基本要求三甲医院都需要复审吗 的功能。使用那条printf语句,我们就能准确地知道现在究竟发生了什么,以便在开始下一阶段之前我们知道牌已经洗好了。在我们对它的工作感到满意之后,我们可以把那一行语句从代码中删掉。这种调试程序的技术看起来很粗糙,但它使用最少的语句完成了调试任 务。以后我们再介绍更复杂的调试器。 请注意两个问题。 我们用传址方式传递参数,你可以从'和'(取地址)操作符看出来。这把变量的机器地址传递给了函数,因此函数自己就能改变变量的值。也可以使用全局变量编写程序,但是应该尽量少使用全局变量。指针是C的一个重要组成部分,你应该充分地理解它。 我们在一个新的。C文件中使用函数调用。操作系统总是寻找名为主调的的函数,并从那里开始执行。洗牌。C中没有主调的函数,因此不能编译为独立的可执行文件。我们必须把它与另一个具有主调的函数并调用'shuffle”的程序组合起来。 运行命令 GCC 并确定它创建了一个名为洗牌。O的新文件。编辑游戏。C文件,在第7行,在deck_t类型的变量甲板声明之后,加上下面这一行: 洗牌(及甲板); 现在,如果我们还象以前一样创建可执行文件,我们就会得到一个错误 游戏。 /甲氧苄啶/ ccmihnjx。阿`主要功能: /甲氧苄啶/ ccmihnjx。O(文本+ 0xf):未定义`洗牌” 收集:LD返回退出状态1 编译成功了,因为我们的语法是正确的。但是连接步骤却失败了,因 为我们没有告诉编译器'shuffle”函数在哪里。那么,到底什么是连 接,我们怎样告诉编译器到哪里寻找这个函数呢, 连接 连接器LD,使用下面的命令,接受前面由作为创建的目标文件并把 它转换为可执行文件 GCC游戏。 这将把两个目标文件组合起来并创建可执行文件游戏。 连接器从洗牌。O目标文件中找到洗牌函数,并把它包括进可执行文 件。目标文件的真正好处在于,如果我们想再次使用那个函数,我们 所要做的就是包含”甲板。H O目标文件连接到新的可执行文件中文 件并把洗牌。 象这样的代码重用是经常发生的。虽然我们并没有编写前面作为调试 语句调用的printf函数,连接器却能从我们用#包括<程序>语句包含 的文件中找到它的声明并把存储在。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. That's why header files generally contain only data and function declarations, and there is no 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 don't put any function declarations in the header file. What can we do to make sure that everything is going well? Two other important options The -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, we'll see the following warning: Game.c:9:, warning:, implicit, declaration, of, function, `shuffle' It tells 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 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. Let's 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 compiler 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. You'll often hit it, so you should know what it means. debugging As 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 used Game 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 at 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 don't 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. 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. What's going on? We can't see anything at that window. Don't worry, KDbg has no problem. The problem is that we don't add any debug information to the executable, so KDbg can't tell us what's 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, shuffle.c, game.c GCC, -g, -o, game, game.o, shuffle.o This puts the hook into executable files so that GDB and KDbg can point out the running. Debugging is a very important technique, and it's worth your time to learn how to use it. The way a debugger helps programmers is to set breakpoints in the source 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 code 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, isn't it? Summary This article introduces the methods of compiling 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. It's a lot of work to really understand what you're doing, but I hope this article will get you started right. You can find more information in GCC, as, and LD's man and info page. Writing your own code allows you to learn more. As an exercise, you can write a 21 point game based on this card game. You can learn how to use the debugger at that time. Using GUI's KDbg can be easier to start with. If you add only a little bit of functionality each time, you'll soon be able to complete it. Remember, be sure to keep the program running all the time! To write a complete game, you need the following: A definition of a card player (that is, you can define deck_t as player_t). A function that sends a certain number of cards to a specified player. Remember to increase the number of cards in cards so that you know which cards are available. Remember how many cards are in the player's hands. Some interaction with the user asks whether the player wants another card. A function that prints cards from a player's hand. Card is equal to value% 13 (the result is 0 to 12), suit is equal to value / 13 (number 0 to 3). A function that determines the value in the player's hand. The value of Ace is zero and can be equal to 1 or 11. The King of value is 12 and can be equal to 10.
本文档为【如何使用gcc编译器(How to use the gcc compiler)】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_153723
暂无简介~
格式:doc
大小:37KB
软件:Word
页数:12
分类:生活休闲
上传时间:2018-04-15
浏览量:54