手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

用GCC进行程序的编译

[日期:2016-06-14] 来源:Linux社区  作者:tianxintian22 [字体: ]

  在Linux系统上,一个档案能不能被执行看的是有没有可执行的那个权限(x),不过,Linux系统上真正认识的可执行文件其实是二进制文件(binary program),例如/usr/bin/passwd 这些档案就是二进制程序代码。

  怎么产生一个可执行的二进制程序呢?首先写程序,用字处理器写完的程序即源代码,这个源代码就是一般的纯文本文档。在完成源代码的编写后,再来就是将程序代码编译成操作系统看得懂的binary program。编译需要编译程序来动作,经过编译程序的编译与连结之后,就可以产生一个可执行的二进制程序。举例来说,Linux上最标准的程序语言是c,我们用c来写源代码,用Linux上标准的C语言编译程序gcc来编译,然后生成可执行的binary program。

  有时候我们会在程序中引用其他外部子程序,或者利用其他软件提供的函数功能,我们就必须在编译的过程中,将函式库加进去,这样,编译程序可以将所有的程序代码与函式库作一个连结(Link)以产生正确的执行档(可执行binary program档案)。
•make和configure

•Tarball

  Tarball档案,其实就是将软件所有的原始代码档案先以tar打包,然后再以压缩技术来压缩,最常见的是gzip,所以tarball档案的扩展名是*.tar.gz或者*tgz。由于bzip2的压缩效率更佳,因此裆名也会变成*.tar.bz2。
•打印hello world

(1)直接以gcc编译原始码

 

[root@localhost]# vi hello.c
[root@localhost Documents]# cat hello.c
#include <stdio.h>
int main(void){
printf("Hello World\n");
}
[root@localhost]# gcc hello.c
[root@localhost]# ll
total 12
-rwxr-xr-x. 1 root root 4643 Jun 14 00:55 a.out  #编译成功的可执行binary program
-rw-r--r--. 1 root root  67 Jun 14 00:55 hello.c
[root@localhost]# ./a.out #执行文档
Hello World

 

(2)产生目标文件来进行其他动作,而且执行的档名也不用预设的a.out

 

[root@localhost]# gcc -c hello.c
[root@localhost]# ll hello*
-rw-r--r--. 1 root root  67 Jun 14 00:55 hello.c
-rw-r--r--. 1 root root 852 Jun 14 01:00 hello.o  #产生的目标文件
[root@localhost]# gcc -o hello hello.o
[root@localhost]# ll
total 16
-rwxr-xr-x. 1 root root 4643 Jun 14 01:00 hello  #可执行文件
-rw-r--r--. 1 root root  67 Jun 14 00:55 hello.c
-rw-r--r--. 1 root root  852 Jun 14 01:00 hello.o
[root@localhost Documents]# ./hello
Hello World

 

(3)子程序的编译

 

[root@localhost]# vi thanks.c
[root@localhost]# cat thanks.c
#include <stdio.h>
int main(void)
{
    printf("Hello World\n");
    thanks_2();  #子程序
}
[root@localhost]# vi thanks_2.c
[root@localhost]# cat thanks_2.c
#include <stdio.h>
void thanks_2(void)
{
    printf("Thank you!\n");
}
[root@localhost]# ll thanks*
-rw-r--r--. 1 root root  71 Jun 14 01:05 thanks_2.c
-rw-r--r--. 1 root root  83 Jun 14 01:03 thanks.c
[root@localhost]# gcc -c thanks.c thanks_2.c
[root@localhost]# ll thanks*
-rw-r--r--. 1 root root  71 Jun 14 01:05 thanks_2.c
-rw-r--r--. 1 root root 856 Jun 14 01:05 thanks_2.o
-rw-r--r--. 1 root root  83 Jun 14 01:03 thanks.c
-rw-r--r--. 1 root root 892 Jun 14 01:05 thanks.o
[root@localhost]# gcc -o thanks thanks.o thanks_2.o
[root@localhost]# ll thanks*
-rwxr-xr-x. 1 root root 4740 Jun 14 01:06 thanks
-rw-r--r--. 1 root root  71 Jun 14 01:05 thanks_2.c
-rw-r--r--. 1 root root  856 Jun 14 01:05 thanks_2.o
-rw-r--r--. 1 root root  83 Jun 14 01:03 thanks.c
-rw-r--r--. 1 root root  892 Jun 14 01:05 thanks.o
[root@localhost]# ./thanks
Hello World
Thank you!

Linux升级GCC 4.8.1清晰简明教程(Ubuntu 12.04 64位版为例)  http://www.linuxidc.com/Linux/2014-04/99583.htm 

Ubuntu 14.04 LST安装GCC 4.1.2  http://www.linuxidc.com/Linux/2016-06/132040.htm

Ubuntu下Vim+GCC+GDB安装及使用 http://www.linuxidc.com/Linux/2013-01/78159.htm 

Ubuntu下两个GCC版本切换 http://www.linuxidc.com/Linux/2012-10/72284.htm 

CentOS6.5升级手动安装GCC4.8.2  http://www.linuxidc.com/Linux/2015-01/112595.htm 

GCC 的详细介绍请点这里

本文永久更新链接地址http://www.linuxidc.com/Linux/2016-06/132318.htm

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款