linux編譯篇第一章

簡介

GCC

  • GCC 的意思也只是 GNU C Compiler 而已序目。經(jīng)過了這么多年的發(fā)展坑律,GCC 已經(jīng)不僅僅能支持 C 語言岩梳;它現(xiàn)在還支持 Ada 語言、C++ 語言晃择、Java 語言冀值、Objective C 語言、Pascal 語言宫屠、COBOL語言列疗,以及支持函數(shù)式編程和邏輯編程的 Mercury 語言,等等浪蹂。而 GCC 也不再單只是 GNU C 語言編譯器的意思了抵栈,而是變成了 GNU Compiler Collection 也即是 GNU 編譯器家族的意思了告材。另一方面,說到 GCC 對于操作系統(tǒng)平臺及硬件平臺支持竭讳,概括起來就是一句話:無所不在

make

  • 無論是在Linux還是在UNIX環(huán)境中创葡,make都是一個非常重要的編譯工具浙踢。無論是自己進行項目開發(fā)還是安裝應(yīng)用軟件绢慢,都需要使用make工具。利用make工具洛波,可以將大型的開發(fā)項目分解成為多個更易于管理的模塊胰舆,對于一個包括幾百個源文件的應(yīng)用程序而言,使用make工具和makefile文件就可以清晰地理順各個源文件之間的關(guān)系蹬挤。而且如此多的源文件缚窿,如果每次都要輸入gcc命令進行編譯的話,對程序員來說是很難忍受的焰扳。make工具可以自動完成編譯工作倦零,并且只對程序員在上次編譯后修改過的部分進行編譯。因此吨悍,有效地利用make工具可以大大提高項目開發(fā)的效率

程序包編譯

  • 上圖就是編譯的過程
  • 程序包編譯安裝:
  • Application-VERSION-release.src.rpm --> 安裝后
  • 使用 用rpmbuild 命令制作成二進制格式的rpm 包,而后再安裝
  • 源代碼--> 預處理--> 編譯--> 匯編--> 鏈接--> 執(zhí)行
  • 源代碼組織格式:
  • 多文件:文件中的代碼之間,很可能存在跨文件依賴關(guān)系
  • C 询微、C++ :make 項目管理器
  • configure --> Makefile.in --> makefile
  • java: mave
  • 下面開始安裝包組里面集成了make和GCC

安裝包組

  • 編譯C 源代碼:
    • 準備:提供開發(fā)工具及開發(fā)環(huán)境
    • 開發(fā)工具:make, gcc等 等
    • 開發(fā)環(huán)境:開發(fā)庫株依,頭文件
    • glibc :標準庫
    • 實現(xiàn):通過“包組”提供開發(fā)組件
    • Development Tools
    • Server Platform Development
  • Development Tools
    • Development Tools里面有幾十個,大家主要使用其中的gcc, make等編譯工具
  • 首先我們要測試是否配好了yum 源躏仇,可以用yum list|grep 來測試
[root@localhost ~]# yum list|grep httpd
httpd.x86_64                               2.4.6-45.el7.centos         bash     
httpd-devel.x86_64                         2.4.6-45.el7.centos         bash     
httpd-manual.noarch                        2.4.6-45.el7.centos         bash     
httpd-tools.x86_64                         2.4.6-45.el7.centos         bash     
libmicrohttpd.i686                         0.9.33-2.el7                bash     
libmicrohttpd.x86_64                       0.9.33-2.el7                bash     
libmicrohttpd-devel.i686                   0.9.33-2.el7                bash     
libmicrohttpd-devel.x86_64                 0.9.33-2.el7                bash     
libmicrohttpd-doc.noarch                   0.9.33-2.el7                bash 
  • yum源配好了以后我們來查找包組可以用yum grouplist來查詢包組
[root@localhost ~]# yum grouplist
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Available Environment Groups:
   Minimal Install
   Compute Node
   Infrastructure Server
   File and Print Server
   Basic Web Server
   Virtualization Host
   Server with GUI
   GNOME Desktop
   KDE Plasma Workspaces
   Development and Creative Workstation
Installed Groups:
   Development Tools               《《《《《《《《這里就是我們要安裝的包組
Available Groups:
   Compatibility Libraries
   Console Internet Tools
   Graphical Administration Tools
   Legacy UNIX Compatibility
   Scientific Support
   Security Tools
   Smart Card Support
   System Administration Tools
   System Management
