zoukankan      html  css  js  c++  java
  • 用gcc进行程序的编译

      在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
    ");
    }
    [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
    ");
        thanks_2();   #子程序
    }
    [root@localhost]# vi thanks_2.c
    [root@localhost]# cat thanks_2.c
    #include <stdio.h>
    void thanks_2(void)
    {
        printf("Thank you!
    ");
    }
    [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!

    (4)此外

    [root@localhost]#gcc -O hello.c -c  #会自动产生hello.o,并且进行优化
    [root@localhost]#gcc -o hello hello.c -Wall  #加入-Wall,程序的编译会变的较为严谨,警告信息会显示出来
  • 相关阅读:
    git知识点总结
    自动化进阶
    unittest单元测试框架
    自动化测试模型
    webdriver
    python文件处理
    uva 11077 置换
    poj 1066 Treasure Hunt
    poj 2661 Factstone Benchmark
    hdu 4180
  • 原文地址:https://www.cnblogs.com/tianxintian22/p/5584861.html
Copyright © 2011-2022 走看看