我的操作系统是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。
在您的第一个代码块中,您指定了所需的编译器。在第二个你没有 - 所以 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)。