Ubuntu 12.04 GCC4.7启用C++11
来源:互联网 发布:联想控股idc数据 编辑:程序博客网 时间:2024/06/04 19:21
因为想用template aliases特性,必须要GCC4.7.
又不想编译源代码,所以按照下面的方法安装。
sudo add-apt-repository ppa:ubuntu-toolchain-r/testsudo apt-get updatesudo apt-get install gcc-4.7sudo apt-get install g++-4.7
如果系统中存在多个版本的GCC,可以在CMake工程的顶层的CMakeLists.txt中配置:
cmake_minimum_required(VERSION 2.8.7)set(CMAKE_C_COMPILER "/usr/bin/gcc-4.7")set(CMAKE_CXX_COMPILER "/usr/bin/g++-4.7")project (sample)add_subdirectory(src bin)
然后在build目录下执行cmake .. 可以看到:
chenshu@chenshu-beijing:~/work/research/tree/normal_tree/build$ cmake ..-- The C compiler identification is GNU-- The CXX compiler identification is GNU-- Check for working C compiler: /usr/bin/gcc-4.7-- Check for working C compiler: /usr/bin/gcc-4.7 -- works-- Detecting C compiler ABI info-- Detecting C compiler ABI info - done-- Check for working CXX compiler: /usr/bin/g++-4.7-- Check for working CXX compiler: /usr/bin/g++-4.7 -- works-- Detecting CXX compiler ABI info-- Detecting CXX compiler ABI info - done-- Configuring done-- Generating done-- Build files have been written to: /home/chenshu/work/research/tree/normal_tree/build
不过我的方法不是CMake推荐的最佳方法,一共有三种方法可以参考:
http://www.cmake.org/Wiki/CMake_FAQ#Method_3_.28avoid.29:_use_set.28.29
How do I use a different compiler?[edit] Method 1: use environment variablesFor C and C++, set the CC and CXX environment variables. This method is not guaranteed to work for all generators. (Specifically, if you are trying to set Xcode's GCC_VERSION, this method confuses Xcode.)For example:CC=gcc-4.2 CXX=/usr/bin/g++-4.2 cmake -G "Your Generator" path/to/your/source[edit] Method 2: use cmake -DSet the appropriate CMAKE_FOO_COMPILER variable(s) to a valid compiler name or full path on the command-line using cmake -D.For example:cmake -G "Your Generator" -D CMAKE_C_COMPILER=gcc-4.2 -D CMAKE_CXX_COMPILER=g++-4.2 path/to/your/source[edit] Method 3 (avoid): use set()Set the appropriate CMAKE_FOO_COMPILER variable(s) to a valid compiler name or full path in a list file using set(). This must be done before any language is set (ie before any project() or enable_language() command).For example:set(CMAKE_C_COMPILER "gcc-4.2")set(CMAKE_CXX_COMPILER "/usr/bin/g++-4.2")project("YourProjectName")[edit]
默认C++11是不启用的。在src/CMakeLists.txt文件中加上这行:
SET(CMAKE_CXX_FLAGS "-std=c++11") # Add c++11 functionality
然后运行
cmake ..
make VERBOSE=1
你会看到编译的g++命令后面带上了:
/usr/bin/g++-4.7 -std=c++11
现在成功了,享受C++11吧。
- Ubuntu 12.04 GCC4.7启用C++11
- Ubuntu 10.04下 GCC4.7
- Ubuntu 12.04 安装设置gcc4.4
- Ubuntu 12.04 安装设置gcc4.4
- Ubuntu 12.04 & 13.04 安装 GCC4.8.1
- Ubuntu 12.04 & 13.04 安装 GCC4.8.1
- UBUNTU 12.04下编译安装 GCC4.1.2
- Ubuntu 12.04 安装设置gcc4.4
- Ubuntu 12.04 升级gcc到gcc4.8
- ubuntu 12.04启用休眠
- Ubuntu 安装GCC4.8
- [转]Ubuntu 12.04 & 13.04 安装 GCC4.8.1
- Compile gcc4.8 on Ubuntu 12.04 LTS X64
- centos6.4安装GCC4.8 C++11
- .ubuntu 10.04 安装 gcc4.6.1
- Ubuntu 13.04 安装 GCC4.8.1
- Ubuntu中编译GCC4.9.0
- Ubuntu 11.10 GCC4.5 GCC4.6无缝切换
- Android平台上sqllite 简介
- linux多线程编程
- 位图排序及其扩展应用——《编程珠玑》读书笔记
- 关于离职
- Linux 线程实现机制分析
- Ubuntu 12.04 GCC4.7启用C++11
- 创业路上,做个优秀的弄潮儿
- 如何删除电话与调制解制器里面的我的位置?
- 静态连接总结
- 通俗易懂云计算
- Java学习:线程基础(二)
- USACO/Mother's Milk (DFS)
- 初始化 多余逗号Arrays: additionnal commas
- 通过样式控制设置Repeater隐藏指定的列