I’d like to write and run some C++ programs (as part of a presentation) from within Mathematica. I’ve seen a simple example in the documentation - using CreateExecutable - but I didn’t succeed. Is it something, maybe settings, that I have to do first?
I am using an almost 5 years old MacBook Pro with Mac OS High Sierra and I write C++ programs using X Code 10.0.
Here’s what I’ve got when I tried the example in the documentation :
In[23]:= Needs["CCompilerDriver`"]
In[24]:= hello = CreateExecutable["
#include <stdio.h>
int main(){
printf(\"hello world\\n\");
}
", "hello"]
During evaluation of In[24]:= CreateExecutable::cmperr: Compile error: xcrun: error: active developer path ("/Applications/Xcode 2.app/Contents/Developer") does not exist >>
Out[24]= $Failed
In[25]:= Import["!" <> QuoteFile[hello], "Text"]
During evaluation of In[25]:= StringJoin::string: String expected at position 2 in !<>QuoteFile($Failed). >>
During evaluation of In[25]:= Import::chtype: First argument !<>QuoteFile($Failed) is not a valid file, directory, or URL specification. >>
Out[25]= $Failed
Does your C++ compiler work at all when using it from a command line?
Have you tried running c++ hello.cpp (or cc hello.c as your program is actually C and not C++) in a terminal window?
Also, when I work with CreateLibrary or CreateExecutable and I get an error, the very first thing I do is to add "ShellCommandFunction" -> Print, "ShellOutputFunction" -> Print as options. This way I can see what exactly is happening.
Finally, why do you have Xcode 2.app instead of Xcode.app? Macs are typically set up in a standard way, so there’s no need to configure compilers explicitly for Mathematica. It would seem that you do not have a standard configuration. What exactly did you change?