# Write and run C++ code in Mathematica ?

Posted 2 months ago
529 Views
|
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 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, :)
3 Replies
Sort By:
Posted 2 months ago
 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 = CCompilerDriverGCCCompilerGCCCompiler The example you mention from CCompilerDriver/ref/CreateExecutable works for me with Mathematica 11.3 on macOS Mojave. Answer Posted 2 months ago  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 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->CCompilerDriverClangCompilerClangCompiler,CompilerInstallation->/usr/bin,CompilerName->Automatic},{Name->GCC,Compiler->CCompilerDriverGCCCompiler`GCCCompiler,CompilerInstallation->/usr/bin,CompilerName->Automatic}} and In[6]:=$CCompilerDefaultDirectory Out[6]= /Users/DCV/Library/Mathematica/SystemFiles/LibraryResources/MacOSX-x86-64 Thank you, :)