gcc/g++ How to support C11/C++11 standard compilation

Source: Internet
Author: User

If you use the command g++-g-wall main.cpp Compile the following code:

/*    file:main.cpp*/#include <stdio.h>int main () {int a[5] = {1, 2, 2, 5, 1};for (int i:a) {printf ("%d\n", a [i]);} return 0;}

Then g++ will prompt the following error:

main.cpp:In function ' int main () ': main.cpp:5:13:error:range-based ' for ' loops is not allowed in c++98 mode for  (in T i:a) {

This means that the loop is not supported in c++98, because this is the c++11 new loop method.

So what if you have to compile it?

The following methods can be learned by ordering man g++ :

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

In addition to g++, GCC can support C11 in a similar way

Gcc-g-wall-std=c11 main.cpp

What should I do if I don't want to write this-std=c++11 this option every time?

Method Source: HTTP://STACKOVERFLOW.COM/QUESTIONS/16886591/HOW-DO-I-ENABLE-C11-IN-GCC

Method 1: Write Makefile

Method 2: Alias: Aliasg++11= "g++-std=c++11"

gcc/g++ How to support C11/C++11 standard compilation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.