1
$\begingroup$

After not using Mathematica 10.0.2.0 for a few months (I think since december 2018) on my MacBook Pro from 2013 (now with macOS Mojave 10.14.5) I am suddenly facing issues with Compile. I've installed the latest Xcode and agreed to the license terms, but I still get the error:

CCompilerDriver`CreateLibrary::cmperr: "Compile error: clang: error: linker command failed with exit code 1 (use -v to see invocation)

Compile::nogen: A library could not be generated from the compiled function.

when I try to do even a trivial

Compile[{{x, _Real}}, x*x, CompilationTarget -> "C"]

Any tips on how to debug or a known solution?

EDIT:

After doing

Compiler`$CCompilerOptions = {"ShellOutputFunction" -> Print};

I see the message

clang: warning: libstdc++ is deprecated; move to libc++ with a minimum deployment target of OS X 10.9 [-Wdeprecated]

ld: library not found for -lstdc++

I'm guessing this last error is (part of) the crux of the matter.

EDIT 2:

I tried to change the compiler from clang to gcc but I don't think Compile noticed (?): I did

$CCompiler = CCompilerDriver`GCCCompiler`GCCCompiler;
DefaultCCompiler[]

CCompilerDriver`GCCCompiler`GCCCompiler

but when I run the Compile line above, I still get clang-related errors...

$\endgroup$
  • $\begingroup$ If the XCode installation is younger than Mathematica's then Mathematica might have a problem with detecting it correctly. Have you tried to kust reinstall Mathematica? $\endgroup$ – Henrik Schumacher Jul 22 at 14:20
  • 1
    $\begingroup$ @HenrikSchumacher, no worries, thanks for reading my post :) Here's hoping Szabolcs will see the post, I have a hunch he has been dealing with this :P $\endgroup$ – Marius Ladegård Meyer Jul 22 at 14:35
  • 1
    $\begingroup$ Best way to make sure that he reads it: @Szabolcs! ;) $\endgroup$ – Henrik Schumacher Jul 22 at 14:54
  • 3
    $\begingroup$ This is due to Apple removing libstdc++. You may need to change CCompilerDriver`ClangCompiler`Private`dllLinkLibs["MacOSX-x86-64"] to use -lc++ instead of -lstdc++ $\endgroup$ – ilian Jul 22 at 15:28
  • 2
    $\begingroup$ Could be done in init.m I think, just load CCompilerDriver first $\endgroup$ – ilian Jul 22 at 17:13
1
$\begingroup$

Summarizing the comments into an answer:

As of version 10.4.0, CCompilerDriver will link to libc++ on Mac by default.

Earlier versions, however, link to libstdc++ and will run into this problem, because that library is no longer available (Apple deprecated it in Xcode 8 and finally removed it in Xcode 10).

The issue can be worked around by setting the following variables, used when linking executables and dynamic libraries respectively

Needs["CCompilerDriver`"]

CCompilerDriver`ClangCompiler`Private`dllLinkLibs["MacOSX-x86-64"] = "-lc++";
CCompilerDriver`ClangCompiler`Private`exeLinkLibraries["MacOSX-x86-64"] = "-lm -lpthread -lc++";

It may be convenient to place the above code in the kernel's init.m file so compilation works in every session.

$\endgroup$

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.