Done
  • 那我們來開始安裝Development Tools包組恋脚,可以用命令yum install Development Tools來安裝包組
[root@localhost yum.repos.d]# yum clean all                    
Loaded plugins: fastestmirror, langpacks
Cleaning repos: bash epel
Cleaning up everything
Cleaning up list of fastest mirrors
[root@localhost yum.repos.d]# yum install "Development tools"
Loaded plugins: fastestmirror, langpacks
bash                                                                                                         | 4.3 kB  00:00:00     
epel                                                                                                         | 4.3 kB  00:00:00     
(1/6): bash/updateinfo                                                                                       | 807 kB  00:00:00     
(2/6): bash/primary_db                                                                                       | 4.7 MB  00:00:04     
(3/6): epel/group_gz                                                                                         | 170 kB  00:00:00     
(4/6): epel/updateinfo                                                                                       | 807 kB  00:00:00     
(5/6): epel/primary_db                                                                                       | 4.7 MB  00:00:04     
(6/6): bash/group_gz  
  • 安裝包組以后開始找到源碼包可以上傳也可以去包里面下載我這里用httpd-2.4.25.tar.bz2源碼包做演示
  • 首先解壓httpd-2.4.25.tar.bz2包,可以用tar -xf命令來解壓
[root@localhost /]# tar -vxf  httpd-2.4.25.tar.bz2 
httpd-2.4.25/support/suexec.h
httpd-2.4.25/support/win32/
httpd-2.4.25/support/win32/apache_header.bmp
httpd-2.4.25/support/win32/ApacheMonitor.c
httpd-2.4.25/support/win32/ApacheMonitor.dep
httpd-2.4.25/support/win32/ApacheMonitor.dsp
httpd-2.4.25/support/win32/ApacheMonitor.h
httpd-2.4.25/support/win32/ApacheMonitor.ico
httpd-2.4.25/support/win32/ApacheMonitor.mak
httpd-2.4.25/support/win32/ApacheMonitor.manifest
httpd-2.4.25/support/win32/ApacheMonitor.rc
httpd-2.4.25/support/win32/aprun.ico
  • 下面httpd-2.4.25就是我們解壓的文件目錄用cd切換到httpd-2.4.25目錄里面
[root@localhost /]# ls
bin   dev  home          httpd-2.4.25.tar.bz2  lib64  mnt  proc  run   srv  tmp  var
boot  etc  httpd-2.4.25  lib                   media  opt  root  sbin  sys  usr
  • 我們來看下httpd-2.4.25目錄里面有什么焰手,想就是httpd-2.4.25所有文件
[root@localhost httpd-2.4.25]# ls
ABOUT_APACHE     build           config.layout  httpd.dsp       LAYOUT        Makefile.win   README.cmake      test
acinclude.m4     BuildAll.dsp    configure      httpd.mak       libhttpd.dep  modules        README.platforms  VERSIONING
Apache-apr2.dsw  BuildBin.dsp    configure.in   httpd.spec      libhttpd.dsp  NOTICE         ROADMAP
Apache.dsw       buildconf       docs           include         libhttpd.mak  NWGNUmakefile  server
apache_probes.d  CHANGES         emacs-style    INSTALL         LICENSE       os             srclib
ap.d             CMakeLists.txt  httpd.dep      InstallBin.dsp  Makefile.in   README         support
  • 下面我們就開始編譯測試安裝了可以用configure來測試用--prefix=/PATH: 指定默認安裝位置, 默認為/usr/local/
[root@localhost httpd-2.4.25]# ./configure  --prefix=/app
  • 編譯開始糟描,編譯過程中可能會遇到很多問題下面請看
[root@localhost httpd-2.4.25]# ./configure  --prefix=/app
checking for chosen layout... Apache
checking for working mkdir -p... yes
checking for grep that handles long lines and -e... /usr/bin/grep
checking for egrep... /usr/bin/grep -E
checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
checking target system type... x86_64-unknown-linux-gnu
configure: 
configure: Configuring Apache Portable Runtime library...
configure: 
checking for APR... no
configure: error: APR not found.  Please read the documentation.
  • 遇到一個錯誤說要APR包,我們可以用yum search apr來查包
