3
$\begingroup$

I'm trying to run this example from the Wolfram website, on Linux Mint 16.

Needs["CCompilerDriver`"];
hello = CreateExecutable["
    #include <stdio.h>
    int main(){
      printf(\"hello world\\n\");
    }
    ", "hello"]
Import["!" <> QuoteFile[hello], "Text"]

But at the CreateExecutable call I get the message

CreateExecutable::cmperr: Compile error: collect2: error: ld returned 1 exit status

And the code returns $Failed (plus some error messages from the Import call, that depends on CreateExecutable working).

$\endgroup$
8
  • $\begingroup$ Prints Hello world on Ubuntu 15.10 (Linux Mint 16 is based on Ubuntu 13.10). Did you install a c compiler? What does CCompilers[] return? $\endgroup$
    – anderstood
    Mar 4, 2017 at 22:40
  • $\begingroup$ I use GCC outside of Mathematica and CCompilers[Full] lists GCC. $\endgroup$
    – gypsophila
    Mar 4, 2017 at 22:42
  • $\begingroup$ What if you remove the printf line and then the #include line? $\endgroup$
    – anderstood
    Mar 4, 2017 at 22:57
  • 2
    $\begingroup$ When something goes wrong with compilation, the very first thing to do is to look at the compilation command line and the compiler output. Add the options "ShellCommandFunction" -> Print and "ShellOutputFunction" -> Print. The output will tell you what is wrong. $\endgroup$
    – Szabolcs
    Mar 4, 2017 at 23:00
  • 2
    $\begingroup$ Seems more like a c++ concern than a MMA's. $\endgroup$
    – anderstood
    Mar 4, 2017 at 23:21

1 Answer 1

3
$\begingroup$

I added the options "ShellCommandFunction" -> Print and "ShellOutputFunction" -> Print to CreateExecutable, they print the output of the compilation to Mathematica, thus it allows debugging.

On the output message there was the line /usr/bin/ld: cannot find -lstdc++, which indicates an issue with the compiler. I installed the C++ compiler (G++) following these instructions and the code worked.

With the options added, the code looks like:

Needs["CCompilerDriver`"];
hello = CreateExecutable["
    #include <stdio.h>
    int main(){
      printf(\"hello world\\n\");
    }
    ", "hello",
    "ShellCommandFunction" -> Print,"ShellOutputFunction" -> Print]
Import["!" <> QuoteFile[hello], "Text"]
$\endgroup$
1
  • $\begingroup$ Did you update gcc or did you install a C++ compiler in addition to the C compiler than you already had? From the error it appears that the latter is needed. While the code is plain C (not C++), typical uses of Mathematica's compilation functions necessitate linking against libraries that in turn depend on the C++ standard library. Thus CreateExecutable will by default try to link against libstdc++ even when compiling only C code. $\endgroup$
    – Szabolcs
    Mar 5, 2017 at 17:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.