2

I'm trying to use TR1 for some C++ project. Unfortunately I get an error and don't understand why or how I should do it correctly! I'm working under Linux with gcc 4.4.5.

I get the error

myfile.cpp:21:35: error: tr1/normal_distribution: No such file or directory     

The TR1 file I need is imported via:

#include <tr1/normal_distribution>

in CMakeLists.txt I turn on TR1 support (-std=c++0x)

SET (CMAKE_CXX_FLAGS "-Wall -std=c++0x -DNDEBUG -O3 -march=nocona -msse4.2")  

Any idea what I'm doing wrong?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Michael Gg
  • 45
  • 1
  • 6

3 Answers3

6

The flag -std=c++0x gives you access to whatever c++11 functionality is implemented in your version of gcc. For random number distributions, c++11 has the header random. You do not need the tr1 namespace when using c++11.

The tr1 version of random is in include tr1/random, and everything there is under the std::tr1 namespace. To access this you do not need the c++0x flag.

To be clear:

For TR1 random numbers: #include <tr1/random> and use std::tr1::normal_distribution.

For c++11 random numbers: compile with flag c++0x, then #include <random> and use std::normal_distribution.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • Apparantly I need it. If I don't, I get the following error: /usr/include/c++/4.4/c++0x_warning.h:31: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. – Michael Gg Apr 18 '12 at 08:19
  • I don't use normal random numbers but a normal_distribution – Michael Gg Apr 18 '12 at 08:21
  • @MichaelGg that is strange. Are you including (c++11) or ? – juanchopanza Apr 18 '12 at 08:21
  • @MichaelGg the `normal_distribution` is defined in and , the latter needs C++11 support, the former not. – juanchopanza Apr 18 '12 at 08:23
  • #include But in there there is some TR1 stuff it seems, such as: #if defined(_GLIBCXX_INCLUDE_AS_CXX0X) # include #else – Michael Gg Apr 18 '12 at 08:25
  • @MichaelGg I updated my answer to spell it out. TR1 shouldn't need C++11 support. C++11 obviously does :-) – juanchopanza Apr 18 '12 at 08:27
  • @MichaelGg it depends, I use C++11 a lot, but if you want your code to be portable to compilers that don't support it, then you are better off with TR1, at least for the time being. – juanchopanza Apr 18 '12 at 08:37
1

I believe its #include <random>. Anyway, check that.

Bartek Banachewicz
  • 38,596
  • 7
  • 91
  • 135
0

try to add the following flags to cxx on CMakeLists.txt:

set (CMAKE_CXX_FLAGS "-stdlib=libstdc++")

-std=c++0x is not necessary

Kimia Zhu
  • 209
  • 2
  • 2