浅谈GCC 编程-入门
下面我想总结学习GCC编程的一些基本方面,同时也记录学习的一个过程。
1.GCC编程环境。
GCC是在linux系统上运行,是linux操作系统的一个很好的编译C和C++的工具。建议用win7系统安装vmware虚拟机,在vmware上安装redhat-server5.0以上的版本。
2.编程的编写。
用SecureCRT本地连到虚拟机里面的linux系统,
Last login: Mon Dec 30 11:13:30 2013
[root@localhost ~]#
[root@localhost ~]#
在此命令行下便可以完成C,C++,shell,pyhon,等等程序的编写。下面先来一个最简单的helloworld。
刚开始学的时候我们都是这样编写:
hello.c
#include<stdio.h>
main()
{
printf("helloworld!.\n");
}
main()
{
printf("helloworld!.\n");
}
2.1程序的执行。
gcc hello.c 生成一个a.out的文件,再执行./a.out就可以看到helloworld的执行结果。
也可以自己命名,生成自己想定义的可执行文件。gcc -o hello hello.c 会生成hello的一个可执行文件。
2.2生成汇编程序.s的一个文件。
gcc -S hello.c
hello.s
[root@localhost test]# more hello.s
.file "hello.c"
.section .rodata
.LC0:
.string "helloworld!."
.text
.globl main
.type main, @function
main:
.LFB2:
pushq %rbp
.LCFI0:
movq %rsp, %rbp
.LCFI1:
movl $.LC0, %edi
call puts
leave
ret
.LFE2:
.size main, .-main
.section .eh_frame,"a",@progbits
.Lframe1:
.long .LECIE1-.LSCIE1
.LSCIE1:
.long 0x0
.byte 0x1
.string "zR"
.uleb128 0x1
.sleb128 -8
.byte 0x10
.uleb128 0x1
.byte 0x3
.byte 0xc
.uleb128 0x7
.uleb128 0x8
.byte 0x90
.uleb128 0x1
.align 8
.LECIE1:
.LSFDE1:
.long .LEFDE1-.LASFDE1
.LASFDE1:
.long .LASFDE1-.Lframe1
.long .LFB2
.long .LFE2-.LFB2
.uleb128 0x0
.byte 0x4
.long .LCFI0-.LFB2
.byte 0xe
.uleb128 0x10
.byte 0x86
.uleb128 0x2
.byte 0x4
.long .LCFI1-.LCFI0
.byte 0xd
.uleb128 0x6
.align 8
.LEFDE1:
.ident "GCC: (GNU) 4.1.2 20080704 (Red Hat 4.1.2-50)"
.section .note.GNU-stack,"",@progbits
.file "hello.c"
.section .rodata
.LC0:
.string "helloworld!."
.text
.globl main
.type main, @function
main:
.LFB2:
pushq %rbp
.LCFI0:
movq %rsp, %rbp
.LCFI1:
movl $.LC0, %edi
call puts
leave
ret
.LFE2:
.size main, .-main
.section .eh_frame,"a",@progbits
.Lframe1:
.long .LECIE1-.LSCIE1
.LSCIE1:
.long 0x0
.byte 0x1
.string "zR"
.uleb128 0x1
.sleb128 -8
.byte 0x10
.uleb128 0x1
.byte 0x3
.byte 0xc
.uleb128 0x7
.uleb128 0x8
.byte 0x90
.uleb128 0x1
.align 8
.LECIE1:
.LSFDE1:
.long .LEFDE1-.LASFDE1
.LASFDE1:
.long .LASFDE1-.Lframe1
.long .LFB2
.long .LFE2-.LFB2
.uleb128 0x0
.byte 0x4
.long .LCFI0-.LFB2
.byte 0xe
.uleb128 0x10
.byte 0x86
.uleb128 0x2
.byte 0x4
.long .LCFI1-.LCFI0
.byte 0xd
.uleb128 0x6
.align 8
.LEFDE1:
.ident "GCC: (GNU) 4.1.2 20080704 (Red Hat 4.1.2-50)"
.section .note.GNU-stack,"",@progbits
2.3需要调试的话可以利用gdb这个调试工具。但是在执行的时候需要用gcc -g -o hello hello.c来执行,-g参数会把调试的一些参数写到执行文件中。
用gdb hello
[root@localhost test]# gdb hello
GNU gdb (GDB) Red Hat Enterprise Linux (7.0.1-32.el5)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /Agile/test/hello...done.
(gdb) ?
Undefined command: "". Try "help".
(gdb) help
List of classes of commands:
aliases -- Aliases of other commands
breakpoints -- Making program stop at certain points
data -- Examining data
files -- Specifying and examining files
internals -- Maintenance commands
obscure -- Obscure features
running -- Running the program
stack -- Examining the stack
status -- Status inquiries
support -- Support facilities
tracepoints -- Tracing of program execution without stopping the program
user-defined -- User-defined commands
Type "help" followed by a class name for a list of commands in that class.
Type "help all" for the list of all commands.
Type "help" followed by command name for full documentation.
Type "apropos word" to search for commands related to "word".
Command name abbreviations are allowed if unambiguous.
(gdb) list 列出原程序文件。
1 #include<stdio.h>
2 main()
3 {
4
5 printf("helloworld!.\n");
6
7 }
(gdb) run 执行
Starting program: /Agile/test/hello
helloworld!.
Program exited with code 015.
(gdb)
GNU gdb (GDB) Red Hat Enterprise Linux (7.0.1-32.el5)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /Agile/test/hello...done.
(gdb) ?
Undefined command: "". Try "help".
(gdb) help
List of classes of commands:
aliases -- Aliases of other commands
breakpoints -- Making program stop at certain points
data -- Examining data
files -- Specifying and examining files
internals -- Maintenance commands
obscure -- Obscure features
running -- Running the program
stack -- Examining the stack
status -- Status inquiries
support -- Support facilities
tracepoints -- Tracing of program execution without stopping the program
user-defined -- User-defined commands
Type "help" followed by a class name for a list of commands in that class.
Type "help all" for the list of all commands.
Type "help" followed by command name for full documentation.
Type "apropos word" to search for commands related to "word".
Command name abbreviations are allowed if unambiguous.
(gdb) list 列出原程序文件。
1 #include<stdio.h>
2 main()
3 {
4
5 printf("helloworld!.\n");
6
7 }
(gdb) run 执行
Starting program: /Agile/test/hello
helloworld!.
Program exited with code 015.
(gdb)
gdb的功能有很多,可以完成很多事情,比如断点调试,跟踪结果,等等。
以上就是最基本的gcc 下c语言的编程过程。
待续。。。。。。