Message Boards Message Boards


Write and run C++ code in Mathematica ?

Posted 2 months ago
3 Replies
3 Total Likes

Hi there friends,

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") 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

Thank you, :)

3 Replies
Posted 2 months ago

As a start, does "/Applications/Xcode" exist on your Mac? (starting with what the error message hints at)

What do


return on your machine? What if you set $CCompiler to an alternative compiler on your machine, for example perhaps a call like:

$CCompiler = CCompilerDriver`GCCCompiler`GCCCompiler

The example you mention from CCompilerDriver/ref/CreateExecutable works for me with Mathematica 11.3 on macOS Mojave.


Thank you for the answer. Things changed but "I'm not there yet". Please find below the answers to your questions.

I created the paths the system asked for. Now I've got the following:

In[4]:= hello = CreateExecutable["
  #include <stdio.h>
  int main(){
    printf(\"hello world\\n\");
  ", "hello"]

During evaluation of In[4]:= CreateExecutable::cmperr: Compile error: xcrun: error: unable to exec Xcode native xcrun (Permission denied). >>

Out[4]= $Failed

On the other hand:
CCompilers[]    and    $CCompilerDefaultDirectory    return

In[5]:= CCompilers[]

Out[5]= {{Name->Clang,Compiler->CCompilerDriver`ClangCompiler`ClangCompiler,CompilerInstallation->/usr/bin,CompilerName->Automatic},{Name->GCC,Compiler->CCompilerDriver`GCCCompiler`GCCCompiler,CompilerInstallation->/usr/bin,CompilerName->Automatic}}


In[6]:= $CCompilerDefaultDirectory

Out[6]= /Users/DCV/Library/Mathematica/SystemFiles/LibraryResources/MacOSX-x86-64

Thank you, :)

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 instead of 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?

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract