DeclareCompiledComponent["name",decls]
将 decls 添加到已编译组件 "name" 中.
DeclareCompiledComponent["name",fieldspec]
将规范 spec 添加到已编译组件 "name" 的指定字段中.
DeclareCompiledComponent
DeclareCompiledComponent["name",decls]
将 decls 添加到已编译组件 "name" 中.
DeclareCompiledComponent["name",fieldspec]
将规范 spec 添加到已编译组件 "name" 的指定字段中.
更多信息
- 已编译组件可用于表示一组已命名的编译器声明.
- 已编译组件可用于表示可内置到共享库中的编译功能.
- 可将已编译组件嵌入到小数据包中.
- DeclareCompiledComponent["name",...] 会创建已编译组件 "name",如果它尚不存在的话.
- 可通过 CreateCompilerEnvironment 和 CompilerEnvironmentAppendTo 将已编译组件的声明添加到编译器环境中.
- 在构建已安装函数和库函数时会使用已编译组件的声明.
- 可用 BuildCompiledComponent 构建包含已安装函数和库函数的库.
- 可用 LoadCompiledComponent 加载包含已安装函数和库函数的库.
- 可给出以下 field 值:
-
"Declarations" 与组件一起使用的声明 "InstalledFunctions" 要编译的函数和要将结果嵌入的符号 "LibraryFunctions" 要编译的函数,由 LoadCompiledComponent 恢复 "RawLibraryFunctions" 要编译的可作为库导出的函数 "LoadingEpilogs" LoadCompiledComponent 加载组件后要运行的函数 "ExternalLibraries" LoadCompiledComponent 加载组件之前要加载的库 - 可用单个声明或声明列表给出 "Declarations".
- 可用规则 symfunc 给出 "InstalledFunctions",指定在加载构建的组件时 func 的编译版本将被安装到符号 sym 中. "InstalledFunctions" 也可以接受规则关联.
- 也可将符号 sym 赋给 "InstalledFunctions",它实际上相当于声明已安装函数 symsym. 如果 sym 是一个可编译函数的名称(包括在组件中定义的函数),那么它将被编译并安装到符号 sym 中,以便在编译代码之外使用. 也可以给出一个符号列表. »
- 可用规则 "name"func 给出"LibraryFunctions",指定 func 的已编译版本将出现在 LoadCompiledComponent 的结果中,且名为 "name". 也可用关联给出 "LibraryFunctions".
- "RawLibraryFunctions" 项以规则 "name"Typed[funcName,ty] 的形式给出. 这将指定该组件构建的库将导出类型为 ty 的 funcName 的编译版本. 如果编译使用了带有 CompiledComponentRawInterface 的组件,则可以直接引用 funcName.
- 可用单个函数或函数列表给出 "LoadingEpilogs".
- 可用单个库或库规范列表给出 "ExternalLibraries". 库规范必须可被 FindLibrary 解析.
- "InstalledFunctions" 和 "LibraryFunctions" 字段包含要编译的源代码. BuildCompiledComponent 对源进行构建并将结果保存到动态库中. 可用 LoadCompiledComponent 加载动态库,以恢复编译结果.
- "RawLibraryFunctions" 的名称受动态库的格式限制.
范例
打开所有单元 关闭所有单元基本范例 (2)
DeclareCompiledComponent["demo", FunctionDeclaration[square, Typed[{"Real64"} -> "Real64"]@Function[x, x ^ 2]]];CompiledComponent["demo"]在对 FunctionCompile 的调用中使用 "demo":
comp = FunctionCompile[CompiledComponent["demo"], Function[Typed[arg, "Real64"], square[arg]]]comp[10]DeleteObject[CompiledComponent["demo"]]DeclareCompiledComponent["demo1", FunctionDeclaration[square, Typed[{"Real64"} -> "Real64"]@Function[{x}, x ^ 2]]];CompiledComponent["demo1"]CompiledComponent["demo1"]["Properties"]CompiledComponent["demo1"]["Declarations"]范围 (5)
DeclareCompiledComponent["fullComponent", {
FunctionDeclaration[return12, Typed[{} -> "MachineInteger"]@Function[{}, 12]],
FunctionDeclaration[addtwo, Typed[{"MachineInteger"} -> "MachineInteger"]@Function[x, x + 2]]
}];DeclareCompiledComponent["fullComponent", "LibraryFunctions" -> <|
"libFunc" -> Function[Typed[arg, "Real64"], Sqrt[arg]]
|>];DeclareCompiledComponent["fullComponent", "InstalledFunctions" -> addtwo];CompiledComponent["fullComponent"][All]CompiledComponent["fullComponent"]["Properties"]CompiledComponent["fullComponent"]["Declarations"]DeleteObject[CompiledComponent["fullComponent"]]DeclareCompiledComponent 可以提供编译成库的函数:
DeclareCompiledComponent["libDemo", "LibraryFunctions" -> <|
"fun1" -> Function[{Typed[arg, "Real64"]}, arg ^ 2]
|>];BuildCompiledComponent["libDemo"]lib = LoadCompiledComponent["libDemo"]lib["LibraryFunctions"]["fun1"][10.5]DeclareCompiledComponent 可定义符号来获取已安装的代码:
DeclareCompiledComponent["demoInstFunc", "InstalledFunctions" -> <|
square -> Function[Typed[arg, "Real64"], arg ^ 2]
|>];DeclareCompiledComponent["demoInstFunc", FunctionDeclaration[double, Typed[{"Real64"} -> "Real64"]@Function[arg, arg * 2]]];DeclareCompiledComponent["demoInstFunc", "InstalledFunctions" -> {
double
}];BuildCompiledComponent["demoInstFunc"]double[12.3]square[12.3]DeclareCompiledComponent 可以定义符号来获取安装代码:
DeclareCompiledComponent["demoInstFunc", "InstalledFunctions" -> <|
square -> Function[Typed[arg, "Real64"], arg ^ 2]
|>];DeclareCompiledComponent["demoInstFunc", FunctionDeclaration[double, Typed[{"Real64"} -> "Real64"]@Function[arg, arg * 2]]];DeclareCompiledComponent["demoInstFunc", "InstalledFunctions" -> {
double
}];BuildCompiledComponent["demoInstFunc"]double[12.3]square[12.3]DeclareCompiledComponent 可以创建一个原始库接口,供其他编译代码使用:
DeclareCompiledComponent["demoLibrary", {
FunctionDeclaration[square, Typed[{"Real64"} -> "Real64"]@Function[arg, arg ^ 2]]
}
];
DeclareCompiledComponent["demoLibrary", "RawLibraryFunctions" -> <|
"square" -> Typed[square, {"Real64"} -> "Real64"]
|>];BuildCompiledComponent["demoLibrary"]LoadCompiledComponent["demoLibrary"]libDecl = LibraryFunctionDeclaration[square -> "square", "demoLibrary", {"Real64"} -> "Real64"];cf = FunctionCompile[{libDecl}, Function[{Typed[arg, "Real64"]}, square[arg]]]cf[5.6]不使用 LibraryFunctionDeclaration,而是直接使用组件原始接口:
cf = FunctionCompile[CompiledComponentRawInterface["demoLibrary"], Function[{Typed[arg, "Real64"]}, square[arg]]]cf[5.6]使用 "RawLibraryFunctions" 构建的组件库可与 ForeignFunctionLoad 一起使用:
fun = ForeignFunctionLoad["demoLibrary", "square", {"CDouble"} -> "CDouble"]fun[ 5.6]技术笔记
-
▪
- 编译组件 ▪
- Wolfram 编译器手册
相关指南
-
▪
- 代码编译
文本
Wolfram Research (2022),DeclareCompiledComponent,Wolfram 语言函数,https://reference.wolfram.com/language/ref/DeclareCompiledComponent.html.
CMS
Wolfram 语言. 2022. "DeclareCompiledComponent." Wolfram 语言与系统参考资料中心. Wolfram Research. https://reference.wolfram.com/language/ref/DeclareCompiledComponent.html.
APA
Wolfram 语言. (2022). DeclareCompiledComponent. Wolfram 语言与系统参考资料中心. 追溯自 https://reference.wolfram.com/language/ref/DeclareCompiledComponent.html 年
BibTeX
@misc{reference.wolfram_2026_declarecompiledcomponent, author="Wolfram Research", title="{DeclareCompiledComponent}", year="2022", howpublished="\url{https://reference.wolfram.com/language/ref/DeclareCompiledComponent.html}", note=[Accessed: 15-October-2026]}
BibLaTeX
@online{reference.wolfram_2026_declarecompiledcomponent, organization={Wolfram Research}, title={DeclareCompiledComponent}, year={2022}, url={https://reference.wolfram.com/language/ref/DeclareCompiledComponent.html}, note=[Accessed: 15-October-2026]}