Static Linking

This document describes how to build FRR without hard dependencies on shared libraries. Note that it’s not possible to build FRR completely statically. This document just covers how to statically link the dependencies that aren’t likely to be present on a given platform - libfrr and libyang. The resultant binaries should still be fairly portable. For example, here is the DSO dependency list for bgpd after using these steps:

$ ldd bgpd (0x00007ffe3a989000) => /usr/lib/x86_64-linux-gnu/ (0x00007f9dc10c0000) => /lib/x86_64-linux-gnu/ (0x00007f9dc0eba000) => /lib/x86_64-linux-gnu/ (0x00007f9dc0b1c000) => /lib/x86_64-linux-gnu/ (0x00007f9dc0918000) => /lib/x86_64-linux-gnu/ (0x00007f9dc06e0000) => /lib/x86_64-linux-gnu/ (0x00007f9dc04d5000) => /lib/x86_64-linux-gnu/ (0x00007f9dc02cd000) => /lib/x86_64-linux-gnu/ (0x00007f9dc00ae000) => /lib/x86_64-linux-gnu/ (0x00007f9dbfe96000) => /lib/x86_64-linux-gnu/ (0x00007f9dbfaa5000)
     /lib64/ (0x00007f9dc1449000)


Note that these steps have only been tested with LLVM 9 / clang.

Today, libfrr can already be statically linked by passing these configure options:

--enable-static --enable-static-bin --enable-shared

libyang is more complicated. You must build and install libyang as a static library. To do this, follow the usual libyang build procedure as listed in the FRR developer docs, but set the ENABLE_STATIC option in your cmake invocation. You also need to build with PIC enabled, which today is disabled when building libyang statically.

The resultant cmake command is:

      -DCMAKE_BUILD_TYPE:String="Release" ..

This produces a bunch of .a static archives that need to ultimately be linked into FRR. However, not only is it 6 archives rather than the usual, you will now also need to link FRR with libpcre.a. Ubuntu’s libpcre3-dev package provides this, but it hasn’t been built with PIC enabled, so it’s not usable for our purposes. So download libpcre from SourceForge, and build it like this:

./configure --with-pic

Hopefully you get a nice, usable, PIC libpcre.a.

So now we have to link all these static libraries into FRR. Rather than modify FRR to accommodate this, the best option is to create an archive with all of libyang’s dependencies. Then to avoid making any changes to FRR build foo, rename this libyang.a and copy it over the usual static library location. Ugly but it works. To do this, go into your libyang build directory, which should have a bunch of .a files. Copy libpcre.a into this directory. Write the following into a shell script and run it:

ar -M <<EOM
  CREATE libyang_fat.a
  ADDLIB libyang.a
  ADDLIB libyangdata.a
  ADDLIB libmetadata.a
  ADDLIB libnacm.a
  ADDLIB libuser_inet_types.a
  ADDLIB libuser_yang_types.a
  ADDLIB libpcre.a
ranlib libyang_fat.a

libyang_fat.a is your archive. Now copy this over your install libyang.a, which on my machine is located at /usr/lib/x86_64-linux-gnu/libyang.a (try locate libyang.a if not).

Now when you build FRR with the static options enabled as above, clang should pick up the static libyang and link it, leaving you with FRR binaries that have no hard DSO dependencies beyond common system libraries. To verify, run ldd over the resultant binaries.