Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to enable c++11 in linux

Tags:

c++

linux

c++11

I wanted to use the unordered_map STL in c++, but as soon as I use the header, it gives me this error:

This file requires support for the compiler and library support for the ISO C++11 standard. This support is currently experimental and must be enabled with -std=c++11 or -std=gnu++11 compiler options.

I am attaching my code that I wanted to run, below. (Any inputs on the code are welcome too. thanks)

#include <iostream>
#include <unordered_map>
using namespace std;

class Node
{
public:
string a,b;
Node()
{
    a="hello";
    b="world";
}
};


int main ()
{
    unordered_map<Node> mymap;
    Node mynode;
    mymap.insert(mynode);
    std::cout << "myrecipe contains:" << std::endl;
  for (auto& x: mymap)
    std::cout << x.a << ": " << x.b << std::endl;

}

Edit: I got it to work by using the following commmand: g++ -std=c++11 [filename].cpp

Thanks for the help.

like image 732
pyroscepter Avatar asked Nov 16 '14 05:11

pyroscepter


People also ask

How do I know if I have C ++ 11?

To see if your compiler has C++11 support, run it with just the --version option to get a print out of the version number. Do this for whichever compiler(s) you wish to use with Rosetta. Acceptable versions: GCC/g++: Version 4.8 or later.

How do I change from C++ to 11?

Go to Tools Tab. Select Compiler Options.. Go to General tab. Check the checkbox named "Add following commands when calling the compiler", write this statement -std=c++11 , and then press OK.

Does G ++ 4.8 support C ++ 11?

Status of Experimental C++11 Support in GCC 4.8GCC provides experimental support for the 2011 ISO C++ standard. This support can be enabled with the -std=c++11 or -std=gnu++11 compiler options; the former disables GNU extensions.

How do I enable C ++ 17 in GCC?

C++17 features are available since GCC 5. This mode is the default in GCC 11; it can be explicitly selected with the -std=c++17 command-line flag, or -std=gnu++17 to enable GNU extensions as well.


2 Answers

First Option:

You can remove to error with -std=c++11 in compile time.

g++ -o binary yourFile.cpp -std=c++11

Second Option to integrate the development with c++11:

You can use a makefile with the CXXFLAGS set with -std=c++11 A makefile is a simple text file with instructions about how to compile your program. Create a new file named Makefile (with a capital M). To automatically compile your code just type the make command in a terminal. You may have to install make.

Here is the code :

CXX=clang++
CXXFLAGS=-g -std=c++11 -Wall -pedantic
BIN=prog

SRC=$(wildcard *.cpp)
OBJ=$(SRC:%.cpp=%.o)

all: $(OBJ)
    $(CXX) -o $(BIN) $^

%.o: %.c
    $(CXX) $@ -c $<

clean:
    rm -f *.o
    rm $(BIN)

It assumes that all the .cpp files are in the same directory as the makefile. But you can easily tweak your makefile to support a src, include and build directories.

like image 22
Skynet Avatar answered Oct 22 '22 04:10

Skynet


The main answer to your question: specify -std=c++11 in your compile command.

Precisely which C++11 features are available will depend on your version of GCC. Here are two links that might help:

  • https://gcc.gnu.org/projects/cxx0x.html

  • http://wiki.apache.org/stdcxx/C++0xCompilerSupport

like image 135
FoggyDay Avatar answered Oct 22 '22 05:10

FoggyDay