歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> 學習Linux >> 用gcc進行程序的編譯,gcc進行程序編譯

用gcc進行程序的編譯,gcc進行程序編譯

日期:2017/3/6 9:39:56   编辑:學習Linux

用gcc進行程序的編譯,gcc進行程序編譯


用gcc進行程序的編譯,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\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!

(4)此外

[root@localhost]#gcc -O hello.c -c  #會自動產生hello.o,並且進行優化
[root@localhost]#gcc -o hello hello.c -Wall  #加入-Wall,程序的編譯會變的較為嚴謹,警告信息會顯示出來

http://xxxxxx/Linuxjc/1134427.html TechArticle

Copyright © Linux教程網 All Rights Reserved