【问题标题】:GCC installed. Mathematica still won't compile to C安装了 GCC。 Mathematica 仍然无法编译为 C
【发布时间】:2011-06-30 15:45:27
【问题描述】:

我在 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 : A library could not be created from the compiled function.

如果我尝试创建一个库,

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

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

CCompilers[]

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

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

【问题讨论】:

  • 文档说 MMA 已经用 GCC 4.0 进行了测试。但是,我不希望微小的版本差异成为您问题的根源。你检查demo文件是否存在?
  • 是的,演示文件存在于这个位置:/Applications/Mathematica.app/AddOns/Applications/CCompilerDriver/\SystemFiles/CSource/createDLL_demo.c
  • @sjoerd 这不是 gcc 版本,例如在我的,Import["!gcc --version", "Text"]i686-apple-darwin10-gcc-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5666) (dot 3)(后面是版权通知)
  • 什么版本的 Mathematica,什么版本的 OS X?它适用于 V8.0.1 和 OS X 10.6.8。 (我的 GCC 版本与 acl 的版本相同。)我假设您可以成功编译一个简单的“hello world”程序?
  • 我刚刚在终端中使用 gcc 编译并运行了一个“hello world”程序。它工作得很好。我正在运行 OSX 10.6.7 和 V8.0.0。

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


【解决方案1】:

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

如果无法从 Mathematica 8 编译成 C 代码,

  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" -&gt; Print 将不会执行命令,因此您需要在此步骤完成后将Compiler`$CCompilerOptions 重新设置为{} 以允许再次执行命令。

  4. Check the output/errors from the compiler:

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

这最后两个步骤有望为您提供足够的线索以继续进行。使用此信息,您可以检查是否将正确的库/包含路径传递给编译器(在 gcc/icc 的情况下,查看指定库路径的 -L 选项和指定包含路径的 -I 选项)。然后检查这些路径中是否存在所需的包含和库文件。

【讨论】:

  • 忍受我;这对我来说变得陌生了。我运行了ShellCommandFunction,它给了我两个-I 和两个-L 路径。我不知道他们应该是什么,所以我不知道他们是否错了。唯一让我印象深刻的是第一条路径-I"/Applications/Mathematica.app/SystemFiles/IncludeFiles/C" 没有包含任何文件。我的预感是这里应该有一些东西。我之前提到过,但我会再写一遍。 ShellOutputFunction 告诉我没有 WolframLibrary.h 和 WolframCompileLibrary.h 标头。此时,我应该重新安装 Mathematica 吗?
  • 我刚刚重新安装了 Mathematica。现在我有了头文件。我想我的原始安装不完整。感谢您的所有帮助。
  • 我猜“I”路径是编译器搜索包含文件的路径(.h),“L”路径是库路径,供链接器搜索附加链接代码的库。
  • 感谢@Leonid,我在答案中更明确地说明了这一点。
  • @Szabolcs 你介意看看我的问题吗?非常相似,但在 Windows 8 上。而且我不确定如何使用第 3 点和第 4 点的信息......任何帮助将不胜感激。 stackoverflow.com/questions/26518086/…
【解决方案2】:

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

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

通常,通过将 CompilationTarget->"C" 更改为 CompilationTarget->{"C", options},您可以将选项传递给底层的 CreateLibrary 调用。设置 Compiler`$CCompilerOptions 也可以,但这种技术的优点是不设置全局变量。

【讨论】:

    【解决方案3】:

    很遗憾,您看到的唯一错误是 $Failed,这并没有多大帮助;我想知道是否可能有一些文件或目录权限问题?

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

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

    【讨论】:

    • 我找到了一个目录/Users/Noon/Library/Mathematica/ApplicationData/CCompilerDriver/BuildFolder/goble-1586,每个人都可以写。 CCompilerDirectory 中似乎没有文件。应该有吗?上面刚刚提到 Mathematica 找不到 WolframLibrary.h 和 WolframCompileLibrary.h 头文件。这可能是问题吗? Mathematica 正在访问 GCC。 /usr/bin/gcc -> gcc-4.2
    • 也许您的安装不完整,或者损坏了?在我的机器上,$CCompilerDirectory 有几个“.m”文件和三个目录(“Documentation”、“Kernel”和“SystemFiles”)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-28
    • 2017-08-08
    • 2015-06-17
    • 2019-06-05
    • 1970-01-01
    相关资源
    最近更新 更多