已安装海湾合作委员会。 Mathematica 仍然无法编译为 C
我在 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从编译的函数创建。
如果我尝试创建库,
demoFile = FileNameJoin[{$CCompilerDirectory,"SystemFiles","CSource","createDLL_demo.c"}];
lib = CreateLibrary[{demoFile},"testLibrary"]
我会收到一条消息$Failed。 Wolfram 说这是因为我没有安装 C 编译器。我发现这很难相信,因为当我运行时
CCompilers[]
它告诉我我已经安装了 GCC: {{"Name" -> “海湾合作委员会”, “编译器”-> CCompilerDriver'GCCCompiler`GCCCompiler, “编译器安装”-> “/usr/bin”,“编译器名称”->自动}}
而且,终端说我也安装了 GCC!任何帮助将不胜感激。我真的很想将 Mathematica 编译为 C 语言。
I'm running Mathematica 8 on a MacOSX, trying to compile even the simplest program to C. Anything having to do with C simply doesn't work in Mathematica. I have GCC 4.2 installed; I've even reinstalled it multiple times with XCode. Here's what I'm doing and the errors I'm getting:
First, I always evaluate the command
Needs["CCompilerDriver`"]
If I set the compilation target to C,
c = Compile[ {{x}}, x^2 + Sin[x^2], CompilationTarget -> "C"];
I get an error that reads: Compile::nogen : A library could not be created from the compiled function.
If I try to create a library,
demoFile = FileNameJoin[{$CCompilerDirectory,"SystemFiles","CSource","createDLL_demo.c"}];
lib = CreateLibrary[{demoFile},"testLibrary"]
I get an message $Failed. Wolfram says that this is because I don't have a C compiler installed. I find that hard to believe because when I run
CCompilers[]
It tells me that I've got GCC installed: {{"Name" -> "GCC",
"Compiler" -> CCompilerDriver'GCCCompiler`GCCCompiler,
"CompilerInstallation" -> "/usr/bin", "CompilerName" -> Automatic}}
What's more, terminal says I have GCC installed too!! Any help would be appreciated. I'd really like to compile Mathematica to C.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在这个答案中我将收集一些类似问题的调试步骤,以供将来参考。请随意编辑/改进它们。
如果在 Mathematica 8 中无法编译为 C 代码,
请检查您是否有受支持的 C 编译器已安装并且可以工作(显而易见)。
请注意,编译器不一定必须位于
PATH
中,至少在 Windows/Visual Studio 上不需要。检查 Mathematica 是否识别编译器
<前><代码><< C编译器驱动程序`
C编译器[]
将列出 Mathematica 已知的编译器。
检查 Mathematica 执行哪些命令来编译生成的 C 代码:
请注意,使用
"ShellCommandFunction" -> Print
命令将不会被执行,因此您需要在此步骤完成后将Compiler`$CCompilerOptions
重新设置为{}
以允许命令再次执行。检查编译器的输出/错误:
这些最后两个步骤有望为您提供足够的线索来继续。有了这些信息,您可以检查是否将正确的库/包含路径传递给编译器(对于 gcc/icc,请查看指定库路径的
-L
选项和- I
选项指定包含路径)。然后检查这些路径中是否存在所需的包含文件和库文件。In this answer I'll collect some debugging steps for similar problems, for future reference. Feel free to edit/improve them.
If compiling to C code does not work from Mathematica 8,
Check that you have a supported C compiler installed and it works (the obvious).
Note that the compiler does not necessarily have to be in the
PATH
, at least on Windows/Visual Studio it doesn't.Check that Mathematica recognizes the compiler
will list the compilers known to Mathematica.
Check what commands Mathematica executes to compile the generated C code:
Note that with
"ShellCommandFunction" -> Print
the commands will not be executed, so you'll need to re-setCompiler`$CCompilerOptions
to{}
after this step is complete to allow command execution again.Check the output/errors from the compiler:
These last two steps will hopefully give you enough clues to proceed. With this information you can check if the correct library / include paths are passed to the compiler (in the case of gcc/icc, look at the
-L
option which specifies library paths and the-I
option which specifies include paths). Then check if the required include and library files are present at those paths.如果您得到 Compile::nogen,则可以通过在 Compile 表达式中设置 ShellOutputFunction->Print 来查看编译器输出:
一般来说,这就是通过更改 CompilationTarget->" 将选项传递给底层 CreateLibrary 调用的方式。” C" 到 CompilationTarget->{"C", options}。设置 Compiler`$CCompilerOptions 也可以,但这种技术的优点是不设置全局变量。
If you get Compile::nogen, you can see the compiler output by setting ShellOutputFunction->Print right in the Compile expression:
In general, this is how you can pass options to the underlying CreateLibrary call, by changing CompilationTarget->"C" to CompilationTarget->{"C", options}. Setting Compiler`$CCompilerOptions works too, but this technique has the advantage of not setting a global variable.
遗憾的是,您看到的唯一错误是 $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"]
It is a shame that the only error you are seeing is $Failed, that's not terribly helpful; I wonder if perhaps there are some file or directory permissions problems?
I'm running on linux not Mac so I am not sure if my setup is "close enough" or not. On my machine your
Compile
command succeeds and generates a file.Mathematica/ApplicationData/CCompilerDriver/BuildFolder/blackie-desktop-5077/compiledFunction1.so
in my home directory. Is there any way you can find a.Mathematica
directory associated with your userid, and see if it exists and is writeable by mathematica?Also, you could check to see if "gcc" is or is not being accessed by checking the file access time of
/usr/bin/gcc
before and after your call toCompile
. From an operating system shell you can dols -lu /usr/bin/gcc
or from Mathematica perhapsImport["!ls -lu /usr/bin/gcc", "Text"]