I have several pieces of documentation about libraries, but I am not yet able to infer what steps are needed to compile and deploy a library and then a contract that uses it. The behavior of the library within solidity is quite clear, but compiling and linking is not obvious to me.

Resources I have consulted:

Here is an example contract and library that I would like to get working:


import "TestLib.sol";

contract MetaCoin {
  TestLib.Data data;
  address public owner;

  function MetaCoin() {
    owner = msg.sender;
    TestLib.Set(data, 2);

  function GetData() constant returns(uint) {
    return TestLib.Get(data);


library TestLib {
  struct Data {
    uint n;

  function Set(Data storage self, uint a) {
    self.n = a;

  function Get(Data storage self) returns(uint) {
    return self.n;

Thanks for your help!


2 Answers 2


First, let's save both of those files in the same directory and run solc --optimize --bin MetaCoin.sol. The output is:

======= MetaCoin =======

======= TestLib =======

If you look closely (scroll right) at the MetaCoin bytecode (aka binary), you'll find a couple instances of __TestLib_______________________________. These are placeholders for where the eventual address of TestLib will be linked. This means that in order to deploy MetaCoin, we'll first need to deploy TestLib, and then use solc to link TestLib's address into the MetaCoin bytecode.

We can deploy TestLib like we would deploy any other contract. See How to deploy contract into local running node using solidity browser? for an example. Once it's deployed, you'll need to copy the address of the deployed TestLib library, which is required to link TestLib to the MetaCoin binary. To do the linking, use the deployed TestLib <address> and run:

solc --optimize --bin MetaCoin.sol | solc --link --libraries TestLib:<address>

The output will look almost the same as before, but now the __TestLib_______________________________ placeholders will have been replaced with the <address> value. This bytecode can now be used to deploy MetaCoin to the same Ethereum network that you deployed TestLib to.

  • 1
    I use 3 different libraries in my contract, but I never get the ___libraryName____________ output, no matter if I use --optimize or not. My solc version is: 0.4.24+commit.e67f0147.Linux.g++
    – sunwarr10r
    Commented Jul 7, 2018 at 17:14

To add, it's possible to manually link contracts (if you wish to deploy both library and contract together.) The simplest way is just to replace all occurrences of the placeholders with the address.

An example, taken from my own (modified) code:

var linkedMetaCoinCode = metaCoinBytecode.replace(
  testLib.address.replace("0x", "")

testLib would be a normal web3 contract, deployed previously, while metaCoinBytecode would be MetaCoin's bytecode produced by solc. (In my own code, I use solc's --combined-json to get it in a nice form for JS). Note that you'll have to strip the '0x' prefix, or the linked code won't be valid.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.