Compile a contract using linked libraries

I see a lot of example of library linking for the deployment. But the contract aspect is not clear.

pragma solidity 0.8.15;

import "./libraries/MyLib.sol";

contract MyContract {
  using MyLib for uint256;


If I want to save size in the contract and deploy stand alone libraries.

 let contract = await ethers.getContractFactory("MyContract",
            libraries: {
                MyLib: MyLib.address

This will not allow me to link since the library is already there and I get this message;

This contract doesn't need linking any libraries.

If i remove my import statement then I am unable to compile since MyContract is not aware of the libraries.

How should I configure hardhat so I can compile MyContract when i'm not importing them so i can link them after, on deployment?

Turns out that the functions cannot be internal or it will be deploy with the contract.

Setting them as public could let me link them.

1 Like