> **Can't find what you're looking for?** Use `search_docs` on the docs MCP server at `https://www.getfoundry.sh/api/mcp` to find what you need.
>
> **Have feedback?** Use `submit_feedback` on the same MCP server.

## Linked libraries

Solidity library functions declared `public` or `external` can create a dependency on a separately
deployed library. Internal library functions are compiled into their callers. Use
`allow_linked_libraries` to reject actual external library dependencies during compilation.

### `allow_linked_libraries`

* Type: boolean
* Default: `true`
* Environment: `FOUNDRY_ALLOW_LINKED_LIBRARIES`

When set to `false`, `forge build`, `forge test`, `forge script`, `forge create`, and `forge coverage`
fail if a compiled contract's creation or runtime bytecode requires an external Solidity library.
The check includes cached artifacts and the compilation used by mutation tests.

```toml
[profile.default]
allow_linked_libraries = false
```

You can also enforce this for one invocation with `--disallow-linked-libraries`:

```sh
forge build --disallow-linked-libraries
forge test --disallow-linked-libraries
```

The flag overrides the selected profile and `FOUNDRY_ALLOW_LINKED_LIBRARIES`, including when either
allows linked libraries.

A violation lists each contract with the library it links. Compiler warnings are printed before
the error:

```text
Error: Linked libraries are not allowed (`allow_linked_libraries = false`):
  src/Consumer.sol:Consumer links src/Lib.sol:Lib
help: make the library functions `internal` to inline them, or set `allow_linked_libraries = true`
```

To fix it, declare the library functions the contract calls as `internal`. `forge build --json`
reports the same message as a `LinkedLibraryError` in its JSON `errors` array and exits with a
nonzero status.

While linked libraries are disallowed, configured `libraries` addresses are withheld from solc. This
preserves link references so explicitly configured or previously cached prelinked dependencies
cannot bypass the check. An unused address entry alone does not fail compilation. Allowing linked
libraries again restores normal library-address configuration and recompiles artifacts when the
compiler settings change.

The policy applies to the artifacts selected for compilation, including dependencies and test or
script contracts in that compilation. It does not inspect arbitrary existing artifacts or libraries
loaded at runtime through cheatcodes. ABI discovery, test listing, and read-only inspection remain
available so you can identify a dependency before compiling executable contracts.

### Dynamic test linking

`dynamic_test_linking` optimizes contract deployment in tests and scripts by replacing eligible
`new Contract(...)` expressions with deployment cheatcodes. It does not introduce Solidity library
link references. Its constructor-argument helpers are also compatible with this policy.

You can keep dynamic test linking enabled while prohibiting external Solidity libraries:

```toml
[profile.default]
allow_linked_libraries = false
dynamic_test_linking = true
```

The prohibition does not change `dynamic_test_linking` or require
`--no-dynamic-test-linking`. It permits internal libraries, unused public library functions, and
ordinary contract deployment with or without dynamic test linking. A contract deployed through
dynamic test linking still fails compilation if that contract actually depends on an external
Solidity library.
