1

我的操作系统是 Windows 7 64 位。我安装了 Microsoft C++ 2008 Express 版和 Intel C++ Compiler v11.1 x86 版。

现在我可以在 Mathematica 中成功编译 x86 C 代码,例如

    In[1]:= Needs["CCompilerDriver`"]
    In[2]:= greeter = CreateExecutable[StringJoin["#include <stdio.h>\n", "int main(){\n", "  printf(\"Hello world.\\n\");\n", "}\n"], "hiworld", 
     "Compiler" -> CCompilerDriver`IntelCompiler`IntelCompiler, 
     "CompilerInstallation" -> "C:\\Program Files (x86)\\Intel\\Compiler\\11.1\\072\\", 
     "CompilerName" -> Automatic, "TargetSystemID" -> "Windows"]

    Out[2]= "C:\\...\\AppData\\Roaming\\Mathematica\\\
       SystemFiles\\LibraryResources\\Windows-x86-64\\hiworld.exe"

但未能用于CompilationTarget -> "C"编译这样的函数

    In[3]:= f = Compile[{x, y}, Sqrt[x^2 + y^2], CompilationTarget -> "C"]

    During evaluation of In[3]:= LibraryFunction::libload: The function compiledFunction5 was not loaded from the file C:\\...\AppData\Roaming\Mathematica\ApplicationData\CCompilerDriver\BuildFolder\vax-5844\compiledFunction5.dll. >>
    During evaluation of In[3]:= Compile::nogen: A library could not be generated from the compiled function. >>

我想我需要指定一个默认值"TargetSystemID"-> "Windows",因为我的平台是 x64,但不知道如何在 Mathematica 中设置这样的选项。

我在这里错过了什么吗?

PS:最近不想安装Microsoft Visual Studio。

4

1 回答 1

1

在您的第一个代码块中,您指定了所需的编译器。在第二个你没有 - 所以可能是 Mathematica 不“知道”你已经安装的编译器。

Needs["CCompilerDriver`"]然后运行CCompilers[Full]以查看 Mathematica 知道哪些编译器。也可以看看全球$CCompilerDirectory

如果您的 Intel 和/或 Microsoft 编译器未显示,请按照CCompilerDriver 用户指南的特定编译器页面进行操作。为了让您的英特尔编译器正常工作,我认为以下内容可能就足够了:

$CCompiler = {
 "Compiler" -> CCompilerDriver`IntelCompiler`IntelCompiler,
 "CompilerInstallation"->"C:\\Program Files (x86)\\Intel\\Compiler\\11.1\\072\\"}

也就是说 - 如果我使用上面的代码将我的编译器的目录更改为错误,那么我得到的第一个错误类型(在 之前 Compile::nogen)是CreateLibrary::instl,而不是LibraryFunction::libload你拥有的消息。您的默认构建目录可能有问题:$CCompilerDefaultDirectory...

为响应您的 PS,适用于 Windows 的英特尔 C++ v11.1 编译器在他们测试过的编译器列表中,因此您不需要安装 MS Visual Studio。另一方面,您可以通过 MiniGW 或 CygWin 尝试适用于 Windows 的GCC (另见SO/187990)。

于 2012-01-22T06:00:57.467 回答