GCC已安装。 Mathematica仍然不会编译为C语言

时间:2011-06-30 15:45:27

标签: c gcc compiler-construction wolfram-mathematica mathematica-8

我在MacOSX上运行Mathematica 8,尝试将最简单的程序编译为C.任何与C有关的东西都不能在Mathematica中运行。我安装了GCC 4.2;我甚至用XCode多次重新安装它。这就是我正在做的以及我得到的错误:

首先,我总是评估命令

Needs["CCompilerDriver`"]

如果我将编译目标设置为C,

c = Compile[ {{x}}, x^2 + Sin[x^2], CompilationTarget -> "C"];

我收到一条错误: Compile :: nogen:无法从已编译的函数创建库。

如果我尝试创建一个库,

demoFile = FileNameJoin[{$CCompilerDirectory,"SystemFiles","CSource","createDLL_demo.c"}];
lib = CreateLibrary[{demoFile},"testLibrary"]

我收到消息 $ Failed 。 Wolfram说这是因为我没有安装C编译器。我觉得很难相信,因为当我跑步时

CCompilers[]

它告诉我我已经安装了GCC: {{“Name” - > “海湾合作委员会”,   “编译器” - > CCompilerDriver'GCCCompiler`GCCCompiler,   “CompilerInstallation” - > “/ usr / bin”,“CompilerName” - >自动}}

更重要的是,终端说我也安装了GCC !!任何帮助,将不胜感激。我真的很想将Mathematica编译成C。

3 个答案:

答案 0 :(得分:12)

在这个答案中,我将针对类似问题收集一些调试步骤,以供将来参考。随意编辑/改进它们。

如果编译为C代码,则无法使用Mathematica 8,

  1. 检查您是否安装了supported C编译器,它是否正常工作(显而易见)。

    请注意,编译器不一定必须在PATH中,至少在Windows / Visual Studio上不是。

  2. 检查Mathematica是否识别编译器

    << CCompilerDriver`
    CCompilers[]
    

    将列出Mathematica已知的编译器。

  3. Check what commands Mathematica executes to compile the generated C code

    Compiler`$CCompilerOptions = {"ShellCommandFunction" -> Print};
    Compile[{{x}}, x^2, CompilationTarget -> "C"];
    

    请注意,对于"ShellCommandFunction" -> Print,命令将不会执行,因此在完成此步骤后,您需要将Compiler`$CCompilerOptions重新设置为{},以便再次执行命令。 / p>

  4. Check the output/errors from the compiler:

    Compiler`$CCompilerOptions = {"ShellOutputFunction" -> Print};
    Compile[{{x}}, x^2, CompilationTarget -> "C"];
    
  5. 这最后两个步骤有望为您提供足够的线索继续进行。使用此信息,您可以检查是否将正确的库/包含路径传递给编译器(在gcc / icc的情况下,查看指定库路径的-L选项和指定的-I选项包括路径)。然后检查这些路径中是否存在所需的包含和库文件。

答案 1 :(得分:3)

如果你得到Compile :: nogen,你可以通过在Compile表达式中设置ShellOutputFunction-&gt; Print来看到编译器输出:

c = Compile[ {{x}}, x^2 + Sin[x^2], 
   CompilationTarget -> {"C", "ShellOutputFunction"->Print}];

通常,通过将CompilationTarget-&gt;“C”更改为CompilationTarget-&gt; {“C”,options},可以将选项传递给基础CreateLibrary调用。设置编译器`$ CCompilerOptions也有效,但这种技术的优点是不设置全局变量。

答案 2 :(得分:1)

遗憾的是,您看到的唯一错误是 $ Failed ,这并不是非常有用;我想知道是否有一些文件或目录权限问题?

我在Linux上运行而不是Mac,所以我不确定我的设置是否“足够接近”。在我的机器上,Compile命令成功并在我的主目录中生成文件.Mathematica/ApplicationData/CCompilerDriver/BuildFolder/blackie-desktop-5077/compiledFunction1.so。有没有什么办法可以找到与你的用户ID关联的.Mathematica目录,看看它是否存在且可以由mathematica写入?

此外,您可以通过在致电/usr/bin/gcc之前和之后检查Compile的文件访问时间来查看是否正在访问“gcc”。从操作系统shell,您可以ls -lu /usr/bin/gcc或从Mathematica Import["!ls -lu /usr/bin/gcc", "Text"]