本文翻译自:Compiling C++11 with g++

I'm trying to update my C++ compiler to C++11. 我正在尝试将我的C ++编译器更新为C ++ 11。 I have searched a bit and I have come to the conclusion that I have to use the flag -std=c++0x or -std=gnu++0x , but I don't know many things about flags. 我进行了一些搜索,得出的结论是我必须使用标志-std=c++0x-std=gnu++0x ,但是我对标志的了解并不多。 Can anyone help me? 谁能帮我? (I'm using Ubuntu 12.04.) (我正在使用Ubuntu 12.04。)

Here is the error that I get from the compiler when I attempt to use a library which is included in C++11 (ie array): 这是当我尝试使用C ++ 11中包含的库(即数组)时从编译器收到的错误:

#include <array>
#include <iostream>int main()
{std::array<int, 3> arr = {2, 3, 5};...
}

This file requires compiler and library support for the upcoming ISO C++ standard, C++0x. 该文件需要编译器和库支持即将推出的ISO C ++标准C ++ 0x。 This support is currently experimental, and must be enabled with the -std=c++0x or -std=gnu++0x compiler options. 该支持目前处于试验阶段,必须使用-std = c ++ 0x或-std = gnu ++ 0x编译器选项启用。


#1楼

参考:https://stackoom.com/question/hU3a/用g-编译C


#2楼

Your ubuntu definitely has a sufficiently recent version of g++. 您的ubuntu肯定具有最新版本的g ++。 The flag to use is -std=c++0x . 要使用的标志是-std=c++0x


#3楼

Flags (or compiler options) are nothing but ordinary command line arguments passed to the compiler executable. 标志(或编译器选项)不过是传递给编译器可执行文件的普通命令行参数。

Assuming you are invoking g++ from the command line (terminal): 假设您从命令行(终端)调用g ++:

$ g++ -std=c++11 your_file.cpp -o your_program

or 要么

$ g++ -std=c++0x your_file.cpp -o your_program

if the above doesn't work. 如果上述方法不起作用。


#4楼

If you want to keep the GNU compiler extensions, use -std=gnu++0x rather than -std=c++0x. 如果要保留GNU编译器扩展,请使用-std = gnu ++ 0x而不是-std = c ++ 0x。 Here's a quote from the man page: 这是手册页中的一句话:

The compiler can accept several base standards, such as c89 or c++98, and GNU dialects of those standards, such as gnu89 or gnu++98. 编译器可以接受几种基本标准,例如c89或c ++ 98,以及这些标准的GNU方言,例如gnu89或gnu ++ 98。 By specifying a base standard, the compiler will accept all programs following that standard and those using GNU extensions that do not contradict it. 通过指定基本标准,编译器将接受该标准之后的所有程序以及使用与该标准不矛盾的GNU扩展的程序。 For example, -std=c89 turns off certain features of GCC that are incompatible with ISO C90, such as the "asm" and "typeof" keywords, but not other GNU extensions that do not have a meaning in ISO C90, such as omitting the middle term of a "?:" expression. 例如,-std = c89将关闭与ISO C90不兼容的GCC某些功能,例如“ asm”和“ typeof”关键字,但不会关闭其他在ISO C90中没有意义的GNU扩展,例如省略“?:”表达式的中间术语。 On the other hand, by specifying a GNU dialect of a standard, all features the compiler support are enabled, even when those features change the meaning of the base standard and some strict-conforming programs may be rejected. 另一方面,通过指定标准的GNU方言,可以启用编译器支持的所有功能,即使这些功能更改了基本标准的含义并且可能会拒绝某些符合严格标准的程序。 The particular standard is used by -pedantic to identify which features are GNU extensions given that version of the standard. -pedantic使用特定的标准来识别给定标准版本的GNU扩展是哪些功能。 For example-std=gnu89 -pedantic would warn about C++ style // comments, while -std=gnu99 -pedantic would not. 例如,-std = gnu89 -pedantic将警告有关C ++样式//的注释,而-std = gnu99 -pedantic则不会。


#5楼

You can check your g++ by command: 您可以通过以下命令检查g++

which g++
g++ --version

this will tell you which complier is currently it is pointing. 这将告诉您当前指向哪个编译器。

To switch to g++ 4.7 (assuming that you have installed it in your machine),run: 要切换到g++ 4.7(假设您已将其安装在计算机中),请运行:

sudo update-alternatives --config gccThere are 2 choices for the alternative gcc (providing /usr/bin/gcc).Selection    Path              Priority   Status
------------------------------------------------------------0            /usr/bin/gcc-4.6   60        auto mode1            /usr/bin/gcc-4.6   60        manual mode
* 2            /usr/bin/gcc-4.7   40        manual mode

Then select 2 as selection(My machine already pointing to g++ 4.7,so the *) 然后选择2作为选择(我的机器已经指向g++ 4.7,所以*)

Once you switch the complier then again run g++ --version to check the switching has happened correctly. 切换编译器后,请再次运行g++ --version来检查切换是否正确进行。

Now compile your program with 现在用

g++ -std=c++11 your_file.cpp -o main

#6楼

You can refer to following link for which features are supported in particular version of compiler. 您可以参考以下链接 ,这些链接在特定版本的编译器中受支持。 It has an exhaustive list of feature support in compiler. 它提供了编译器中功能支持的详尽列表。 Looks GCC follows standard closely and implements before any other compiler. 看起来GCC严格遵循标准并在任何其他编译器之前实现。

Regarding your question you can compile using 关于您的问题,您可以使用

  1. g++ -std=c++11 for C++11 g++ -std=c++11 for C ++ 11
  2. g++ -std=c++14 for C++14 g++ -std=c++14 for C ++ 14
  3. g++ -std=c++17 for C++17 g++ -std=c++17 for C ++ 17
  4. g++ -std=c++2a for C++20, although all features of C++20 are not yet supported refer this link for feature support list in GCC. g++ -std=c++2a for C ++ 20,尽管尚不支持C ++ 20的所有功能,但请参考此链接以获取GCC中的功能支持列表。

The list changes pretty fast, keep an eye on the list, if you are waiting for particular feature to be supported. 如果您正在等待特定功能的支持,则列表变化非常快,请密切注意列表。

用g ++编译C ++ 11相关推荐

  1. g++编译c++11特性 的.cc文件

    写一个.cc文件,其中抱哈std::lock_guard以及std::thread等c++11特性,开始使用gcc编译,过程中出现如下问题 gcc test_lock.cc -o test_lock ...

  2. g++编译c++11 thread报错问题 及c++多线程操作

    测试代码thread.cpp #include <thread> #include <iostream> using namespace std;void run(int n) ...

  3. gcc g++ 支持c++11编译的标准和区别

    g++ -g -Wall -std=c++11 main.cpp gcc -g -Wall -std=c11 main.cpp 如果不想每次写这个-std=C++11这个选项该怎么办呢? 方法出处:h ...

  4. g++编译支持c++11的方法

    网上找了一下有两种方式: 方法一:直接在代码的cpp文件最开始的位置,添加如下代码: #pragma GCC diagnostic error "-std=c++11" 方法二:使 ...

  5. 用g++编译C++ 的流程示例如下

    注意 - 编译代码时,确保使用 C ++ 11. 你可以通过以下命令从命令行执行此操作: cmd中执行 g++  -std=c++11 tests.cpp 执行完上述命令后会在tests.cpp的同级 ...

  6. linux编译c++11的代码

    C++11,(即ISO/IEC 14882:2011),是目前的C++编程语言的最新正式标准.它取代了第二版标准(第一版公开于1998年,第二版于2003年更新,分别通称C++98以及C++03,两者 ...

  7. Mingw下g++编译执行顺序错误

    今天写一个简单的线性表时,用Mingw中的g++编译.调试.运行时发现一个奇怪的现象:程序的执行顺序与实际编写顺序不一致. 编译环境:代码编写  win7下   editplus + Mingw 4. ...

  8. 为Sublime Text 3的C++编译器(g++)添加C++11标准的方法

    写在前面 最近熟悉了使用Sublime写C++单文件并编译的方法, 但是美中不足的就是不能使用C++11的新特性, 网上有的方法是修改默认的编译命令, 这个方法需要修改安装目录下的一个文件, 还要解压 ...

  9. linux g++开启C++11/14支持

    linux g++开启C++11/14支持 sudo vim ~/.bashrc 在some more ls aliases注释块的地方添加下面这两行: alias g++11='g++ -g -Wa ...

最新文章

  1. php将图片链接转换为base64编码文件流
  2. 软件测试培训分享:软件测试岗位面试技巧有哪些?
  3. 你可以恢复模糊的图像吗?
  4. ISCW实验:配置Cisco IOS EASY ××× Server和Cisco ××× Client
  5. 仿微信的网络聊天室项目开发【完整源码讲解,Java一年工作经验面试题
  6. hdu 1874畅通工程续(基础Floyd)
  7. java Mina sftp_java – 使用Apache Mina作为模拟/内存SFTP服务器进行单元测试
  8. 浮点卷积winograd算法
  9. 一个追求高效的学习者手机里装有哪些APP?(转)
  10. python创建变量_【转载】 Python动态生成变量
  11. linux怎样禁止他人远程,linux禁止用户远程登录的方法
  12. JAVA总裁--Java数组基础知识
  13. 仿照java做一个脚本语言_java调用其它语言脚本(python、js)
  14. 手机html文件转TXT,Html 转换 Txt
  15. 最新教程:M1芯片的Mac电脑进入恢复模式?
  16. 【转】移动前端工作的那些事---前端制作篇之框架篇--jqMobi框架
  17. java反汇编_Java虚拟机学习总结(3)——JDK内置工具(jps、jstack、jmap、jstat)使用详解...
  18. android ble 写失败,Android低功耗蓝牙BLE写入数据很大几率会失败 求解
  19. Mysql获取流水号
  20. 2-2 Aruba控制器 无线漫游优化 2020

热门文章

  1. BATZ,一份《Android架构开发手册》就够,已offer
  2. android核心机制之Zygote启动流程
  3. Android log 里面快速搜索错误堆栈 ( 关键字)
  4. 【C++】修改const变量的值
  5. Android GLSurfaceView.Renderer 类 onDrawFrame 方法的刷新机制
  6. C语言函数集(十三)
  7. php中访问控制关键字,PHP 关于访问控制和运算符优先级简介
  8. python 功能 代码_让你的Python代码实现类型提示功能
  9. 前端知识点回顾之重点篇——JavaScript异步机制
  10. 【margin和padding的区别】