编译安装 gcc 4.9 并验证使用
1. 准备环境(GCC 编译器)
- centOS 6.3
- cat /proc/version
- Linux version 2.6.32-279.el6.x86_64 (mockbuild@c6b9.bsys.dev.centos.org) (gcc version 4.4.6 20120305 (Red Hat 4.4.6-4) (GCC) ) #1 SMP Fri Jun 22 12:19:21 UTC 2012
- gcc --version
- gcc (GCC) 4.4.6 20120305 (Red Hat 4.4.6-4)
2. 准备文件
gcc-4.9.0.tar.gz
官网 (http://gcc.gnu.org/) 下载:
- ftp://mirrors.kernel.org/gnu/gcc/gcc-4.9.0/gcc-4.9.0.tar.gz
- http://gcc.petsads.us/releases/
3. 安装步骤
3.1. 解压 gcc-4.9.0.tar.gz
得到文件夹 gcc-4.9.0, 进入文件夹
- #tar -xvzf gcc-4.9.0.tar.gz
- #cd gcc-4.9.0
3.2. 下载编译准备文件
主要是须要以下的库文件(须要完整版本号, 下载所有文件)
- MPFR=mpfr-2.4.2
- GMP=gmp-4.3.2
- MPC=mpc-0.8.1
- ISL=isl-0.12.2
- CLOOG=cloog-0.18.1
运行./contrib/download_prerequisites
假设编译机器不能上网, 直接打开文本文件 download_prerequisites, 把里面
依赖的库下载回来后放在指定文件夹, 然后凝视下载命令, 再次运行
contrib/download_prerequisites, 把相关文件解压, 并建立链接
#vi contrib/download_prerequisites
凝视 wget 相关的内容, 手动把这些文件下载回来, 复制到工作文件夹 gcc-4.9.0 下
#./contrib/download_prerequisites
3.3. 创建编译文件夹并编译安装
- #mkdir ../gcc-build-4.9.0
- #cd ../gcc-build-4.9.0
- #../gcc-4.9.0/configure?--prefix=/usr/local/gcc-4.9.0?--enable-stage1-checking=release?--enable-stage1-languages=c,c++,go
- #make?-j?4
- #make install
编译后安装到? /usr/local/gcc-4.9.0
3.4. 验证安装
- #cd /usr/local/gcc-4.9.0
- #./bin/gcc -v
看到版本号信息
...
线程模型: posix
gcc 版本号 4.9.0 (GCC)
3.5. 验证编译功能(C++ 2011)
构造代码文件 test_gcc4.9.cpp, 内容例如以下:
- #include <string.h>
- #include <iostream>
- #include <stdio.h>
- #include <thread>
- #include <future>
- using namespace std;
- template<typename T> T stringcat(T &x,T &y)
- {
- ?? ?std::cout<<"stringcat start...."<<endl;
- ?? ?x += x;
- ?? ?y += y;
- ?? ???? // 睡眠指定毫秒数
- ?? ?std::this_thread::sleep_for(std::chrono::milliseconds(1000));
- ?? ?std::cout<<"stringcat end...."<<endl;
- ?? ?return x + y;
- }
- template<typename T> class CTheadOperator
- {
- ?? ?public:
- ?? ??? ??? // 採有创建线程发起异步调用
- ?? ??? ??? void createthread();
- };
- template<typename T> void CTheadOperator<T>::createthread()
- {
- ?? ?std::string x = "x",y = "y";
- ?? ?auto b = std::bind(stringcat<std::string>,x,y);
- ?? ?std::cout<<"createthread wait stringcat....\n";
- ?? ?std::thread tp(b);
- ?? ?tp.join();
- ?? ? // 注意观察返回值的变化
- ?? ?std::cout<<"无參数被改动"<<x<<" "<<y<<endl;
- }
- int main(int argc,char *argv[])
- {
- ?? ? CTheadOperator<std::string> tp;
- ?? ? tp.createthread();
- ?? ?return 0;
- }?? ?
编译运行, 注意看參数 -std=c++11,-lstdc++, 证明是使用新标准
查看链接的动态库, 是新的库
编译后的文件可运行运行正常
- #/usr/local/gcc-4.9.0/bin/g++ -Wwrite-strings -std=c++11 test_gcc4.9.cpp -o test_gcc4.9 -I/usr/local/gcc-4.9.0/include -L/usr/local/gcc-4.9.0/lib64 -lstdc++ -fpermissive -lpthread
- #ldd test_gcc4.9
- ??????? linux-vdso.so.1 =>? (0x00007fff2fbff000)
- ??????? libstdc++.so.6 => /usr/local/gcc-4.9.0/lib64/libstdc++.so.6 (0x00007f94ae037000)
- ??????? libpthread.so.0 => /lib64/libpthread.so.0 (0x0000003431800000)
- ??????? libm.so.6 => /lib64/libm.so.6 (0x0000003431000000)
- ??????? libgcc_s.so.1 => /usr/local/gcc-4.9.0/lib64/libgcc_s.so.1 (0x00007f94ade07000)
- ??????? libc.so.6 => /lib64/libc.so.6 (0x0000003430c00000)
- ??????? /lib64/ld-linux-x86-64.so.2 (0x0000003430800000
- #./test_gcc4.9
- createthread wait stringcat....
- stringcat start....
- stringcat end....
无參数被改动 x y?????? ?
3.6. 验证编译功能(C11), 缺少 threads.h 文件, 编译失败
创建文件 test_gcc4.9.c, 内容例如以下:
- #include <string.h>
- #include <stdio.h>
- // 注意这行, 路径不一致
- #include <thr/threads.h>
- // 注意这个, 函数定义必须是
- //typedef int(*_Thrd_start_t)(void *);
- int fun(void *data)
- {
- ??? printf("thread=%d\n", thrd_current());
- ??? //std::map<std::string, std::string> m;
- ??? //m.insert(std::make_pair("a1", "b1"));
- ??? //auto result = m.insert(std::make_pair("a1", "b1"));
- ??? //if (result.second)
- ??? //{
- ??????? //printf("m insert true %d,", thrd_current());
- ??????? //std:std::cout << "key=" << result.first->first << ",value=" << result.first->second << endl;
- ??? //}
- ??? //else
- ??? //{
- ?????? //printf("m insert fail %d\n", thrd_current());
- ??? //}
- ??? return 0;
- }
- int main(int argc,char *argv[])
- {
- ??? thrd_t loc;
- ??? thrd_create(&loc, fun,0);
- ??? thrd_join(loc,0);
- ??? return 0;
- }
编译运行, 注意看參数 -std=c11,-lgcc_s, 证明是使用新标准
查看链接的动态库, 是新的库
呜呼, 无法编译成功, 无法运行正常!!!
- #/usr/local/gcc-4.9.0/bin/gcc -Wwrite-strings -std=c11 test_gcc4.9.c -o test_gcc4.9 -I/usr/local/gcc-4.9.0/include -L/usr/local/gcc-4.9.0/lib64 -lgcc_s -lpthread;
- #test_gcc4.9.c:4:21: 致命错误: threads.h: 没有那个文件或文件夹
- ?#include <threads.h>
- ???????????????????? ^
编译中断
后记:
安装免费的 visual studio express 2013 后, 正确编译后运行正常;
来源: http://www.bubuko.com/infodetail-2493680.html