[root@localhost httpd-2.4.25]# yum  search apr
apr-devel.i686 : APR library development kit   《《《《《《《這個就是我們的APR包
apr-devel.x86_64 : APR library development kit
apr-util-devel.i686 : APR utility library development kit
apr-util-devel.x86_64 : APR utility library development kit
apr-util-ldap.x86_64 : APR utility library LDAP support
apr-util-mysql.x86_64 : APR utility library MySQL DBD driver
apr-util-nss.x86_64 : APR utility library NSS crytpo support
apr-util-odbc.x86_64 : APR utility library ODBC DBD driver
apr-util-openssl.x86_64 : APR utility library OpenSSL crytpo support
apr-util-pgsql.x86_64 : APR utility library PostgreSQL DBD driver
apr-util-sqlite.x86_64 : APR utility library SQLite DBD driver
  • 我們只需要安裝包即可用命令yum install apr-devel來安裝

  • 安裝完畢以后又來編譯測試看他還報錯不 ./configure --prefix=/app

  • 又出現(xiàn)了錯誤error: APR-util not found. Please read the documentation.

[root@localhost httpd-2.4.25]# ./configure  --prefix=/app
checking for chosen layout... Apache
checking for working mkdir -p... yes
checking for grep that handles long lines and -e... /usr/bin/grep
checking for egrep... /usr/bin/grep -E
checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
checking target system type... x86_64-unknown-linux-gnu
configure: 
configure: Configuring Apache Portable Runtime library...
configure: 
checking for APR... yes
  setting CC to "gcc"
  setting CPP to "gcc -E"
  setting CFLAGS to "  -pthread"
  setting CPPFLAGS to " -DLINUX -D_REENTRANT -D_GNU_SOURCE"
  setting LDFLAGS to " "
configure: 
configure: Configuring Apache Portable Runtime Utility library...
configure: 
checking for APR-util... no
configure: error: APR-util not found.  Please read the documentation.
  • 我們繼續(xù)找APR-util包一樣的命令yum search APR-util
[root@localhost httpd-2.4.25]# yum  search APR-util
apr-util.i686 : Apache Portable Runtime Utility library
apr-util.x86_64 : Apache Portable Runtime Utility library
apr-util-devel.i686 : APR utility library development kit      
apr-util-devel.x86_64 : APR utility library development kit 《我的APR-util包
apr-util-ldap.x86_64 : APR utility library LDAP support
apr-util-mysql.x86_64 : APR utility library MySQL DBD driver
apr-util-nss.x86_64 : APR utility library NSS crytpo support
apr-util-odbc.x86_64 : APR utility library ODBC DBD driver
apr-util-openssl.x86_64 : APR utility library OpenSSL crytpo support
apr-util-pgsql.x86_64 : APR utility library PostgreSQL DBD driver
apr-util-sqlite.x86_64 : APR utility library SQLite DBD driver
  • 安裝完畢以后在用yum search apr來查包
  • 出現(xiàn)pcre-config 錯誤
configure: error: pcre-config for libpcre not found. PCRE is required and available from http://pcre.org/

-在來找pcre-config包,辦法一樣

[root@localhost httpd-2.4.25]# yum  search pcre
pcre-devel.i686 : Development files for pcre  《《這個包
pcre-devel.x86_64 : Development files for pcre
pcre-static.i686 : Static library for pcre
pcre-static.x86_64 : Static library for pcre
pcre-tools.x86_64 : Auxiliary utilities for pcre
pcre.i686 : Perl-compatible regular expression library
pcre.x86_64 : Perl-compatible regular expression library
  • 安裝完畢以后在查包书妻,不過我這里已經(jīng)測試編譯完畢出現(xiàn)下面畫面
Server Version: 2.4.25
Install prefix: /app
C compiler:     gcc -std=gnu99
CFLAGS:           -pthread
LDFLAGS:         
LIBS:           
CPPFLAGS:        -DLINUX -D_REENTRANT -D_GNU_SOURCE
C preprocessor: gcc -E
  • 那我們開始編譯船响,上面只是測試編譯,現(xiàn)在開始正式編譯
  • 用到的工具是make編譯必須在解壓的路徑才有效果切記切記
  • 開始編譯:出現(xiàn)下面畫面就表示
[root@localhost httpd-2.4.25]# make
  • 出現(xiàn)下面畫面表示編譯完成
 -avoid-version  mod_rewrite.lo 
