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.
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
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.
compile with:
g++ -o binary source.cpp -std=c++11
Related
while using g++ (version 4.9.2) from MinGW provided with Code::Blocks IDE, I am getting incredibly huge .exe files, going up to 1000KB for a "Hello, World!" program. I use exactly the same compiling options as like it would be compiled from "inside" of Code::Blocks (mingw32-g++.exe -Wall -g hello.cpp -o hello.exe, anyway, i just copy it from the Code::Blocks log window), but unlike that way, which produced about 70KB output for Debug target and 50KB for Release, this keeps on making incredibly large output.
Did anyone else meet a similar issue?
[EDIT] My project (created only for testing Makefiles purposes) consists on two files, main.cpp and fun.cpp (no headers). Here is main.cpp:
#include<iostream>
using namespace std;
void fun(void);
int main()
{
cout<<"Hello Make!"<<endl;
fun();
return 0;
}
and here is fun.cpp:
#include<iostream>
using namespace std;
void fun()
{
cout<<"Hello from The Module!"<<endl;
}
And finally, this is my Makefile:
CC = mingw32-g++
CFLAGS = -Wall -Os -lto
all: main.o fun.o
${CC} -o hello.exe $^
relink:
mingw32-g++ -o hello.exe hello.o fun.o
main.o: main.cpp
mingw32-g++ ${CFLAGS} -c main.cpp -o $#
fun.o: fun.cpp
mingw32-g++ ${CFLAGS} -c fun.cpp -o $#
clean:
del *.o
When compiling this "project" under Code::Blocks I get the following
log
When using Makefile, the output file is 1024KB large:
(print screen)
Debug builds will usually be larger than optimized (aka "release") ones. Try adding -O2 or -O3 to your build options (or -Os to specifically optimize for size).
Also, if you don't need the debug symbols in a release build then remove -g (or remove them afterwards with the strip command).
Also; compiling with Link-Time Optimization can sometimes yield a size reduction (in addition to a performance improvement) - for that use the -lto option (in addition to one of the -O options).
The above should give you a significant size reduction.
See the gcc manual for more details on the options.
I'm trying to compile C++ 11 code on a server running Ubuntu from my home computer using Putty. I'm using a makefile to compile and include shared pointers in the code, but it gives me this error:
/usr/include/c++/4.8/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
However, when I try to compile the code from the server hosting the files (when I'm at university), it compiles perfectly. Here is my makefile:
all: huffmandriver.o huffmannode.o huffmantree.o
g++ -o huffencode huffmandriver.o huffmannode.o huffmantree.o -std=c++11
huffmannode.o: huffmannode.cpp huffmannode.h
g++ -c huffmannode.cpp -std=c++11
huffmantree.o: huffmantree.cpp huffmantree.h
g++ -c huffmantree.cpp -std=c++11
clean:
#rm -f *.o
#rm -f huffencode
I have also tried adding the flags -stdlib=libc++ -std=gnu++, but that does not work either. Here is a snippet of the code where the error is being thrown:
// Huffman Node class header
#ifndef HUFFMANNODE_H
#define HUFFMANNODE_H
#include <memory>
#include <string>
namespace YNGMAT005 {
class HuffmanNode {
private:
std::shared_ptr<HuffmanNode> left;
std::shared_ptr<HuffmanNode> right;
std::shared_ptr<HuffmanNode> parent;
std::string letter;
int frequency;
public:
HuffmanNode(std::string l, int freq);
~HuffmanNode();
std::shared_ptr<HuffmanNode> & get_left();
std::shared_ptr<HuffmanNode> & get_right();
std::shared_ptr<HuffmanNode> & get_parent();
void set_left(std::shared_ptr<HuffmanNode> & l);
void set_right(std::shared_ptr<HuffmanNode> & r);
bool has_left();
bool has_right();
void set_parent(std::shared_ptr<HuffmanNode> & p);
bool has_parent();
std::string get_letter();
int get_frequency();
};
}
#endif
Many thanks!
I don't know why you're seeing different behaviour from a different login context (check which g++; g++ --version on both and see if your .profile, .bash_profile or .bashrc does anything weird based on ssh-vs-local login).
Nonetheless, you should be able to getting it working in both settings by providing -std=c++11 to the default rule for .cpp to .o thus:
CXXFLAGS = -std=c++11
Additionally, (shamelessly stealing from Daniel's answer) you can remove the explicit rules for the other .cpp files, just leaving the .h dependencies:
huffmannode.o: huffmannode.h
huffmantree.o: huffmantree.h
These will then automatically pick up the CXXFLAGS setting.
You might need to compile huffmandriver.cpp with -std=c++11 as well. Currently, you have rules for compiling huffmannode.cpp and huffmantree.cpp with the -std=c++11 compiler option, but not huffmandriver.cpp.
You can create a custom pattern rule for making .o files from .cpp files and specifying the header dependencies like so:
huffmannode.o: huffmannode.h
huffmantree.o: huffmantree.h
%.o: %.cpp
g++ -std=c++11 -c -march=native -o $# $<
An alternative approach is to define the CXXFLAGS implicit variable to -std=c++11. This way, the built-in rule ‘$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c’ for compiling C++ source files will use the -std=c++11 compiler option.
Makefile
ifeq ($(wifiSim),1)
WIFISIM :=1
endif
all: test.cpp
test.cpp : test.o
./a.out
test.o :
c++ test.cpp
test.cpp
#include <iostream>
using namespace std;
int main()
{
#ifdef WIFISIM
cout << "Inside wifisim = 1" << endl;
#else
cout << "Outside wifisim = 1" << endl;
#endif
return 0;
}
I want to use the WIFISIM in the test.cpp.
I am running make wifiSim=1 all
But the else is being executed in test.cpp
Is there any way I can do it without doing any changes in the way the compilation for test.cpp is done, because I need to use this flag WIFISIM in many files and I do not want to change the way compilation for them is being done.
You may do something like this
ifeq ($(wifiSim),1)
WIFISIM := -DWIFISIM
endif
all: test.cpp
test.cpp : test.o
./a.out
test.o :
c++ $(WIFISIM) test.cpp
"Is there any way I can do it without doing any changes in the way the compilation for "test.cpp" is done, because I need to use this flag WIFISIM in many files and I do not want to change the way compilation for them is being done."
No, there's no way without changing the compiler call action in the rule.
You should change your strategy writing the makefile. make actually supports implicit rules how to create a .o file from a .cpp and uses an action that looks like
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c
Thus you could add the -DWIFISIM conditionally to the $(CPPFLAGS) or $(CXXFLAGS) variables, and it will be applied for all .cpp files compiled.
Sample using implicit rules:
ifeq ($(wifiSim),1)
CXXFLAGS += -DWIFISIM
endif
SRC_FILES := test.cpp abc.cpp yxz.cpp
OBJ_FILES := $(patsubst %.cpp,%.o,$(SRC_FILES))
all: test
test: $(OBJ_FILES)
If you use GCC, you may use option -DWIFISIM as options passed to GCC/G++. Other compilers have similiar options, such as /D in Microsoft Visual Studio:
CXXFLAGS =
ifeq ($(wifiSim),1)
CXXFLAGS += -DWIFISIM
endif
all: test.cpp
test.cpp : test.o
./a.out
test.o :
c++ $(CXXFLAGS) test.cpp
Result:
$ make -n wifiSim=1
c++ -DWIFISIM test.cpp
./a.out
$ make -n
c++ test.cpp
./a.out
I have a Makefile for a project using regex. Thus I need to use g++-4.9 and c++11.
BIN = bin
OBJ = src/main.o src/time2.o src/except.o src/except2.o src/struct.o src/index.o
FLAGS = -lncurses
CC = g++-4.9 -std=c++11 -Wall -pedantic -Wno-long-long -O0 -ggdb
all: compile
run: compile
./$(BIN)
clean:
rm -f $(BIN) $(OBJ)
compile: $(OBJ)
$(CC) -o $(BIN) $(OBJ) $(FLAGS)
but when I try to make compile:
g++ -c -o src/main.o src/main.cpp In file included from
/usr/include/c++/4.8/regex:35:0,
from src/time2.h:16,
from src/main.cpp:3: /usr/include/c++/4.8/bits/c++0x_warning.h:32:2: error: #error This
file requires compiler and library support for the ISO C++ 2011
standard. This support is currently experimental, and must be enabled
with the -std=c++11 or -std=gnu++11 compiler options. #error This
file requires compiler and library support for the \ ^ In file
included from src/main.cpp:5:0: src/struct.h:99:18: error: ‘regex’ has
not been declared bool isnamegood(regex x,const string& name);
^
so I don't understand what is wrong, may you help me please?
The problem is that your compile target already depends on the object files! But they aren't build yet, so make tries to find a rule to build them. Because you have not defined a custom rule, it uses the implicit rule:
Compiling C++ programs n.o is made automatically from n.cc, n.cpp, or
n.C with a recipe of the form ‘$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c’. We
encourage you to use the suffix ‘.cc’ for C++ source files instead of
‘.C’.
This rule will of course not use the flags you set in CC. This explains why the gcc command line printed by make does not match your CC. You can test this by running make with --no-builtin-rules, as this should disable all the implicit rules.
A good idea in such cases is to run make with -d or --debug, I think this should display rule evaluations.
Instead of using the c macros, use the c++ macros
CXX= g++-4.9
CXXFLAGS= -std=c++11 -Wall -pedantic -Wno-long-long -O0 -ggdb
Then the default rules should pick them up and you need to type less in your makefile.
Also see the complete list of variables (macros) used by Make
When I want to compile, I need to specify -std=c++11 like this:
g++ -Wall -std=c++11 main.cpp -o main
and I wonder if there was a solution to set the -std=c++11 flag permanently so it will be possible to do:
g++ -Wall main.cpp -o main
without flags.
Create an alias: alias g++='g++ -std=c++11' should do the trick.
(However, the version of GCC that comes with OS X is so ancient that it doesn't support C++11, you'd be better off using clang and clang++.)
I know this already has an accepted but I feel like I have some advice to offer. For one you should be using a makefile for c++, this is the one I use for answering on SO.
CFLAGS=-std=c++11
CFLAGS+=-stdlib=libc++
CC=clang++
#flags for test.c
cc=clang
DEBUG=-g
#warnings
WARNINGS=-Weverything
#always have -Weverything on for SO lol
OPT= -O0 -O1 -O2 -O3 -O4
test: test.cpp
$(info set CC for compiler)
$(CC) $(CFLAGS) $< -o $# $(DEBUG)
stack: stack.cpp
$(CC) $(CFLAGS) stack.cpp -o $# $(DEBUG) $(WARNINGS)
testc: test.c
$(cc) $< -o $# $(DEBUG)
clean:
rm test
Now whenever I download someones crappy code from SO I have a makefile for c and c++ files where I can easily change the flags if I want to.
As for bash alias I would suggest you alias it like so alias clang++11='clang++ -std=c++11 this way you don't overwrite the clang++ if you don't want to use the c++11 standard. Lastly you can add the line I just showed you to your .bash_profile on a mac which is in your home or ~ folder, this will make the change permanent. Once you change it run source .bash_profile to put the changes into effect. On linux I think the file is called .bashrc. Hopefully these tips will help you out when ur c++ing, I would advise you to learn the mac command line, has differences from the linux one, it can be very useful to know some of the things it can do.