I am trying to call and run the C++ code at the bottom of this post which must be stored in a file called poissonvariate.cpp. I am getting the following error message which I will display here before discussing how I got to this point.

"Compile error: !(\"clang: error: invalid deployment target for \ -stdlib=libc++ (requires OS X 10.7 or later)\")"

I am running OS X 10.7.4 with XCode 4.5.1 so somewhere a flag is set to 10.6.x or earlier and for some reason Mathematica is finding it. On the command line I can compile and run code with clang++ without difficulty. The same error message is reported here which has nothing to do with Mathematica: https://stackoverflow.com/questions/24243176/how-to-specify-target-mac-os-x-version-using-qmake

I found this line of code in a python config.log file: CONFIGURE_MACOSX_DEPLOYMENT_TARGET='10.3' .

I think this is set with Xcode for application developers but I'm not sure? How to set deployment TARGET TO 10.7?

The C++ code I want to run was originally discussed here: Can Mathematica's random number generation be improved? . The library that the c++ provides can be created with the code below. (If you want to actually run the code go to the link and scroll down to blochwave's answer and grab the lines of code needed to run it.)


PoissonVariateLib = CreateLibrary[{ToString[NotebookDirectory[]] <> 
 "poissonvariate.cpp"}, "PoissonVariateLib", "Debug" -> False,  "CompileOptions" -> "-std=c++11 -O3"];

The errors that I get on Macs occur in the call to CreateLibrary . After playing around a little I came up with this line:

PoissonVariateLib = CreateLibrary[{ToString[NotebookDirectory[]] <>     "poissonvariate.cpp"}, "PoissonVariateLib", "Debug" -> False, "LibraryDirectories" -> "/usr/bin/../lib/clang/4.1", "IncludeDirectories" -> "/usr/lib/c++/v1","CompileOptions" -> "-std=c++11 -stdlib=libc++ -O3",  "CompilerInstallation" -> "/usr/bin/clang++"];

which I had hoped would work. As stated above I am running Mac OS X 10.7.4 with XCode 4.5.1 .

Execution of this line generates the invalid deployment target error at the top of this post.

---------------The C++ code------------------------------------

#include "WolframLibrary.h"
#include "stdio.h"
#include "stdlib.h"
#include <random>
#include <chrono>

EXTERN_C DLLEXPORT mint WolframLibrary_getVersion(){return WolframLibraryVersion;}
EXTERN_C DLLEXPORT int WolframLibrary_initialize( WolframLibraryData libData) {return 0;}
EXTERN_C DLLEXPORT void WolframLibrary_uninitialize( WolframLibraryData libData) {}

EXTERN_C DLLEXPORT int BinomialVariate(WolframLibraryData libData, mint Argc, MArgument *Args, MArgument Res){

int err; // error code

MTensor m1; // input tensor 2
MTensor m2; // input tensor 2
MTensor m3; // output tensor

mint const* dims1; // dimensions of the tensor

double* data1; // actual data of the input tensor
double* data2;
double* data3; // data for the output tensor

mint i; // bean counters

m1 = MArgument_getMTensor(Args[0]);
dims1 = libData->MTensor_getDimensions(m1);
m2 = MArgument_getMTensor(Args[1]);
err = libData->MTensor_new(MType_Real, 1, dims1, &m3);
data1 = libData->MTensor_getRealData(m1);
data2 = libData->MTensor_getRealData(m2);
data3 = libData->MTensor_getRealData(m3);

unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::mt19937 generator (seed); // RNG

for(i=0;i<dims1[0];i++) {
    std::binomial_distribution<int> distribution(data1[i], data2[i]); 
    data3[i] = distribution(generator); 

MArgument_setMTensor(Res, m3);


  • 1
    $\begingroup$ did you try shrx's comment on my answer? namely, "@JEP and others: I got it to work on OS X 10.10.2 with g++ installed with homebrew and adding "CompilerInstallation" -> "/usr/local/bin/g++" to CreateLibrary options. Timings are 0.012565 and 51.336809 for the compiled and native method, respectively." $\endgroup$ Feb 24 '15 at 8:02
  • $\begingroup$ I'm going to try his as soon as I can another laptop (in a day or three) but not on the machine that I am currently using. In any event, this should work with the XCode g++ without installing another one. $\endgroup$
    – JEP
    Feb 24 '15 at 14:05

The reason you probably need to install g++ as blochwave and shrx suggested is because after XCode 4.2 they stopped installing gcc and used Clang instead. So while it acts almost exactly the same, it isn't: see below what happens when I get an error message in Terminal after typing g++ without any input.

 Ryans-Laptop:~ Ryan$ g++ 
 clang: error: no input files
  • $\begingroup$ I have g++ installed. It is a different executable than clang++. The problem is with g++ I can't compile some simple code from the command line that I thought was a prerequisite for getting this to work. ls -l /usr/bin/g++ /usr/bin/g++ -> llvm-g++-4.2 ls -l /usr/bin/clang++ /usr/bin/clang++ -> clang $\endgroup$
    – JEP
    Feb 25 '15 at 16:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.