Linux C++ 开发环境搭建

前言

工欲善其事必先利其器,想要学好 Linux C++ 编程,那么就要有对应环境,本文将带你一步步搭建开发环境。

准备工作

为了学习方便,在 win 下面安装虚拟机,然后安装 CentOs 操作系统。当然也可以使用 阿里云等云产品(但个人推荐本地搭建,因为阿里云远程连接经常掉线)。

  • 搭建 centos 开发环境: 前文介绍过,这里将不再赘述。
  • 安装常用工具
    作为一个合格的程序员我觉得所有的东西都应该标准化、文档化。所以我对我的文件进行规划:
    /home/package : 存放二进制安装包
    /home/software: 存放安装路径
    /home/workspace: 存放代码工作区

安装包路径:https://pan.baidu.com/s/1Vqy3J8In1z4AG5ZosK4k3w 提取码:atrs
将下载下来的文件解压放入 /home/package 中。
为了方便我写了一个执行脚本(带注释):

#!/bin/bash
# 更新环境变量
source /etc/profile

#编译加速选项
CPU=-j4  

#动态库路径
LD_CONF=/etc/ld.so.conf

#错误路径
ERROR_LOG=/home/error.log

#环境变量文件
ETC_PROFILE=/etc/profile

#安装包目录
PACKAGE_PATH=/home/package

#安装路径
SOFTWARE_PATH=/home/software

#获取系统版本
SYSTEM_VERSION=$(rpm -q centos-release | cut -d- -f3)

#创建错误日志文件
if [ ! -f "${ERROR_LOG}" ]; then

    touch ${ERROR_LOG}

    chmod 755 ${ERROR_LOG}

fi

#创建安装包目录 & 赋予权限
if [ ! -d "${PACKAGE_PATH}" ]; then

    mkdir ${PACKAGE_PATH}

    chmod 755 ${PACKAGE_PATH}

fi

#创建安装目录 & 赋予权限
if [ ! -d "${SOFTWARE_PATH}" ]; then

    mkdir ${SOFTWARE_PATH}

    chmod 755 ${SOFTWARE_PATH}

fi


#/etc/yum.repos.d  yum源的相关配置文件
if [ ! -f "/etc/yum.repos.d/epel.repo" ]; then

    if [ ${SYSTEM_VERSION} == "7" ]; then

        rpm -Uvh http://mirrors.ustc.edu.cn/epel/7/x86_64/Packages/e/epel-release-7-11.noarch.rpm

        rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7

        sed -i 'N;8a\priority=11' /etc/yum.repos.d/epel.repo

        yum makecache

    elif [ ${SYSTEM_VERSION} == "6" ]; then

        #安装epel源
        rpm -Uvh http://mirrors.ustc.edu.cn/epel/6/x86_64/Packages/e/epel-release-6-8.noarch.rpm

        #rpm -Uvh  http://mirrors.ustc.edu.cn/epel/6/i386/Packages/e/epel-release-6-8.noarch.rpm

        curl -Lks http://www.hop5.in/yum/el6/hop5.repo >/etc/yum.repos.d/hop5.repo

        rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6

        sed -i 'N;8a\priority=11' /etc/yum.repos.d/epel.repo

        yum makecache

    fi

fi

# 安装相关依赖
yum upgrade -y

yum install ibus* -y

yum install kcachegrind -y

yum install libwebp libwebp-devel uuid libuuid-devel -y

yum install libXau-devel ibX11-devel libXext-deve net-snmp-devel -y

yum install wget net-tools createrepo utp ntpdate openssh-server  man man-pages -y

yum groupinstall "X Window System" "Chinese Support" "GNOME DESKTOP" -y

yum install yum-priorities flex flex-devel clang clang-devel bison bison-devel ctags nc libdnet libdnet-devel  -y

yum install gcc gcc-c++ glibc-static glibc-devel centos-release-scl devtoolset-7-* docker-io redis libpcap-devel libpcap -y

yum install xorg-x11-server-Xvfb readline readline-devel binutils binutils-devel texinfo gdb gdb-gdbserver openldap-devel -y

yum install curl libcurl libcurl-devel boost boost-devel boost-doc openssl-devel libgcrypt-devel gtk2-devel tcl-devel tk-devel -y

rpm -ivh http://repo.mysql.com/mysql-community-release-el6-5.noarch.rpm && yum install mysql-community-server mysql-community-devel -y

yum install samba samba-swat samba-client zlib-devel bzip2 bzip2-devel minizip unzip minizip-deve make automake autoconf libconfig-deve libev-devel libevent-devel libxml2-devel -y

yum install xz bc vim tee tree swig cpan telnet libtool hmaccalc graphviz nss git git-svn git-cvs git-all git-gui git-email git-review git-tools pcre-devel libcurl-devel libssh2-devel -y

yum install kernel-headers kernel-devel kernel-tools kernel-tools-libs-devel valgrind valgrind-devel valgrind-openmpi nasm gperf gnutls mercurial pkgconfig freetype-devel docbook-utils-pdf -y



