---
title: "FunctionCompileExportLibrary"
language: "en"
type: "Symbol"
summary: "FunctionCompileExportLibrary[file, fspec] exports a compiled version of function specification fspec as a shared library suitable for external use. FunctionCompileExportLibrary[file, defs, fspec] uses the auxiliary definitions defs for compilation."
canonical_url: "https://reference.wolfram.com/language/ref/FunctionCompileExportLibrary.html"
source: "Wolfram Language Documentation"
related_guides: 
  - 
    title: "C/C++ Language Interface"
    link: "https://reference.wolfram.com/language/guide/CLanguageInterface.en.md"
  - 
    title: "External Language Interfaces"
    link: "https://reference.wolfram.com/language/guide/ExternalLanguageInterfaces.en.md"
  - 
    title: "Code Compilation"
    link: "https://reference.wolfram.com/language/guide/CodeCompilation.en.md"
  - 
    title: "GPU Computing"
    link: "https://reference.wolfram.com/language/guide/GPUComputing.en.md"
  - 
    title: "GPU Computing with NVIDIA"
    link: "https://reference.wolfram.com/language/guide/GPUComputing-NVIDIA.en.md"
  - 
    title: "GPU Computing with Apple"
    link: "https://reference.wolfram.com/language/guide/GPUComputing-Apple.en.md"
related_functions: 
  - 
    title: "FunctionCompile"
    link: "https://reference.wolfram.com/language/ref/FunctionCompile.en.md"
  - 
    title: "FunctionCompileExport"
    link: "https://reference.wolfram.com/language/ref/FunctionCompileExport.en.md"
  - 
    title: "FunctionCompileExportString"
    link: "https://reference.wolfram.com/language/ref/FunctionCompileExportString.en.md"
  - 
    title: "FunctionCompileExportByteArray"
    link: "https://reference.wolfram.com/language/ref/FunctionCompileExportByteArray.en.md"
  - 
    title: "LibraryFunctionLoad"
    link: "https://reference.wolfram.com/language/ref/LibraryFunctionLoad.en.md"
  - 
    title: "FunctionDeclaration"
    link: "https://reference.wolfram.com/language/ref/FunctionDeclaration.en.md"
  - 
    title: "CreateCompilerEnvironment"
    link: "https://reference.wolfram.com/language/ref/CreateCompilerEnvironment.en.md"
related_tutorials: 
  - 
    title: "Wolfram Compiler Manual"
    link: "https://reference.wolfram.com/language/CompilerManual/tutorial/Overview.en.md"
---
[EXPERIMENTAL]

# FunctionCompileExportLibrary
⚠ *Unsupported in Public Cloud*

FunctionCompileExportLibrary[file, fspec] exports a compiled version of function specification fspec as a shared library suitable for external use.

FunctionCompileExportLibrary[file, defs, fspec] uses the auxiliary definitions defs for compilation.

## Details and Options

* ``FunctionCompileExportLibrary`` generates a library suitable for use on the type of computer on which it is run.

* Functions can be given as a ``Function`` pure function, a list of pure functions or an association of pure functions.

* Auxiliary definitions can be given with ``FunctionDeclaration`` or by giving a ``CompilerEnvironment`` option.

* The following library types are generated on different types of systems:

|           |        |
| --------- | ------ |
| Macintosh | .dylib |
| Windows   | .dll   |
| Linux     | .so    |

* The following option can be given:

|                      |           |                                               |
| -------------------- | --------- | --------------------------------------------- |
| CompilerEnvironment  | Automatic | an environment of definitions for compilation |
| ProgressReporting    | Automatic | how to report progress during the compilation |

* By default, ``FunctionCompileExportLibrary`` creates a library in the current directory given by ``Directory[]``.

* ``FunctionCompileExportLibrary`` has the option ``CompilerOptions``, which allows detailed options to be passed to different parts of the compilation pipeline.

* The library generated by ``FunctionCompileExportLibrary`` is suitable for linking into external programs. It can also be loaded into the Wolfram System using ``LibraryFunctionLoad``.

---

## Examples (7)

### Basic Examples (4)

Export a compiled version of a function into a library:

```wl
In[1]:= lib = FunctionCompileExportLibrary["function.dylib", Function[Typed[arg, "MachineInteger"], arg + 1]]

Out[1]= "/Users/daniels/function.dylib"
```

Load the library and create a ``CompiledCodeFunction`` :

