What is the right way to add <random> header file? - c++

I am trying to generate 64-bit random numbers with the use of mersenne_twister_engine but when I try to include #include <random>, the compiler gives me a warning shown below
/usr/include/c++/4.6/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the upcoming ISO C++ standard, C++0x. This support is currently experimental, and must be enabled with the -std=c++0x or -std=gnu++0x compiler options. make: * [fuse.o] Error 1
How can I fix this?

... and must be enabled with the -std=c++0x or -std=gnu++0x compiler options.
Did you not read that bit? You need to add one of those to your compiler command line (or, if you're using an IDE, whatever method your IDE uses to set the options). Details on C++11 support in gcc can be found here.
For example (command line compiling), if your current command is
g++ -o myprog myprog.cpp
you would change it to something like
g++ -std=c++0x -o myprog myprog.cpp

random is introduced in C++11 so add this to your g++ option:
--std=c++0x
or
--std=gnu++0x
The option is probably in your makefile.

Related

Mutex and condition_variable compiling error with g++ [duplicate]

I'm trying to update my C++ compiler to C++11.
I have searched a bit and I have come to the conclusion that I have to use the flag -std=c++0x or -std=gnu++0x, but I don't know many things about flags. Can anyone help me? (I'm using Ubuntu 12.04.)
Here is the error that I get from the compiler when I attempt to use a library which is included in C++11 (i.e. array):
#include <array>
#include <iostream>
int main()
{
std::array<int, 3> arr = {2, 3, 5};
...
}
This file requires compiler and library support for the upcoming ISO C++ standard, C++0x. This support is currently experimental, and must be enabled with the -std=c++0x or -std=gnu++0x compiler options.
Flags (or compiler options) are nothing but ordinary command line arguments passed to the compiler executable.
Assuming you are invoking g++ from the command line (terminal):
$ g++ -std=c++11 your_file.cpp -o your_program
or
$ g++ -std=c++0x your_file.cpp -o your_program
if the above doesn't work.
You can check your g++ by command:
which g++
g++ --version
this will tell you which complier is currently it is pointing.
To switch to g++ 4.7 (assuming that you have installed it in your machine),run:
sudo update-alternatives --config gcc
There are 2 choices for the alternative gcc (providing /usr/bin/gcc).
Selection Path Priority Status
------------------------------------------------------------
0 /usr/bin/gcc-4.6 60 auto mode
1 /usr/bin/gcc-4.6 60 manual mode
* 2 /usr/bin/gcc-4.7 40 manual mode
Then select 2 as selection(My machine already pointing to g++ 4.7,so the *)
Once you switch the complier then again run g++ --version to check the switching has happened correctly.
Now compile your program with
g++ -std=c++11 your_file.cpp -o main
You can refer to following link to know which features are supported in which version of compiler. It has an exhaustive list of feature support in modern compilers. Seems like GCC follows the standard very closely and implements before any other compiler.
Regarding your question, you can compile using
g++ source_file.cpp -o executable_name -std=c++11 for C++11
g++ source_file.cpp -o executable_name -std=c++14 for C++14
g++ source_file.cpp -o executable_name -std=c++17 for C++17
g++ source_file.cpp -o executable_name -std=c++2a for C++20, All the features of C++20 are not yet supported. Refer to this link for feature support list in GCC.
The list changes pretty fast, keep an eye on the list, if you are waiting for a particular feature to be supported.
Your Ubuntu definitely has a sufficiently recent version of g++. The flag to use is -std=c++0x.
If you want to keep the GNU compiler extensions, use -std=gnu++0x rather than -std=c++0x. Here's a quote from the man page:
The compiler can accept several base standards, such as c89 or c++98,
and GNU dialects of those standards, such as gnu89 or gnu++98. By
specifying a base standard, the compiler will accept all programs
following that standard and those using GNU extensions that do not
contradict it. For example, -std=c89 turns off certain features of GCC
that are incompatible with ISO C90, such as the "asm" and "typeof"
keywords, but not other GNU extensions that do not have a meaning in
ISO C90, such as omitting the middle term of a "?:" expression. On the
other hand, by specifying a GNU dialect of a standard, all features
the compiler support are enabled, even when those features change the
meaning of the base standard and some strict-conforming programs may
be rejected. The particular standard is used by -pedantic to identify
which features are GNU extensions given that version of the standard.
For example-std=gnu89 -pedantic would warn about C++ style //
comments, while -std=gnu99 -pedantic would not.
Use -std=c++11 compiler flag for ISO C++11.
For more details on C++ compiler flags and options, check this.

Avoid compilation ISO C++

