title: Statically link shared library using go build
body: <p>I need to create a static library to run it in a docker container. When running my code with <code>go run main.go</code> it just runs smoothly. However, things get complicated when I try to build my code as a <strong>static</strong> program (re-compile all dynamic dependencies into a standalone lib, including shared libraries).</p>
  
  <p>My app depends on a C shared library located in my <code>/usr/local/lib/librandom_c.so</code>, while my app source code is located in <code>$GOPATH/src/github.com/&lt;user&gt;/myapp</code>.</p>
  
  <p>When I build the app <strong>without</strong> trying to statically link the shared library, it works without complaining:</p>
  
  <pre><code>go build -a -o hello .
  </code></pre>
  
  <p>...but then docker complains that the shared library does not exist (using from scratch).</p>
  
  <p>So far I have tried many combinations to statically build my app:</p>
  
  <pre><code>go build -a -ldflags '-linkmode "external" -extldflags "-static"' -o hello .
  </code></pre>
  
  <p>gives an error:</p>
  
  <blockquote>
    <p>cannot find -lrandom_c</p>
  </blockquote>
  
  <p>How do I statically link my lib?</p>