```wl
In[2]:= fun = LibraryFunctionLoad[lib]

Out[2]=
CompiledCodeFunction[Association["Signature" -> TypeSpecifier[{"Integer64"} -> "Integer64"], 
  "ErrorFunction" -> Automatic, "LoadedFunction" -> 
   LibraryFunction["/Users/daniels/function.dylib", "Main_Pointer", {}, Integer], 
  "FunctionName" - ... .dylib", 
  "VersionData" -> {14.2, 0, 0}, "SystemID" -> "MacOSX-ARM64", 
  "Input" -> Compile`Program[{}, Function[Typed[arg, "MachineInteger"], arg + 1]]], 14549941024, 
 14549940852, 14549940912, 14549940816, "{\"Integer64\"} -> \"Integer64\""]
```

Execute the function in the ``CompiledCodeFunction`` :

```wl
In[3]:= fun[10]

Out[3]= 11
```

The file can be an absolute path:

```wl
In[4]:= FunctionCompileExportLibrary[FileNameJoin[{$HomeDirectory, "tmp", "function.dylib"}], Function[Typed[arg, "MachineInteger"], arg + 1]]

Out[4]= "/Users/daniels/tmp/function.dylib"
```

---

Several functions can be compiled with ``FunctionCompileExportLibrary`` :

```wl
In[1]:= lib = FunctionCompileExportLibrary["functionlist.dylib", {Function[Typed[arg, "Integer64"], arg + 1], Function[Typed[arg, "Integer64"], arg - 1]}];
```

Loading the library creates a list of compiled code functions:

```wl
In[2]:= funs = LibraryFunctionLoad[lib]

Out[2]=
{CompiledCodeFunction[Association["Signature" -> TypeSpecifier[{"Integer64"} -> "Integer64"], 
  "ErrorFunction" -> Automatic, "LoadedFunction" -> 
   LibraryFunction["/Users/daniels/functionlist.dylib", "Main_Pointer", {}, Integer], 
  "FunctionNa ... emID" -> "MacOSX-ARM64", 
  "Input" -> Compile`Program[{}, {Function[Typed[arg, "Integer64"], arg + 1], 
     Function[Typed[arg, "Integer64"], arg - 1]}]], 14550006364, 14550005804, 14550006252, 
 14550005768, "{\"Integer64\"} -> \"Integer64\""]}
```

Execute the code that was loaded:

```wl
In[3]:= funs[[1]][10] + funs[[2]][20]

Out[3]= 30
```

---

Several functions can be compiled with ``FunctionCompileExportLibrary`` :

```wl
In[1]:= lib = FunctionCompileExportLibrary["functionassociation.dylib", <|"f1" -> Function[Typed[arg, "Integer64"], arg + 1], "f2" -> Function[Typed[arg, "Integer64"], arg - 1]|>];
```

Loading the library creates a list of compiled code functions:

```wl
In[2]:= funs = LibraryFunctionLoad[lib]

Out[2]=
<|"f1" -> CompiledCodeFunction[Association["Signature" -> TypeSpecifier[{"Integer64"} -> "Integer64"], 
  "ErrorFunction" -> Automatic, "LoadedFunction" -> 
   LibraryFunction["/Users/daniels/functionassociation.dylib", "f1_Pointer", {}, Integer],  ... "Input" -> Compile`Program[{}, Association["f1" -> Function[Typed[arg, "Integer64"], arg + 1], 
     "f2" -> Function[Typed[arg, "Integer64"], arg - 1]]]], 14550071920, 14550071360, 14550071808, 
 14550071324, "{\"Integer64\"} -> \"Integer64\""]|>
```

Execute the code that was loaded:

```wl
In[3]:= funs["f1"][10] + funs["f2"][20]

Out[3]= 30
```

---

Auxiliary definitions can be given with ``FunctionDeclaration`` :

```wl
In[1]:= lib = FunctionCompileExportLibrary["functiondef.dylib", FunctionDeclaration[AddTwo, Typed[{"Integer64"} -> "Integer64"]@Function[arg, 2 + arg]], Function[Typed[arg, "Integer64"], AddTwo[AddTwo[arg]]]];
```

Load and execute code in the library:

```wl
In[2]:=
fun = LibraryFunctionLoad[lib];
fun[10]

Out[2]= 14
```

### Options (2)

#### CompilerEnvironment (1)

Create a compiler environment and add a function definition:

```wl
In[1]:=
env = CreateCompilerEnvironment[];
CompilerEnvironmentAppendTo[env, {FunctionDeclaration[addTwo, Typed[{"Integer64"} -> "Integer64"]@Function[arg, 2arg]]}];
```

Use a compiler environment in a compilation:

```wl
In[2]:= lib = FunctionCompileExportLibrary[ "functiondef.dylib", Function[{Typed[arg, "Integer64"]}, addTwo[arg]], CompilerEnvironment -> env];
```

Loading the library creates a ``CompiledCodeFunction`` :

```wl
In[3]:= funs = LibraryFunctionLoad[lib]

Out[3]=
CompiledCodeFunction[Association["Signature" -> TypeSpecifier[{"Integer64"} -> "Integer64"], 
  "ErrorFunction" -> Automatic, "LoadedFunction" -> 
   LibraryFunction["/Users/daniels/functiondef.dylib", "Main_Pointer", {}, Integer], 
  "FunctionName ... tion[Cell$$6832`arg, 2 + Cell$$6832`arg]]], 
    Function[Typed[Cell$$6832`arg, "Integer64"], Cell$$6832`AddTwo[
      Cell$$6832`AddTwo[Cell$$6832`arg]]]]], 14550891196, 14550890932, 14550891004, 14550890852, 
 "{\"Integer64\"} -> \"Integer64\""]
