linux编译篇第一章

简介

GCC

  • GCC 的意思也只是 GNU C Compiler 而已。经过了这么多年的发展,GCC 已经不仅仅能支持 C 语言;它现在还支持 Ada 语言、C++ 语言、Java 语言、Objective C 语言、Pascal 语言、COBOL语言,以及支持函数式编程和逻辑编程的 Mercury 语言,等等。而 GCC 也不再单只是 GNU C 语言编译器的意思了,而是变成了 GNU Compiler Collection 也即是 GNU 编译器家族的意思了。另一方面,说到 GCC 对于操作系统平台及硬件平台支持,概括起来就是一句话:无所不在

make

  • 无论是在Linux还是在UNIX环境中,make都是一个非常重要的编译工具。无论是自己进行项目开发还是安装应用软件,都需要使用make工具。利用make工具,可以将大型的开发项目分解成为多个更易于管理的模块,对于一个包括几百个源文件的应用程序而言,使用make工具和makefile文件就可以清晰地理顺各个源文件之间的关系。而且如此多的源文件,如果每次都要输入gcc命令进行编译的话,对程序员来说是很难忍受的。make工具可以自动完成编译工作,并且只对程序员在上次编译后修改过的部分进行编译。因此,有效地利用make工具可以大大提高项目开发的效率

程序包编译

  • 上图就是编译的过程
  • 程序包编译安装:
  • Application-VERSION-release.src.rpm --> 安装后
  • 使用 用rpmbuild 命令制作成二进制格式的rpm 包,而后再安装
  • 源代码--> 预处理--> 编译--> 汇编--> 链接--> 执行
  • 源代码组织格式:
  • 多文件:文件中的代码之间,很可能存在跨文件依赖关系
  • C 、C++ :make 项目管理器
  • configure --> Makefile.in --> makefile
  • java: mave
  • 下面开始安装包组里面集成了make和GCC

安装包组

  • 编译C 源代码:
    • 准备:提供开发工具及开发环境
    • 开发工具:make, gcc等 等
    • 开发环境:开发库,头文件
    • glibc :标准库
    • 实现:通过“包组”提供开发组件
    • 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

  • 又出现了错误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.
  • 我们继续找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来查包
  • 出现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
  • 安装完毕以后在查包,不过我这里已经测试编译完毕出现下面画面
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
  • 那我们开始编译,上面只是测试编译,现在开始正式编译
  • 用到的工具是make编译必须在解压的路径才有效果切记切记
  • 开始编译:出现下面画面就表示
[root@localhost httpd-2.4.25]# make
  • 出现下面画面表示编译完成
 -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
  • 出现下面画面就表示安装完成
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'
  • 就表示我们软件已经编译安装完成需要开启服务,就可以用了
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,835评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,598评论 1 295
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,569评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,159评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,533评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,710评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,923评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,674评论 0 203
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,421评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,622评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,115评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,428评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,114评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,097评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,875评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,753评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,649评论 2 271

推荐阅读更多精彩内容