LibraryFunctionDeclaration["extName",lib,type]
represents a function declaration that exposes the library function extName with the type type from the library lib, for use in compiled code.
LibraryFunctionDeclaration[name->"extName",lib,type]
aliases the function with name in compiled code.
LibraryFunctionDeclaration[nameSpec,type]
assumes that the library will be loaded by the time the function is compiled.
LibraryFunctionDeclaration
LibraryFunctionDeclaration["extName",lib,type]
represents a function declaration that exposes the library function extName with the type type from the library lib, for use in compiled code.
LibraryFunctionDeclaration[name->"extName",lib,type]
aliases the function with name in compiled code.
LibraryFunctionDeclaration[nameSpec,type]
assumes that the library will be loaded by the time the function is compiled.
Details
- LibraryFunctionDeclaration is a symbolic representation of a declaration and does not evaluate on its own.
- LibraryFunctionDeclaration can be used inside of CompilerEnvironmentAppendTo and the first argument of functions like FunctionCompile.
- If name is not given, LibraryFunction["extName"] is used.
- If the library has not already been loaded, then it will be loaded when a FunctionCompile is called.
- Libraries referenced by LibraryFunctionDeclaration run in the same process as the Wolfram Language kernel.
- FindLibrary is used to locate libraries found on $LibraryPath.
- When interfacing with libraries generated from C, it is preferable to use types such as "CInt" and "CFloat" instead of "Integer32" and "Real32", as the size of C types can vary on some platforms.
- LibraryLoad can be used to manually load a library. Functions from manually loaded libraries can be referenced by LibraryFunctionDeclaration without specifying the library. »
Examples
open all close allBasic Examples (1)
Represent a declaration of a function from an external library:
dec = LibraryFunctionDeclaration["addone", "compilerDemoBase", {"CInt"} -> "CInt"];Compile a program using the library function:
cf = FunctionCompile[dec, Function[Typed[n, "CInt"], LibraryFunction["addone"][n]]]cf[4]Scope (2)
Give the function an explicit name:
dec = LibraryFunctionDeclaration[add -> "addone", "compilerDemoBase", {"CInt"} -> "CInt"];cf = FunctionCompile[dec, Function[Typed[n, "CInt"], add[n]]]cf[4]Load a library manually with LibraryLoad:
LibraryLoad["compilerDemoBase"];Create a LibraryFunctionDeclaration that references a function already loaded into the kernel:
dec = LibraryFunctionDeclaration["addone", {"CInt"} -> "CInt"];cf = FunctionCompile[dec, Function[Typed[n, "CInt"], LibraryFunction["addone"][n]]]cf[3]Using the compiled function after unloading the library can lead to crashes.
Applications (1)
Represent a declaration of the SHA256 function from OpenSSL:
opensslPath = FileNameJoin[...];
dec = LibraryFunctionDeclaration["SHA256", opensslPath, {"CArray"::["CChar"], "CSizeT", "CArray"::["CChar"]} -> "CArray"::["CChar"]];Compile a function that uses OpenSSL to compute the SHA256 hash of a string:
sha256 = FunctionCompile[dec,
Function[Typed[str, "String"],
Module[{plaintext, ciphertext},
plaintext = Cast[str, "Managed"::["CString"]];
ciphertext = CreateTypeInstance["Managed"::["CArray"::["CChar"]], 32];
LibraryFunction["SHA256"][plaintext, Cast[StringLength[str], "CSizeT"], ciphertext];
CreateTypeInstance["NumericArray", ciphertext, 32]
]
]
]Normal@sha256["This is a test"]Compare the computed hash with the built-in function Hash:
Normal@Hash["This is a test", "SHA256", "ByteArray"]Related Guides
History
Text
Wolfram Research (2022), LibraryFunctionDeclaration, Wolfram Language function, https://reference.wolfram.com/language/ref/LibraryFunctionDeclaration.html.
CMS
Wolfram Language. 2022. "LibraryFunctionDeclaration." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/LibraryFunctionDeclaration.html.
APA
Wolfram Language. (2022). LibraryFunctionDeclaration. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/LibraryFunctionDeclaration.html
BibTeX
@misc{reference.wolfram_2026_libraryfunctiondeclaration, author="Wolfram Research", title="{LibraryFunctionDeclaration}", year="2022", howpublished="\url{https://reference.wolfram.com/language/ref/LibraryFunctionDeclaration.html}", note=[Accessed: 15-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_libraryfunctiondeclaration, organization={Wolfram Research}, title={LibraryFunctionDeclaration}, year={2022}, url={https://reference.wolfram.com/language/ref/LibraryFunctionDeclaration.html}, note=[Accessed: 15-June-2026]}