Http://blog.csdn.net/feixiaoxing/article/details/7194756#comments
[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]
In the eyes of many people, C language and Linux are often inseparable. There are many reasons for this. The most important part is that Linux itself is an outstanding work of C language. However, the Linux operating system supports the C language quite well. As a realProgramIf you haven't compiled a complete program in C language in Linux, you can only say that his understanding of the C language itself is superficial, and his understanding of the system itself is not in place. As a programmer, the Linux system provides us with many ideal environments, including the following aspects,
(1) A complete compilation environment, including GCC, As, LD, and other compilation and link tools
(2) Powerful debugging environment, mainly GDB tools
(3) rich automatic compilation tools, mainly make tools
(4) Diversified OS options, such as Ubuntu and redflag
(5) vast developmentSource codeLibrary
Of course, no matter what I say, my friends should be brave enough to take the first step forward. If you have no Linux programming experience, you can first install a virtual machine on your PC, and then write your own C language in shell.Code.
[CPP] View plaincopy
- # Include <stdio. h>
- IntMain ()
- {
- Printf ("Hello! \ N");
- Return1;
- }
After writing the above Code, you need to do two steps: 1. Input GCC hello. C-O hello; 2. Input./hello. If everything works, you will see a line of Hello printing on the screen. If you see it, congratulations, you can start LinuxC LanguageProgramming journey.
Of course, we will not be satisfied with such a simple printing function. Below we can write a simple iterative function,
[CPP] View plaincopy
- # Include <stdio. h>
-
-
- IntIterate (IntValue)
-
- {
-
- If(1 = value)
-
- Return1;
-
- ReturnIterate (value-1) + value;
-
- }
-
- IntMain ()
-
- {
-
- Printf ("% D \ n", Iterate (10 ));
-
- Return1;
-
- }
In this case, repeat the preceding steps: 1. Input GCC hello. C-O hello; 2. Input./hello. Of course, if everything is OK, you will see the output of 55 on the screen. Originally, the sum of data from 1 to 10 is 55, which indicates that our program is correct.
Of course, there will be some friends interested in program disassembly, so he needs two steps: 1. GCC hello. c-g-o hello; 2. objdump-s-d. /Hello. The reason why-G is added during GCC compilation is to add debugging information. The-s option in objdump is to display the original C language source code when the assembly code is displayed.
[CPP] View plaincopy
- IntIterate (IntValue)
-
- {
-
- 8048374: 55 push % EBP
-
- 8048375: 89 E5 mov % ESP, % EBP
- 8048377: 83 EC 08 Sub $0x8, % ESP
-
- If(1 = value)
-
- 804837a: 83 7d 08 01 CMPL $0x1,0x8 (% EBP)
-
- 804837e: 75 09 JNE 8048389 <iterate + 0x15>
- Return1;
-
- 8048380: C7 45 FC 01 00 00 00 movl $0x1, 0 xfffffffc (% EBP)
-
- 8048387: EB 16 JMP 804839f <iterate + 0x2b>
-
- ReturnIterate (value-1) + value;
- 8048389: 8B 45 08 mov 0x8 (% EBP), % eax
-
- 804838c: 83 E8 01 Sub $0x1, % eax
-
- 804838f: 89 04 24 mov % eax, (% ESP)
- 8048392: E8 dd FF call 8048374 <iterate>
-
- 8048397: 8B 55 08 mov 0x8 (% EBP), % edX
-
- 804839a: 01 C2 add % eax, % edX
- 804839c: 89 55 FC mov % edX, 0 xfffffffc (% EBP)
-
- 804839f: 8B 45 FC mov 0 xfffffffc (% EBP), % eax
-
- }
-
- 80483a2: C9 leave
- 80483a3: C3 RET
-