码迷,mamicode.com
首页 > 编程语言 > 详细

【转】gcc/g++ 如何支持c11 / c++11标准编译

时间:2016-04-10 21:17:14      阅读:343      评论:0      收藏:0      [点我收藏+]

标签:

1
2
3
4
5
6
7
8
9
10
11
12
/*
    file : main.cpp
*/
#include <stdio.h>
 
int main() {
    int a[5] = { 1, 2, 2, 5, 1 };
    forint i:a ) {
        printf"%d\n", a[i] );
    }
    return 0;
}

那么g++ 就会提示以下错误:

1
2
3
main.cpp: In function ‘int main()’:
main.cpp:5:13: error: range-based ‘for’ loops are not allowed in C++98 mode
  forint i:a ) {

 

意思是指在C++98中不支持此循环方式,因为这是C++11新增的循环方式。

那么如果一定要编译呢?

通过命令man g++可以得知以下方法:

g++ -g -Wall -std=c++11 main.cpp

除了g++ , gcc 也可以类似方法支持C11

gcc -g -Wall -std=c11 main.cpp

如果不想每次写这个-std=C++11这个选项该怎么办呢?

  方法出处:http://stackoverflow.com/questions/16886591/how-do-i-enable-c11-in-gcc

  方法1:写Makefile

  方法2:取别名 :alias g++11="g++ -std=c++11"

【转】gcc/g++ 如何支持c11 / c++11标准编译

标签:

原文地址:http://www.cnblogs.com/JarningGau/p/5375220.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!