• Linux C/C++ 编译集锦


    1. Linux C/C++ 编译集锦

    1.1. 常规编译

    ./configure
    make
    sudo make install
    
    • 1
    • 2
    • 3

    1.1.1. 修改安装目录

    1. 修改 configure 文件中 prefix 的值:

    vi/vim 打开 configure 文件, 然后找到 prefix 值, 修改未 prefix=你的安装目录, 然后保存退出, 再执行 ./configure & make & sudo make install 就可以, 不过该方法比较麻烦, 会容易改动到 configure 文件的其他的参数, 不建议使用。

    1. 执行 configure 文件时指定安装目录:
    ./configure --prefix=/home/user/zws/build
    
    • 1
    1. make install 指定 DESTDIR 参数:
    ./configure
    make 
    make install DESTDIR=/home/user/zws/build
    
    • 1
    • 2
    • 3

    1.2. gcc

    1.2.1. gcc 编译器的 std=c99 选项

    gcc 默认使用的是 C89 的标准,而 C89 的标准不支持在 for 中定义循环变量,而在 for 循环中需要定义循环变量的话,需要在 C99 标准中才支持,因此需要增加 -std=c99-std=gun99 参数才能编译通过。

    1.2.2. 编译 gcc

    Follow the instructions at https://gcc.gnu.org/wiki/InstallingGCC

    Specifically, don’t install ISL manually in some non-standard path, because GCC needs to find its shared libraries at run-time.

    The simplest solution is to use the download_prerequisites script to add the GMP, MPFR, MPC and ISL source code to the GCC source tree, which will cause GCC to build them for you automatically, and link to them statically.

    If it provides sufficiently recent versions, use your OS package management system to install the support libraries in standard system locations. For Debian-based systems, including Ubuntu, you should install the packages libgmp-dev, libmpfr-dev and libmpc-dev. For RPM-based systems, including Fedora and SUSE, you should install gmp-devel, mpfr-devel and libmpc-devel (or mpc-devel on SUSE) packages. The packages will install the libraries and headers in standard system directories so they can be found automatically when building GCC.

    Alternatively, after extracting the GCC source archive, simply run the ./contrib/download_prerequisites script in the GCC source directory. That will download the support libraries and create symlinks, causing them to be built automatically as part of the GCC build process. Set GRAPHITE_LOOP_OPT=no in the script if you want to build GCC without ISL, which is only needed for the optional Graphite loop optimizations.

    tar xzf gcc-4.6.2.tar.gz
    cd gcc-4.6.2
    ./contrib/download_prerequisites
    cd ..
    mkdir objdir
    cd objdir
    $PWD/../gcc-4.6.2/configure --prefix=$HOME/GCC-4.6.2 --enable-languages=c,c++,fortran,go
    make
    make install
    
    # option
    export PATH=$HOME/GCC-4.6.2/bin:$PATH
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    1.3. 问题

    1.3.1. Target requires the language dialect “CXX17” (with compiler extensions), but CMake does not know the compile flags to use to enable it

    需要:

    • CMAKE > 3.8
    • GCC/C++17 > 5.1.0

    Still the same "dialect “CXX17” error ?In my case, something else was needed to make it works:

    sudo ln -s /usr/local/bin/gcc /usr/local/bin/cc
    
    • 1

    i’ve setted -DCMAKE_C_COMPILER=/usr/local/bin/gcc and -DCMAKE_CXX_COMPILER=/usr/local/bin/g++ to cmake command options and had worked fine.

    1.3.2. 解决 “错误: 只允许在 C99 模式下使用‘for’循环初始化声明” 问题

    $ make CFLAGS=-std=c99
    # 或者
    $ export CFLAGS=-std=c99
    
    • 1
    • 2
    • 3

    1.3.3. Linux error while loading shared libraries: cannot open shared object file: No such file or directory

    sudo find / -name the_name_of_the_file.so
    echo $LD_LIBRARY_PATH
    
    LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/my_library/
    export LD_LIBRARY_PATH
    ./my_app
    
    sudo ldconfig
    
    sudo ldconfig /opt/intel/oneapi/mkl/2021.2.0/lib/intel64
    
    ln -s /lib/libpthread_rt.so /lib/libpthread_rt.so.1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    installed the -dev version of that package.

    sudo apt install libgconf2-dev
    
    • 1
  • 相关阅读:
    MySql的索引与算法-B+树索引
    Day 7.UDP编程、不同主机之间用网络进行通信
    web前端期末大作业 基于HTML+CSS+JavaScript程序员个人博客模板(web学生作业源码)
    23年云计算全国职业技能大赛-私有云
    【Java】HashMap 背诵版
    PCF8591学习笔记
    Spring Cloud Gateway现高风险漏洞,建议采取措施加强防护
    C++11 条件变量
    从零开始 Spring Cloud 15:多级缓存
    安装VMware
  • 原文地址:https://blog.csdn.net/wan212000/article/details/126001480