make[4]: Leaving directory `/httpd-2.4.25/modules/mappers'
make[3]: Leaving directory `/httpd-2.4.25/modules/mappers'
make[2]: Leaving directory `/httpd-2.4.25/modules'
make[2]: Entering directory `/httpd-2.4.25/support'
make[2]: Leaving directory `/httpd-2.4.25/support'

make[1]: Leaving directory `/httpd-2.4.25'
  • 那我們開始安裝可以用命令make install來安裝
[root@localhost httpd-2.4.25]# make install
  • 出現(xiàn)下面畫面就表示安裝完成
Installing configuration files
mkdir /app/conf
mkdir /app/conf/extra
mkdir /app/conf/original
mkdir /app/conf/original/extra
Installing HTML documents
mkdir /app/htdocs
Installing error documents
mkdir /app/error
Installing icons
mkdir /app/icons
mkdir /app/logs
Installing CGIs
mkdir /app/cgi-bin
Installing header files
mkdir /app/include
Installing build system files
mkdir /app/build
Installing man pages and online manual
mkdir /app/man
mkdir /app/man/man1
mkdir /app/man/man8
mkdir /app/manual
make[1]: Leaving directory `/httpd-2.4.25'
  • 就表示我們軟件已經(jīng)編譯安裝完成需要開啟服務(wù)驻子,就可以用了
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末灿意,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子崇呵,更是在濱河造成了極大的恐慌缤剧,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,826評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件域慷,死亡現(xiàn)場離奇詭異荒辕,居然都是意外死亡汗销,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,968評論 3 395
  • 文/潘曉璐 我一進店門抵窒,熙熙樓的掌柜王于貴愁眉苦臉地迎上來弛针,“玉大人,你說我怎么就攤上這事李皇∠髯拢” “怎么了?”我有些...
    開封第一講書人閱讀 164,234評論 0 354
  • 文/不壞的土叔 我叫張陵掉房,是天一觀的道長茧跋。 經(jīng)常有香客問我,道長卓囚,這世上最難降的妖魔是什么瘾杭? 我笑而不...
    開封第一講書人閱讀 58,562評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮哪亿,結(jié)果婚禮上粥烁,老公的妹妹穿的比我還像新娘。我一直安慰自己蝇棉,他們只是感情好讨阻,可當我...
    茶點故事閱讀 67,611評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著银萍,像睡著了一般变勇。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上贴唇,一...
    開封第一講書人閱讀 51,482評論 1 302
  • 那天搀绣,我揣著相機與錄音,去河邊找鬼戳气。 笑死链患,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的瓶您。 我是一名探鬼主播麻捻,決...
    沈念sama閱讀 40,271評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼呀袱!你這毒婦竟也來了贸毕?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,166評論 0 276
  • 序言:老撾萬榮一對情侶失蹤夜赵,失蹤者是張志新(化名)和其女友劉穎明棍,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體寇僧,經(jīng)...
    沈念sama閱讀 45,608評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡摊腋,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,814評論 3 336
  • 正文 我和宋清朗相戀三年沸版,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片兴蒸。...
    茶點故事閱讀 39,926評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡视粮,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出橙凳,到底是詐尸還是另有隱情蕾殴,我是刑警寧澤,帶...
    沈念sama閱讀 35,644評論 5 346
  • 正文 年R本政府宣布痕惋,位于F島的核電站区宇,受9級特大地震影響娃殖,放射性物質(zhì)發(fā)生泄漏值戳。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,249評論 3 329
  • 文/蒙蒙 一炉爆、第九天 我趴在偏房一處隱蔽的房頂上張望堕虹。 院中可真熱鬧,春花似錦芬首、人聲如沸赴捞。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,866評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽赦政。三九已至,卻和暖如春耀怜,著一層夾襖步出監(jiān)牢的瞬間恢着,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,991評論 1 269
  • 我被黑心中介騙來泰國打工财破, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留掰派,地道東北人。 一個月前我還...
    沈念sama閱讀 48,063評論 3 370
  • 正文 我出身青樓左痢,卻偏偏與公主長得像靡羡,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子俊性,可洞房花燭夜當晚...
    茶點故事閱讀 44,871評論 2 354

推薦閱讀更多精彩內(nèi)容