By default, on Ubuntu, when I try to compile with g ++ like:
g++ -o name file.cpp
Sometimes it responds with "ISO C++ forbids..."
If I don't want to compile with ISO, what flag should I use?
use
g++ -std=c++0x
or
g++ -std=gnu++0x
Note: If you proprietary enable extensions, your code will become less portable.
C++11 features are available as part of the "mainline" GCC compiler in the trunk of GCC's Subversion repository and in GCC 4.3 and later. To enable C++0x support, add the command-line parameter -std=c++0x to your g++ command line. Or, to enable GNU extensions in addition to C++0x extensions, add -std=gnu++0x to your g++ command line. GCC 4.7 and later support -std=c++11 and -std=gnu++11 as well.
See http://gcc.gnu.org/projects/cxx0x.html
If you want to use gcc/g++ extensions, then use -std=gnu++0x. Assuming of course it's not a case of "you can't actually do this in C++".

Compile code to a specific C/C++ standard

I have tried googling this but all I get is results on how to compile a basic program.
Is it possible to compile code to a specific C++ standard using Clang++ and G++ by specifying that code should be compiled to say, C89, C99, C++98, etc?
You can use the -std flag. For example, to compile to C99, use -std=c99
The documentation for it is here
Use the -std flag like this:
g++ -std=c++98 -o myprog myprog.cpp -lfoo
Here is a man page with plenty of GCC/G++ options, including this one.

g++ (MinGW), C++11 and SSE

When I try to compile the next simple example of code:
echo "#include <cmath>" | g++ -x c++ -c - -m64 -mfpmath=both -std=gnu++11 -o /dev/null
(along with -m64 option is activated (by default) a using of SSE (say, AVX)).
It is appear the following error message:
In file included from <stdin>:1:0:
c:\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.2/../../../../include/c++/4.7.2/cmath:1040:11: error: '::double_t' has not been declared
c:\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.2/../../../../include/c++/4.7.2/cmath:1041:11: error: '::float_t' has not been declared
Is this a bug?
Without -mfpmath=both option an error does not happen.
My workaround is to add the -D__FLT_EVAL_METHOD__=2 (and -Wp,-w if -Werror is present, because of "is redefined" warning) to g++'s option list. I think that this is a dirty way.
This error is in wrong commit on mingw-w64 trunk. I wrote a bug report for this. Thanks!

gnu gcc How to suppress warning: ‘typedef’ was ignored in this declaration [enabled by default]

I am using GNU gcc 4.6.2 on Fedora 16. I am writing an application using a 3rd party API, after compilation, I got a lot warnings.
warning: ‘typedef’ was ignored in this declaration [enabled by default]
Just wondering how can I suppress this? I compile my program with -Wall flag.
In this doc, http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html, it mentioned something like -Wunused-local-typedefs.
I have tried -Wno-unused-local-typedefs, but doesn't work.
Thanks.
-Wno-unused-local-typedefs works in GCC 4.8.
gcc allows you to specify that certain library include paths should be treated as system libraries with the -isystem switch which allows those headers special treatment with respect to the flags you use on the rest of your code. So for example if you have unused local typedefs from using certain Boost libraries in test.cpp (I ran into this using including the Boost signals2 library recently)
g++ -o test{,.cpp} -Wall -Wextra -Werror -I /usr/local/boost-1.55.0/include -L /usr/local/boost-1.55.0/lib
and the above does not build cleanly try the following
g++ -o test{,.cpp} -Wall -Wextra -Werror -isystem /usr/local/boost-1.55.0/include -L /usr/local/boost-1.55.0/lib
which will (provided the warnings coming from the Boost libraries you are including in test.cpp are your only problem of course).
According to the gcc-source-code(gcc/cp/decl.c:4108):
warning (0, "%<typedef%> was ignored in this declaration");
There is no command line flag(that is what the 0 stands for) to suppress this warning in gcc 4.6.2.
As -Wunused-local-typedefs is part of -Wall, make sure you don't have -Wall after -Wno-unused-local-typedefs. If you do, -Wall just turns the option back on again.
In C++17, you should use [[maybe_unused]].
For an overview of all attributes, please see http://en.cppreference.com/w/cpp/language/attributes.
Proposal: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0212r0.pdf
(sorry, I could't post an example, as it's considered as badly indented by stackoverflow)
This GCC warning means that your typedef maybe duplicated and you should remove typedef keyword instead. For example:
typedef enum class Something {
THING1,
THING2,
} Something;
This code above is type duplicate, because enum class is defined as type already. So you must remove typedef keyword as well as Something at the end too!