Ubuntu 12.04 GCC4.7启用C++11

Ubuntu 12.04 GCC4.7启用C++11

[日期:2012-06-06]来源:Linux社区作者:sheismylife[字体:]

因为想用template aliases特性,必须要GCC4.7.

又不想编译源代码,所以按照下面的方法安装。

sudoadd-apt-repositoryppa:Ubuntu-toolchain-r/test sudoapt-getupdate sudoapt-getinstallgcc-4.7 sudoapt-getinstallg++-4.7

如果Ubuntu 12.04 系统中存在多个版本的GCC,,可以在CMake工程的顶层的CMakeLists.txt中配置:

cmake_minimum_required(VERSION2.8.7) set(CMAKE_C_COMPILER”/usr/bin/gcc-4.7″) set(CMAKE_CXX_COMPILER”/usr/bin/g++-4.7″) project(sample) add_subdirectory(srcbin)

然后在build目录下执行cmake .. 可以看到:

chenshu@chenshu-beijing:~/work/research/tree/normal_tree/build$cmake.. –TheCcompileridentificationisGNU –TheCXXcompileridentificationisGNU –CheckforworkingCcompiler:/usr/bin/gcc-4.7 –CheckforworkingCcompiler:/usr/bin/gcc-4.7–works –DetectingCcompilerABIinfo –DetectingCcompilerABIinfo-done –CheckforworkingCXXcompiler:/usr/bin/g++-4.7 –CheckforworkingCXXcompiler:/usr/bin/g++-4.7–works –DetectingCXXcompilerABIinfo –DetectingCXXcompilerABIinfo-done –Configuringdone –Generatingdone –Buildfileshavebeenwrittento:/home/chenshu/work/research/tree/normal_tree/build

不过我的方法不是CMake推荐的最佳方法,一共有三种方法可以参考:

HowdoIuseadifferentcompiler? [edit]Method1:useenvironmentvariables ForCandC++,settheCCandCXXenvironmentvariables.Thismethodisnotguaranteedtoworkforallgenerators.(Specifically,ifyouaretryingtosetXcode’sGCC_VERSION,thismethodconfusesXcode.) Forexample: CC=gcc-4.2CXX=/usr/bin/g++-4.2cmake-G”YourGenerator”path/to/your/source [edit]Method2:usecmake-D SettheappropriateCMAKE_FOO_COMPILERvariable(s)toavalidcompilernameorfullpathonthecommand-lineusingcmake-D. Forexample: cmake-G”YourGenerator”-DCMAKE_C_COMPILER=gcc-4.2-DCMAKE_CXX_COMPILER=g++-4.2path/to/your/source [edit]Method3(avoid):useset() SettheappropriateCMAKE_FOO_COMPILERvariable(s)toavalidcompilernameorfullpathinalistfileusingset().Thismustbedonebeforeanylanguageisset(iebeforeanyproject()orenable_language()command). Forexample: 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″)#Addc++11functionality

然后运行

cmake ..

make VERBOSE=1

你会看到编译的g++命令后面带上了:

世界会向那些有目标和远见的人让路(冯两努——香港着名推销商

Ubuntu 12.04 GCC4.7启用C++11

相关文章:

你感兴趣的文章:

标签云: