ubuntu12.04 lts 安装gcc 4.8

  gcc 4.8.1 是第一个完全支持C++11 的编译器,Windows上可以安装mingw版的,在sourceforge 上有下载,安装也比较方便。在Linux上安装的话需要首先安装一些依赖库。在Ubuntu12.04 lts默认安装的是gcc4.6.3,其实该版本也支持一些c++11的特性,可以通过增加“-std=c++0x” 编译选项来使用这些特性,但是对多线程库的支持较差,gcc 4.8.1 是通过ppa来安装的,因此需要安装ppa repository 。下面就来看一下安装步骤:

首先安装依赖:

sudo apt-get install libgmp-dev
sudo apt-get install libmpfr4 libmpfr-dev
sudo apt-get install libmpc-dev libmpc2
sudo apt-get install libtool
sudo apt-get install m4
sudo apt-get install bison
sudo apt-get install flex
sudo apt-get install autoconf

接下来进入到安装gcc4.8.1 的主要步骤:

sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update

sudo apt-get install gcc-4.8
sudo apt-get install g++-4.8
sudo apt-get install gcc-4.8-multilib
sudo apt-get install g++-4.8-multilib
sudo apt-get install gcc-4.8-doc
sudo update-alternatives –install /usr/bin/gcc gcc /usr/bin/gcc-4.8 20
sudo update-alternatives –install /usr/bin/g++ g++ /usr/bin/g++-4.8 20
sudo update-alternatives –config gcc
sudo update-alternatives –config g++

sudo apt-get update
sudo apt-get upgrade -y
sudo apt-get dist-upgrade

之后就可以使用了,通过使用gcc -v 命令可以查看当前gcc的版本:

ubuntu12.04 lts 安装gcc 4.8

 

 网络条件不好的话,安装过程比较慢,可以使用网易或者sohu的源提高安装速度。

 

 

time since epoch

C++11 提供了新的获取系统时间的库函数,在获取时间的时候一般常用的是获取time since epoch,下面来看一下如何获取这个时间。

 

 1 #include <iostream>
 2 #include <chrono>
 3 #include <ctime>
 4 #include <sys/time.h>
 5 #include <cstdint>
 6 constexpr int microSecPerSec = 1000000;
 7  
 8 int main()
 9 {
10     std::chrono::time_point<std::chrono::system_clock> p1, p2, p3;
11     struct timeval tv;
12     int64_t mSecond;
13  
14     p2 = std::chrono::system_clock::now();
15     p3 = p2 - std::chrono::hours(24);
16     gettimeofday(&tv,NULL);
17     mSecond = tv.tv_sec ;
18  
19     std::time_t epoch_time = std::chrono::system_clock::to_time_t(p1); // seconds
20     std::cout << "epoch: " << std::ctime(&epoch_time);
21     std::time_t today_time = std::chrono::system_clock::to_time_t(p2);
22     std::cout << "today: " << std::ctime(&today_time);
23  
24     std::cout << "hours since epoch: "
25                 << std::chrono::duration_cast<std::chrono::hours>(   //hours
26                    p2.time_since_epoch()).count() 
27                 << std::endl;
28                 
29     std::cout << "yesterday, hours since epoch: "
30                 << std::chrono::duration_cast<std::chrono::hours>(
31                    p3.time_since_epoch()).count() 
32                 << std::endl;
33               
34     std::cout << "Microseconds since epoch: (chrono) : "   //microseconds
35                 << std::chrono::duration_cast<std::chrono::microseconds>(p2.time_since_epoch()).count() // p2 - p1 ==
36                 <<std::endl;
37                 
38     std::cout << "Microseconds since epoch: (gettimeofday) :" << mSecond * microSecPerSec + tv.tv_usec 
39                 <<std::endl;
40                 
41     return 0;
42 }

结果:

C:/Users/zhuyp/Desktop/tmp>cpp_time
epoch: Thu Jan 01 08:00:00 1970
today: Fri Jan 24 23:51:10 2014
hours since epoch: 386271
yesterday, hours since epoch: 386247
Microseconds since epoch: (chrono) : 1390578670080869
Microseconds since epoch: (gettimeofday) :1390578670080869