如何在Win7(x64)上使用英特尔C++编译器(x86)将Mathematica编译为C代码

How to compile Mathematica to C code with intel C++ compiler (x86) on Win7(x64)?

本文关键字:Mathematica x86 编译器 编译 代码 C++ Win7 x64 英特尔      更新时间:2023-10-16

我的操作系统是windows 7 64位。我安装了Microsoft C++2008速成版和"英特尔C++编译器"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:\...AppDataRoamingMathematicaApplicationDataCCompilerDriverBuildFoldervax-5844compiledFunction5.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。

在第一个代码块中,您指定了所需的编译器。在第二个例子中,你没有——所以可能是Mathematica不"知道"你安装的编译器。

运行Needs["CCompilerDriver`"],然后运行CCompilers[Full],查看Mathematica了解哪些编译器。也可以看看全局$CCompilerDirectory

如果您的英特尔和/或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,英特尔C++v11.1 Windows版编译器已在他们测试过的编译器列表中,因此您不需要安装MS Visual Studio。另一方面,您可以通过MiniGW或CygWin尝试GCC for Windows(另请参阅SO/187990)。