```

Execute the code that was loaded:

```wl
In[4]:= funs[10]

Out[4]= 14
```

#### ProgressReporting (1)

Progress during a compilation is reported:

```wl
In[1]:= FunctionCompileExportLibrary["mylib.dylib", Function[{Typed[s, "String"]}, ToCharacterCode[s]]]

In[2]:= [image]
```

This can be suppressed by setting the option ``ProgressReporting`` to ``False``.

The default value of ``ProgressReporting`` is ``Automatic``, which means that the global setting ``\$ProgressReporting`` is used. If this is set to ``False``, then no progress reporting takes place.

### Possible Issues (1)

#### Destination Problems (1)

If an output path is chosen that does not exist or for which there is no permission, an error occurs:

```wl
In[1]:= lib = FunctionCompileExportLibrary["/unknown/function.dylib", Function[Typed[arg, "MachineInteger"], arg + 1]]
```

FunctionCompileExportLibrary::dirnex: Directory /unknown/ does not exist.

```wl
Out[1]=
Failure["FunctionCompileExportLibraryFailure", 
 Association["MessageTemplate" :> FunctionCompileExportLibrary::dirnex, 
  "MessageParameters" -> {"/unknown/"}, "DirectoryPath" -> "/unknown/"]]
```

## See Also

* [`FunctionCompile`](https://reference.wolfram.com/language/ref/FunctionCompile.en.md)
* [`FunctionCompileExport`](https://reference.wolfram.com/language/ref/FunctionCompileExport.en.md)
* [`FunctionCompileExportString`](https://reference.wolfram.com/language/ref/FunctionCompileExportString.en.md)
* [`FunctionCompileExportByteArray`](https://reference.wolfram.com/language/ref/FunctionCompileExportByteArray.en.md)
* [`LibraryFunctionLoad`](https://reference.wolfram.com/language/ref/LibraryFunctionLoad.en.md)
* [`FunctionDeclaration`](https://reference.wolfram.com/language/ref/FunctionDeclaration.en.md)
* [`CreateCompilerEnvironment`](https://reference.wolfram.com/language/ref/CreateCompilerEnvironment.en.md)

## Tech Notes

* [Wolfram Compiler Manual](https://reference.wolfram.com/language/CompilerManual/tutorial/Overview.en.md)

## Related Guides

* [C/C++ Language Interface](https://reference.wolfram.com/language/guide/CLanguageInterface.en.md)
* [External Language Interfaces](https://reference.wolfram.com/language/guide/ExternalLanguageInterfaces.en.md)
* [Code Compilation](https://reference.wolfram.com/language/guide/CodeCompilation.en.md)
* [GPU Computing](https://reference.wolfram.com/language/guide/GPUComputing.en.md)
* [GPU Computing with NVIDIA](https://reference.wolfram.com/language/guide/GPUComputing-NVIDIA.en.md)
* [GPU Computing with Apple](https://reference.wolfram.com/language/guide/GPUComputing-Apple.en.md)

## History

* [Introduced in 2019 (12.0)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn120.en.md) \| [Updated in 2021 (12.3)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn123.en.md)