ForeignFunctionLoad[lib,fun,{argtype1,argtype2,…}rettype]
从库 lib 加载具有指定参数和输出类型的函数 fun.
ForeignFunctionLoad[ptr,{argtype1,argtype2,…}rettype]
根据函数指针 ptr 创建外部函数.
ForeignFunctionLoad
ForeignFunctionLoad[lib,fun,{argtype1,argtype2,…}rettype]
从库 lib 加载具有指定参数和输出类型的函数 fun.
ForeignFunctionLoad[ptr,{argtype1,argtype2,…}rettype]
根据函数指针 ptr 创建外部函数.
更多信息
- ForeignFunctionLoad 返回一个 ForeignFunction 对象.
- lib 由 FindLibrary 解析.
- lib 必须是兼容 C 的动态库.
- 支持的类型通常与 Wolfram Compiler 支持的类型一致.
- 可能的参数和返回类型及其对应的 C 类型包括:
-
"UnsignedInteger8" uint8_t 没有正负号的 8 位整数 "Integer8" int8_t 有正负号的 8 位整数 "UnsignedInteger16" uint16_t 没有正负号的 16 位整数 "Integer16" int16_t 有正负号的 16 位整数 "UnsignedInteger32" uint32_t 没有正负号的 32 位整数 "Integer32" int32_t 有正负号的 32 位整数 "UnsignedInteger64" uint64_t 没有正负号的 64 位整数 "Integer64" int64_t 有正负号的 64 位整数 "CUnsignedChar" unsigned char 与 C 兼容的 unsigned char "CSignedChar" signed char 与 C 兼容的 signed char "CUnsignedShort" unsigned short 与 C 兼容的 unsigned short "CShort" short 与 C 兼容的 short "CUnsignedInt" unsigned int 与 C 兼容的 unsigned int "CInt" int 与 C 兼容的 int "CUnsignedLong" unsigned long 与 C 兼容的 unsigned long "CLong" long 与 C 兼容的 long "CSizeT" size_t 与 C 兼容的 size_t "CFloat" float 与 C 兼容的 float "CDouble" double 与 C 兼容的 double "OpaqueRawPointer" void* 不透明指针 "RawPointer"::[t] t* 类型化指针 {ty1,ty2,…} struct {ty1 f1; ty2 f2; …} 构造或 product 类型 "Void" void 没有输出(仅在输出类型中) - 构造或 product 类型也可被写为 "ListTuple"::[ty1,ty2, …].
- 在 ForeignFunctionLoad[ptr,funTy] 中,ptr 必须是指向库中有效函数的 OpaqueRawPointer.
范例
打开所有单元 关闭所有单元基本范例 (1)
范围 (2)
ForeignFunctionLoad 用 FindLibrary 查找库:
ForeignFunctionLoad["compilerDemoBase", "addone", {"CInt"} -> "CInt"]或者用 FindLibrary 找到库然后传递给 ForeignFunctionLoad:
lib = FindLibrary["compilerDemoBase"];
ForeignFunctionLoad[lib, "addone", {"CInt"} -> "CInt"]funPtr = ForeignPointerLookup["compilerDemoBase", "addone"]ForeignFunctionLoad[funPtr, {"CInt"} -> "CInt"]应用 (2)
为 OpenSSL 的 RAND_bytes 函数创建一个外部函数:
opensslPath = FileNameJoin[...];randBytes = ForeignFunctionLoad[opensslPath, "RAND_bytes", {"RawPointer"::["UnsignedInteger8"], "CInt"} -> "CInt"]out = RawMemoryAllocate["UnsignedInteger8", 32]randBytes[out, 32]RawMemoryImport[out, {"List", 32}]generateRandomBytes[len_] :=
With[{out = RawMemoryAllocate["UnsignedInteger8", len]},
randBytes[out, len];RawMemoryImport[out, {"ByteArray", len}]
]RepeatedTiming[generateRandomBytes[10000000];]RepeatedTiming[RandomInteger[{0, 255}, 10000000];]opensslPath = FileNameJoin[...];sha256 = ForeignFunctionLoad[opensslPath, "SHA256", {"RawPointer"::["UnsignedInteger8"], "CUnsignedLong", "RawPointer"::["UnsignedInteger8"]} -> "RawPointer"::["UnsignedInteger8"]]plaintext = "Hello, World!";
plaintextBuf = RawMemoryExport[plaintext]ciphertextBuf = RawMemoryAllocate["UnsignedInteger8", 32]sha256[plaintextBuf, StringLength[plaintext], ciphertextBuf]RawMemoryImport[ciphertextBuf, {"List", 32}]与内置函数 Hash 相比较:
Normal@Hash[plaintext, "SHA256", "ByteArray"]generateSHA256[plaintext_] :=
With[{
plaintextBuf = RawMemoryExport[plaintext], ciphertextBuf = RawMemoryAllocate["UnsignedInteger8", 32]
},
sha256[plaintextBuf, StringLength[plaintext], ciphertextBuf];
RawMemoryImport[ciphertextBuf, {"ByteArray", 32}]
]RepeatedTiming[generateSHA256[plaintext]]RepeatedTiming[Hash[plaintext, "SHA256", "ByteArray"]]属性和关系 (1)
一般来说,ForeignFunctionLoad 创建库的可调用链接的速度比编译自定义链接的速度快:
With[{libPath = FindLibrary["compilerDemoBase"]},
RepeatedTiming[addone = ForeignFunctionLoad[libPath, "addone", {"CInt"} -> "CInt"]]
]RepeatedTiming[addoneCf = FunctionCompile[
LibraryFunctionDeclaration["addone", "compilerDemoBase", {"CInt"} -> "CInt"],
Function[Typed[x, "CInt"], LibraryFunction["addone"][x]]
]]RepeatedTiming[addone[10]]RepeatedTiming[addoneCf[10]]可能存在的问题 (3)
如果库不存在,将返回 Failure:
ForeignFunctionLoad["fakeLibrary", "addone", {"CInt"} -> "CInt"]如果函数不存在,将返回 Failure:
ForeignFunctionLoad["compilerDemoBase", "fakeFunction", {"CInt"} -> "CInt"]如果碰到不支持的类型,ForeignFunctionLoad 将返回 $Failed:
ForeignFunctionLoad["compilerDemoBase", "addone", {"UnsignedInger18"} -> "CInt"]技术笔记
-
▪
- 外部函数
相关指南
-
▪
- C/C++ 语言接口 ▪
- 调用外部程序 ▪
- 外部函数接口 ▪
- 外部操作
文本
Wolfram Research (2023),ForeignFunctionLoad,Wolfram 语言函数,https://reference.wolfram.com/language/ref/ForeignFunctionLoad.html.
CMS
Wolfram 语言. 2023. "ForeignFunctionLoad." Wolfram 语言与系统参考资料中心. Wolfram Research. https://reference.wolfram.com/language/ref/ForeignFunctionLoad.html.
APA
Wolfram 语言. (2023). ForeignFunctionLoad. Wolfram 语言与系统参考资料中心. 追溯自 https://reference.wolfram.com/language/ref/ForeignFunctionLoad.html 年
BibTeX
@misc{reference.wolfram_2026_foreignfunctionload, author="Wolfram Research", title="{ForeignFunctionLoad}", year="2023", howpublished="\url{https://reference.wolfram.com/language/ref/ForeignFunctionLoad.html}", note=[Accessed: 27-September-2026]}
BibLaTeX
@online{reference.wolfram_2026_foreignfunctionload, organization={Wolfram Research}, title={ForeignFunctionLoad}, year={2023}, url={https://reference.wolfram.com/language/ref/ForeignFunctionLoad.html}, note=[Accessed: 27-September-2026]}