# Write and run C++ code in Mathematica ?

**URL:** <https://community.wolfram.com/t/write-and-run-c-code-in-mathematica/13169>\
**Category:** Questions\
**Tags:** mathematica, compilation, external-interfaces, import-and-export, packages-and-paclets\
**Created:** [October 11, 2018, 8:33pm UTC](https://community.wolfram.com/t/write-and-run-c-code-in-mathematica/13169 "2018-10-11T20:33:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dcvoinescu](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/dcvoinescu/32/13174_2.png) [@dcvoinescu](https://community.wolfram.com/u/dcvoinescu)\
**Post date:** [October 11, 2018, 8:33pm UTC](https://community.wolfram.com/t/write-and-run-c-code-in-mathematica/13169/1 "2018-10-11T20:33:34Z")

</div>

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

```

Thank you,  
🙂

---

<div class="post-metadata">

**Author:** ![kmartin](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/kmartin/32/38196_2.png) [@kmartin](https://community.wolfram.com/u/kmartin)\
**Post date:** [October 11, 2018, 11:50pm UTC](https://community.wolfram.com/t/write-and-run-c-code-in-mathematica/13169/2 "2018-10-11T23:50:48Z")

</div>

As a start, does “/Applications/Xcode 2.app/Contents/Developer” exist on your Mac? (starting with what the error message hints at)

What do

```
CCompilers[]
$CCompilerDefaultDirectory

```

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.

---

<div class="post-metadata">

**Author:** ![dcvoinescu](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/dcvoinescu/32/13174_2.png) [@dcvoinescu](https://community.wolfram.com/u/dcvoinescu)\
**Post date:** [October 12, 2018, 5:27am UTC](https://community.wolfram.com/t/write-and-run-c-code-in-mathematica/13169/3 "2018-10-12T05:27:47Z")

</div>

Hi,

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}}

and 

In[6]:= $CCompilerDefaultDirectory

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

```

Thank you,  
🙂

---

<div class="post-metadata">

**Author:** ![szhorvat](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/szhorvat/32/3531_2.png) [@szhorvat](https://community.wolfram.com/u/szhorvat)\
**Post date:** [October 12, 2018, 12:38pm UTC](https://community.wolfram.com/t/write-and-run-c-code-in-mathematica/13169/4 "2018-10-12T12:38:51Z")

</div>

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?
