1

I'm having trouble getting the library working on macosx. First off, I tried to compile the following code, saved as rand.cpp, taken from the c++ website

#include <iostream>
#include <random>

int main()
{
     const int nrolls=10000;  // number of experiments
     const int nstars=100;    // maximum number of stars to distribute

     std::default_random_engine generator;
     std::normal_distribution<double> distribution(5.0,2.0);

     int p[10]={};

     for (int i=0; i<nrolls; ++i) {
         double number = distribution(generator);
         if ((number>=0.0)&&(number<10.0)) ++p[int(number)];
     }

     std::cout << "normal_distribution (5.0,2.0):" << std::endl;

     for (int i=0; i<10; ++i) {
          std::cout << i << "-" << (i+1) << ": ";
          std::cout << std::string(p[i]*nstars/nrolls,'*') << std::endl;
     }

     return 0;
}

Upon running this with g++ rand.cpp -o rand i get the following errors

rand.cpp:9: error: ‘default_random_engine’ is not a member of ‘std’
rand.cpp:10: error: ‘normal_distribution’ is not a member of ‘std’

Searching around it seems to be suggested that the issue is the compiler, apparently thus library is only available to gcc11. I found a way to update gcc using the macport package as shown here Update GCC on OSX but I still don't know how to use this new compiler. Running g++ rand.cpp -o rand returns the same errors even when I change the compiler with sudo port select --set gcc gcc40 or sudo port select --set gcc mp-gcc46. I also tried using g++ -std=c++11 rand.cpp -o rand which just returns

cc1plus: error: unrecognized command line option "-std=c++11"

Does anyone know what I am doing wrong?

Community
  • 1
  • 1
mrkprc1
  • 37
  • 1
  • 3

1 Answers1

1

Try it with Clang++, which should be installed in your mac, or a new version of GCC.

  • gcc42: I had this version installed, it didn't work, and didn't recognize -std=c++0x and -std=c++11.
  • gcc49: Installed this with brew, it gave the same error but -std=c++11 made it work.
  • Clang++: Worked like a charm without even specifying the standard (it probably defaults to c++11).

Also, check if you have the latest version of the command line tools, if not, download them from the Downloads for Apple Developers website.


What you're doing wrong

The version you installed doesn't have the -std=c++11 option, but it should work with -std=c++0x or -std=gnu++0x, that's what it says in the manual for the 4.6 version.

NiñoScript
  • 4,523
  • 2
  • 27
  • 33