How to not embed a library?

Library Embedding issue.

:computer: Environment
Truffle, Solidity version: “0.5.16”

I am using the library contract in our base contract. I thought I understood the difference between embedding a library and launching and linking it differently. One public function should cause the library not to embed (Ive read that a lot, to my experience one isn’t always enough).

Anyways we have one library that is just for storage which is only structs. I notice that we don’t link it, which I have to assume means it is embedded. We are close to block size limit and launching it separately would save a lot of space.

I tried adding a public function and even calling it from the base contract. Still isn’t throwing the normal errors when I switch a library away from embedding (which is something along the lines of hey link this library)

:1234: Code to reproduce
Hard to give full code reproduction but I made a gist

1 Like

Hi @JesseAbram,

Welcome to the community forum :wave:

Looking at the Solidity documentation on Libraries:

There is an example library Set:

The calls to Set.contains, Set.insert and Set.remove are all compiled as calls (DELEGATECALL) to an external contract/library. If you use libraries, be aware that an actual external function call is performed.

There is an example library BigInt:

shows how to use types stored in memory and internal functions in libraries in order to implement custom types without the overhead of external function calls

My understanding is that you would need to interact with your storage library using public functions for it to be external, as per the library Set example in the Solidity documentation (though there is the added gas cost of external calls).

I deployed your example and the library wasn’t required to be deployed separately. (I verified the contract)

I added a call to the library public function and the library was deployed separately. (I verified the library and the contract)


pragma solidity ^0.5.0;

library Storage {
    struct Structing {
        uint256 this;
        mapping(uint256 => bool) that;
    } // this does not require linking and is therefore embedding in contract demo?

    function bePublic() public {} // with this function library should not embed, yet it does not seem to work


Includes call to library public function

pragma solidity ^0.5.0;

import "./Storage.sol";

contract demo {
    Storage.Structing public structing;

    function doStuff() public {

We recently had a discussion about Using modifiers for permission access control when faced with the code size limit.

in general and ideally, we shouldn’t have to change how we use the language, the compiler should automagically handle this for us and optimize for contract byte size or gas cost depending how we configure it. Rather than we having to understand the internal workings of the compiler and manually optimize to handle the implementation.

This is definitely frustrating and I have similar feelings for having to use libraries to work around contract byte size.

Please ask all the questions that you need.

1 Like

Thanks this worked! I really really thought I tried this but I guess I didn’t again thank you so much! The modifier tip would probably shave off some size too if needed

1 Like