Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot statically link against Tensorflow library in Golang #15563

metral opened this issue Dec 21, 2017 · 2 comments

Cannot statically link against Tensorflow library in Golang #15563

metral opened this issue Dec 21, 2017 · 2 comments


Copy link

@metral metral commented Dec 21, 2017

System information

  • Have I written custom code (as opposed to using a stock example script provided in TensorFlow): no
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Ubuntu 16.04
  • TensorFlow installed from (source or binary): Binary
  • TensorFlow version (use command below): 1.4.0
  • Bazel version (if compiling from source): N/A
  • CUDA/cuDNN version: N/A
  • GPU model and memory: N/A
  • Exact command to reproduce: go build -a -v -o hellotf --ldflags '-linkmode external -extldflags "-static -L /usr/local/lib"' -x <GITHUB_PROJ_NAME>/hellotf

Describe the problem

I'm trying to compile a statically-linked binary of the hello world app against the TF 1.4.0 binary per the golang install & hello-world instructions and am not able to successfully link against the TF library as it reports that ltensorflow cannot be found by /usr/bin/ld in the go build.

The command used is:
go build -a -v -o hellotf --ldflags '-linkmode external -extldflags "-static -L /usr/local/lib"' -x <GITHUB_PROJ_NAME>/hellotf

I've tried linking against /usr/local/lib (where libtensorflow lives) using -extldflags, CGO_LDFLAGS, LDFLAGS etc, done sudo ldconfig and the env setup with LD_LIBRARY_PATH and LIBRARY_PATH, but cannot move past this step and always get the error status:

/.gvm/gos/go1.8.3/pkg/tool/linux_amd64/link: running gcc failed: exit status 1
/usr/bin/ld: cannot find -ltensorflow

The only related issue I've encountered for this is:, but that didn't help much either.

That being said, I can dynamically link and build the helloworld go code e.g. go build hello_tf.go and go test all work successfully; the issue arises when I try to statically link and cannot link to libtensorflow after it compiles, no matter what settings I try using.

Any help or advice would be greatly appreciated. Thanks!

Copy link

@tensorflowbutler tensorflowbutler commented Dec 22, 2017

Thank you for your post. We noticed you have not filled out the following field in the issue template. Could you update them if they are relevant in your case, or leave them as N/A? Thanks.
Bazel version
CUDA/cuDNN version
GPU model and memory
Exact command to reproduce

Copy link

@asimshankar asimshankar commented Dec 24, 2017

TensorFlow for Go depends on the libtensorflow library for the TensorFlow C API. This is currently built and distributed as a shared library ( only, not a static library (libtensorflow.a). So, fully statically linked binaries are not yet possible from the binaries we distribute with releases.

Once bazelbuild/bazel#1920 is resolved, we should be able to build static libraries as part of our release and hopefully that will help.

Till then, you'd have to either live with dynamic linking or figure out how to build a static libtensorflow.a using some other build process (e.g., via the CMake build or something)

Hope that helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants