linux编译篇第一章

作者: 数据革命 | 来源:发表于2017-06-13 17:16 被阅读50次

    简介

    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'
    
    • 就表示我们软件已经编译安装完成需要开启服务,就可以用了

    相关文章

      网友评论

        本文标题:linux编译篇第一章

        本文链接:https://www.haomeiwen.com/subject/titjqxtx.html