CentOS 7 构造GCC 4.8.2 32位编译环境

来源:互联网 发布:vb连接mysql实例 编辑:程序博客网 时间:2024/05/20 09:26

centos 7 构造gcc 32位编译环境


1引言

1.1背景
学习新的 C++ 2011和C11标准。

1.2使用软件
CentOS 7(Linux version 3.10.0-123.el7.x86_64)
gcc version 4.8.2 20140120 (Red Hat 4.8.2-16)

1.3本次配置
32位gcc 开发环境。
因为 RHEL 7(红帽企业版 linux 7)已经不再提供32位版本,短期内 CentOS 7也不会提供32位的版本,故为了考虑升级和研究可用的操作系统,对开放环境进行配置。
64位版本的CentOS 7目前只提供了GCC 4.8.2 64位版本,默认不安装 GLIBC(version 2.17) 32位版本。

1.4目的
构造CentOs 64位版本下的 GCC 4.8.2 支持编译和运行32位程序,新标准编译环境。

2安装步骤
2.1安装GLIBC(32位)
安装 GLIBC 32位版本。
yum --disablerepo=* --enablerepo=c6-media install glibc-2.17-55.el7.i686

2.2安装 gcc 4.8.2
安装完整的 gcc 4.8.2。
2.3拷贝运行库libgcc_s.so
假设 GCC 4.8.2 安装到/usr/local/gcc-4.8.2。需要把32位版本的libgcc_s.so拷贝到32位库
/lib 下面。
cp /usr/local/gcc-4.8.2/lib/libgcc* /lib

2.4验证安装(C11)
[root@localhost src]# cat aa.c

#include <stdio.h>
#include <pthread.h>

//gcc -m32 -g -std=c11 aa.c -o aa -lpthread;./aa

void * f(void *data)
{
        //char buff[10];
        //char * p = 0;
        //memcpy(buff,"ddddddddddddddd",20);
        //memcpy(p,"sss",10);
        //p[0] = 'd';
        printf("centos 7,gcc 4.8.2 C11 test ok.\n");
        return 0;
}

int main(int argc,char * argv[])
{
        pthread_t t;
        pthread_create(&t,0,f,0);
        pthread_join(t,0);
        return 0;
}

gcc -m32 -g -std=c11 aa.c -o aa -lpthread;./aa;ldd aa
centos 7,gcc 4.8.2 C11 test ok.
        linux-gate.so.1 =>  (0xf77bb000)
        libpthread.so.0 => /lib/libpthread.so.0 (0xf7799000)
        libc.so.6 => /lib/libc.so.6 (0xf75db000)

        /lib/ld-linux.so.2 (0xf77bc000)

实际上,看到编译出来的程序,并不需要链接libgcc_s.so.*,而只需要 GLBC 的运行库,ldd 查看应用程序链接的动态库可以明确看出来。应该是 GCC 需要上诉库,实际上是GCC套件的ld程序需要libgcc_s.so*库文件。


2.5验证安装(C++ 2011)
[root@localhost src]# cat aa.cpp  

#include <stdio.h>
#include <thread>
#include <functional>
#include <algorithm>
using namespace std::placeholders;
using namespace std;

//g++ -std=c++11 -g aa.cpp -o aa -lpthread;./aa

void * f(void *data)
{
        printf("centos 7,gcc 4.8.2 c++ 2011 test ok.\n");
        return 0;
}

int main(int argc,char * argv[])
{
        void *p;
        //std::function<void * (void *)> pf = std::bind(f,nullptr);
        auto pf = std::bind(f,nullptr);
        std::thread t(pf);
        //t.start();
        t.join();
        return 0;
}

[root@localhost src]# g++ -std=c++11 -g aa.cpp -o aa -lpthread;./aa;ldd aa
centos 7,gcc 4.8.2 c++ 2011 test ok.
        linux-vdso.so.1 =>  (0x00007fffe7d6c000)
        libpthread.so.0 => /lib64/libpthread.so.0 (0x00007fd40dd34000)
        libstdc++.so.6 => /lib64/libstdc++.so.6 (0x00007fd40da2d000)
        libm.so.6 => /lib64/libm.so.6 (0x00007fd40d72a000)
        libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007fd40d514000)
        libc.so.6 => /lib64/libc.so.6 (0x00007fd40d153000)

        /lib64/ld-linux-x86-64.so.2 (0x00007fd40df58000)




0 0