#cmake工具来帮助我们构建并维护项目代码。
if [ ! -d "${SOFTWARE_PATH}/cmake" ]; then

    cd ${PACKAGE_PATH}
  
  #解压缩
    tar -xvf cmake-3.14.1-Linux-x86_64.tar.gz
  
  #删除安装包
    rm -rf cmake-3.14.1-Linux-x86_64.tar.gz
  
    mv ${PACKAGE_PATH}/cmake-3.14.1-Linux-x86_64 ${SOFTWARE_PATH}/cmake
  
  #将安装路径下的可执行文件软连接到 /usr/local/bin/
    ln -sf ${SOFTWARE_PATH}/cmake/bin/* /usr/local/bin/

fi

# gcc 相关依赖
if [ ! -d "${SOFTWARE_PATH}/gcc/gmp" ]; then
 
    if [ ! -d "${PACKAGE_PATH}/gmp" ]; then

        cd ${PACKAGE_PATH}

        tar -xvf gmp-6.1.2.tar.xz

        rm -rf gmp-6.1.2.tar.xz

        mv ${PACKAGE_PATH}/gmp-6.1.2 ${PACKAGE_PATH}/gmp

    fi

    if [ -d "${PACKAGE_PATH}/gmp" ]; then

        cd ${PACKAGE_PATH}/gmp
    
    #编译选项  --prefix:设置安装位置
        ./configure \
            --prefix=${SOFTWARE_PATH}/gcc/gmp
    
    #make 编译  make install 安装
        make ${CPU} && make install

    fi

    if [ ! -d "${SOFTWARE_PATH}/gcc/gmp" ]; then

        echo gcc/gmp >>${ERROR_LOG}

        exit 1

    else

        echo ${SOFTWARE_PATH}/gcc/gmp/lib >>${LD_CONF}

        ldconfig

    fi

fi


if [ ! -d "${SOFTWARE_PATH}/gcc/mpfr" ]; then

    if [ ! -d "${PACKAGE_PATH}/mpfr" ]; then

        cd ${PACKAGE_PATH}

        tar -xvf mpfr-4.0.1.tar.gz

        rm -rf mpfr-4.0.1.tar.gz

        mv ${PACKAGE_PATH}/mpfr-4.0.1 ${PACKAGE_PATH}/mpfr

    fi

    if [ -d "${PACKAGE_PATH}/mpfr" ]; then

        cd ${PACKAGE_PATH}/mpfr

        ./configure \
            --prefix=${SOFTWARE_PATH}/gcc/mpfr \
            --with-gmp=${SOFTWARE_PATH}/gcc/gmp \
            --enable-thread-safe \
            --enable-warnings

        make ${CPU} && make install

    fi

    if [ ! -d "${SOFTWARE_PATH}/gcc/mpfr" ]; then

        echo gcc/mpfr >>${ERROR_LOG}

        exit 1

    else

        echo ${SOFTWARE_PATH}/gcc/mpfr/lib >>${LD_CONF}

        ldconfig

    fi

fi

#http://mirrors.ustc.edu.cn/gnu/mpc/mpc-1.1.0.tar.gz
if [ ! -d "${SOFTWARE_PATH}/gcc/mpc" ]; then

    if [ ! -d "${PACKAGE_PATH}/mpc" ]; then

        cd ${PACKAGE_PATH}

        tar -xvf mpc-1.1.0.tar.gz

        rm -rf mpc-1.1.0.tar.gz

        mv ${PACKAGE_PATH}/mpc-1.1.0 ${PACKAGE_PATH}/mpc

    fi

    if [ -d "${PACKAGE_PATH}/mpc" ]; then

        cd ${PACKAGE_PATH}/mpc

        ./configure \
            --prefix=${SOFTWARE_PATH}/gcc/mpc \
            --with-gmp=${SOFTWARE_PATH}/gcc/gmp \
            --with-mpfr=${SOFTWARE_PATH}/gcc/mpfr

        make ${CPU} && make install

    fi

    if [ ! -d "${SOFTWARE_PATH}/gcc/mpc" ]; then

        echo gcc/mpc >>${ERROR_LOG}

        exit 1

    else

        echo ${SOFTWARE_PATH}/gcc/mpc/lib >>${LD_CONF}

        ldconfig

    fi

fi

#http://mirrors.ustc.edu.cn/gnu/gcc/gcc-8.1.0/gcc-8.1.0.tar.gz
#安装gcc
if [ ! -d "${SOFTWARE_PATH}/gcc/gcc" ]; then

    if [ ! -d "${PACKAGE_PATH}/gcc" ]; then

        cd ${PACKAGE_PATH}

        tar -xvf gcc-8.1.0.tar.gz

        rm -rf gcc-8.1.0.tar.gz

        mv ${PACKAGE_PATH}/gcc-8.1.0 ${PACKAGE_PATH}/gcc

    fi

    if [ -d "${PACKAGE_PATH}/gcc" ]; then

        cd ${PACKAGE_PATH}/gcc

        CFLAGS=-O2 \
            CXXFLAGS=-O2 \
            CPPFLAGS=-O2 \
            ./configure \
            CFLAGS=-O2 \
            CXXFLAGS=-O2 \
            CPPFLAGS=-O2 \
            --prefix=${SOFTWARE_PATH}/gcc/gcc \
            --mandir=${SOFTWARE_PATH}/gcc/gcc/share/man \
            --infodir=${SOFTWARE_PATH}/gcc/gcc/share/info \
            --with-bugurl=http://bugzilla.redhat.com/bugzilla \
            --enable-bootstrap \
            --enable-shared \
            --enable-threads=posix \
            --enable-checking=release \
            --with-system-zlib \
            --enable-__cxa_atexit \
            --disable-libunwind-exceptions \
            --enable-gnu-unique-object \
            --enable-linker-build-id \
            --with-linker-hash-style=gnu \
            --enable-languages=c,c++ \
            --enable-plugin \
            --enable-initfini-array \
            --disable-libgcj \
            --disable-dssi \
            --enable-libgcj-multifile \
            --with-ppl \
            --with-cloog \
            --enable-gnu-indirect-function \
            --with-tune=generic \
            --with-arch_32=x86-64 \
            --build=x86_64-redhat-linux \
            --with-diagnostics-color=auto \
            --disable-multilib \
            --program-suffix=8.1.0 \
            --with-gmp=${SOFTWARE_PATH}/gcc/gmp \
            --with-mpfr=${SOFTWARE_PATH}/gcc/mpfr \
            --with-mpc=${SOFTWARE_PATH}/gcc/mpc \
            CFLAGS=-O2 \
            CXXFLAGS=-O2 \
            CPPFLAGS=-O2

        make ${CPU} && make install

    fi

    if [ ! -d "${SOFTWARE_PATH}/gcc/gcc" ]; then

        echo gcc/gcc >>${ERROR_LOG}

        exit 1

    else

        mv /usr/bin/gcc /usr/bin/gcc4.8
        mv /usr/bin/g++ /usr/bin/g++4.8
        mv /usr/bin/c++ /usr/bin/c++4.8
        mv /usr/bin/cpp /usr/bin/cpp4.8
        #  -sf  s  软连接   f  强制执行
        ln -sf ${SOFTWARE_PATH}/gcc/gcc/bin/cpp8.1.0 /usr/bin/cpp
        ln -sf ${SOFTWARE_PATH}/gcc/gcc/bin/c++8.1.0 /usr/bin/c++
        ln -sf ${SOFTWARE_PATH}/gcc/gcc/bin/g++8.1.0 /usr/bin/g++
        ln -sf ${SOFTWARE_PATH}/gcc/gcc/bin/gcc8.1.0 /usr/bin/gcc

        echo ${SOFTWARE_PATH}/gcc/gcc/lib64 >>${LD_CONF}

        ldconfig

    fi

fi

#http://mirrors.ustc.edu.cn/gnu/gdb/gdb-8.1.tar.gz
#安装断点调试工具
if [ ! -d "${SOFTWARE_PATH}/gdb" ]; then

    if [ ! -d "${PACKAGE_PATH}/gdb" ]; then

        cd ${PACKAGE_PATH}

        tar -xvf gdb-8.1.tar.gz

        rm -rf gdb-8.1.tar.gz

        mv ${PACKAGE_PATH}/gdb-8.1 ${PACKAGE_PATH}/gdb

    fi

    if [ -d "${PACKAGE_PATH}/gdb" ]; then

        cd ${PACKAGE_PATH}/gdb

        ./configure \
            --prefix=${SOFTWARE_PATH}/gdb \
            --program-suffix=8.1 \
            --enable-libada \
            --enable-libssp \
            --enable-lto \
            --enable-vtable-verify \
            --enable-host-shared \
            --with-gmp=${SOFTWARE_PATH}/gcc/gmp \
            --with-mpc=${SOFTWARE_PATH}/gcc/mpc \
            --with-mpfr=${SOFTWARE_PATH}/gcc/mpfr \
            CC=${SOFTWARE_PATH}/gcc/gcc/bin/gcc8.1.0 \
            CXX=${SOFTWARE_PATH}/gcc/gcc/bin/g++8.1.0

        make ${CPU} && make install

    fi

    if [ ! -d "${SOFTWARE_PATH}/gdb" ]; then

        echo gdb >>${ERROR_LOG}

        exit 1

    else

        ln -sf ${SOFTWARE_PATH}/gdb/bin/* /usr/local/bin/

        echo ${SOFTWARE_PATH}/gdb/lib >>${LD_CONF}

        ldconfig

    fi

fi

安装

新建一个文件××.sh,将上述代码复制,然后 chmod 755 ××.sh。执行 ./××.sh

那么恭喜你,你的环境已经安装好了

测试

  • 执行 gcc --version
    在这里插入图片描述
  • 执行 gdb --version
    Linux C++ 开发环境搭建_第1张图片
  • 执行 cmake --version
    在这里插入图片描述
    如上显示,那么说明安装成果了。

原文链接:Linux C++ 开发环境搭建

你可能感兴趣的:(C/C++,c++,Linux)