pyroscepter
pyroscepter

Reputation: 205

how to enable c++11 in linux

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.

Upvotes: 5

Views: 27574

Answers (3)

Arnab Nandy
Arnab Nandy

Reputation: 6702

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.

Upvotes: 3

Rupesh Yadav.
Rupesh Yadav.

Reputation: 904

compile with:

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

Upvotes: 0

paulsm4
paulsm4

Reputation: 121649

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:

Upvotes: 8

Related Questions