歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 用GCC進行程序的編譯

用GCC進行程序的編譯

日期:2017/3/1 9:14:53   编辑:Linux編程

  在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 的詳細介紹:請點這裡

Copyright © Linux教程網 All Rights Reserved