Failing to Bazel build C++ project with tensorflow as a dependency












2














I see a lot of tutorials that explain how to build projects inside Tensorflow's Bazel WORKSPACE (like this one). But I can't seem to be able to find a way to build my own project and include tensorflow as a dependency. I looked at this Bazel documentation, and there is clearly a way to build with external dependencies, which I tried to follow myself. (since tf is also built with bazel).



Here is what my directory structure looks like:



.
├── perception
│   ├── BUILD
│   └── graph_loader.cc
├── third-party
│   └── tensorflow # I cloned tf repo into this folder
└── WORKSPACE


Here is what's inside my perception/BUILD file:



cc_binary(
name = "graph-loader",
srcs = [
"graph_loader.cc",
],
deps = [
"@tensorflow//tensorflow:libtensorflow.so",
]
)


Here is what's inside my WORKSPACE file:



local_repository(
name = "tensorflow",
path = "path/to/my/project/third-party/tensorflow",
)


Here is what's inside my perception/graph_loader.cc file:



#include "tensorflow/cc/client/client_session.h"
#include "tensorflow/cc/ops/standard_ops.h"
#include "tensorflow/core/framework/tensor.h"

int main() {
using namespace tensorflow;
using namespace tensorflow::ops;
Scope root = Scope::NewRootScope();
// Matrix A = [3 2; -1 0]
auto A = Const(root, { {3.f, 2.f}, {-1.f, 0.f} });
// Vector b = [3 5]
auto b = Const(root, { {3.f, 5.f} });
// v = Ab^T
auto v = MatMul(root.WithOpName("v"), A, b, MatMul::TransposeB(true));
std::vector<Tensor> outputs;
ClientSession session(root);
// Run and fetch v
TF_CHECK_OK(session.Run({v}, &outputs));
// Expect outputs[0] == [19; -3]
LOG(INFO) << outputs[0].matrix<float>();
return 0;
}


I run my build with the following command:



build //perception:graph-loader


But it fails with this message:



ERROR: path/to/my/project/perception/BUILD:1:1: error loading package '@tensorflow//tensorflow': Extension file not found. Unable to load package for '@local_config_cuda//cuda:build_defs.bzl': The repository could not be resolved and referenced by '//perception:graph-loader'
ERROR: Analysis of target '//perception:graph-loader' failed; build aborted: error loading package '@tensorflow//tensorflow': Extension file not found. Unable to load package for '@local_config_cuda//cuda:build_defs.bzl': The repository could not be resolved
INFO: Elapsed time: 0.037s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (1 packages loaded, 0 targets configured)
currently loading: @tensorflow//tensorflow


Here are the quesitions:




  1. What am I doing wrong so that my build keeps failing?

  2. Is it even possible to do what I'm trying here, i.e. build tensorflow inside of my project?










share|improve this question





























    2














    I see a lot of tutorials that explain how to build projects inside Tensorflow's Bazel WORKSPACE (like this one). But I can't seem to be able to find a way to build my own project and include tensorflow as a dependency. I looked at this Bazel documentation, and there is clearly a way to build with external dependencies, which I tried to follow myself. (since tf is also built with bazel).



    Here is what my directory structure looks like:



    .
    ├── perception
    │   ├── BUILD
    │   └── graph_loader.cc
    ├── third-party
    │   └── tensorflow # I cloned tf repo into this folder
    └── WORKSPACE


    Here is what's inside my perception/BUILD file:



    cc_binary(
    name = "graph-loader",
    srcs = [
    "graph_loader.cc",
    ],
    deps = [
    "@tensorflow//tensorflow:libtensorflow.so",
    ]
    )


    Here is what's inside my WORKSPACE file:



    local_repository(
    name = "tensorflow",
    path = "path/to/my/project/third-party/tensorflow",
    )


    Here is what's inside my perception/graph_loader.cc file:



    #include "tensorflow/cc/client/client_session.h"
    #include "tensorflow/cc/ops/standard_ops.h"
    #include "tensorflow/core/framework/tensor.h"

    int main() {
    using namespace tensorflow;
    using namespace tensorflow::ops;
    Scope root = Scope::NewRootScope();
    // Matrix A = [3 2; -1 0]
    auto A = Const(root, { {3.f, 2.f}, {-1.f, 0.f} });
    // Vector b = [3 5]
    auto b = Const(root, { {3.f, 5.f} });
    // v = Ab^T
    auto v = MatMul(root.WithOpName("v"), A, b, MatMul::TransposeB(true));
    std::vector<Tensor> outputs;
    ClientSession session(root);
    // Run and fetch v
    TF_CHECK_OK(session.Run({v}, &outputs));
    // Expect outputs[0] == [19; -3]
    LOG(INFO) << outputs[0].matrix<float>();
    return 0;
    }


    I run my build with the following command:



    build //perception:graph-loader


    But it fails with this message:



    ERROR: path/to/my/project/perception/BUILD:1:1: error loading package '@tensorflow//tensorflow': Extension file not found. Unable to load package for '@local_config_cuda//cuda:build_defs.bzl': The repository could not be resolved and referenced by '//perception:graph-loader'
    ERROR: Analysis of target '//perception:graph-loader' failed; build aborted: error loading package '@tensorflow//tensorflow': Extension file not found. Unable to load package for '@local_config_cuda//cuda:build_defs.bzl': The repository could not be resolved
    INFO: Elapsed time: 0.037s
    INFO: 0 processes.
    FAILED: Build did NOT complete successfully (1 packages loaded, 0 targets configured)
    currently loading: @tensorflow//tensorflow


    Here are the quesitions:




    1. What am I doing wrong so that my build keeps failing?

    2. Is it even possible to do what I'm trying here, i.e. build tensorflow inside of my project?










    share|improve this question



























      2












      2








      2







      I see a lot of tutorials that explain how to build projects inside Tensorflow's Bazel WORKSPACE (like this one). But I can't seem to be able to find a way to build my own project and include tensorflow as a dependency. I looked at this Bazel documentation, and there is clearly a way to build with external dependencies, which I tried to follow myself. (since tf is also built with bazel).



      Here is what my directory structure looks like:



      .
      ├── perception
      │   ├── BUILD
      │   └── graph_loader.cc
      ├── third-party
      │   └── tensorflow # I cloned tf repo into this folder
      └── WORKSPACE


      Here is what's inside my perception/BUILD file:



      cc_binary(
      name = "graph-loader",
      srcs = [
      "graph_loader.cc",
      ],
      deps = [
      "@tensorflow//tensorflow:libtensorflow.so",
      ]
      )


      Here is what's inside my WORKSPACE file:



      local_repository(
      name = "tensorflow",
      path = "path/to/my/project/third-party/tensorflow",
      )


      Here is what's inside my perception/graph_loader.cc file:



      #include "tensorflow/cc/client/client_session.h"
      #include "tensorflow/cc/ops/standard_ops.h"
      #include "tensorflow/core/framework/tensor.h"

      int main() {
      using namespace tensorflow;
      using namespace tensorflow::ops;
      Scope root = Scope::NewRootScope();
      // Matrix A = [3 2; -1 0]
      auto A = Const(root, { {3.f, 2.f}, {-1.f, 0.f} });
      // Vector b = [3 5]
      auto b = Const(root, { {3.f, 5.f} });
      // v = Ab^T
      auto v = MatMul(root.WithOpName("v"), A, b, MatMul::TransposeB(true));
      std::vector<Tensor> outputs;
      ClientSession session(root);
      // Run and fetch v
      TF_CHECK_OK(session.Run({v}, &outputs));
      // Expect outputs[0] == [19; -3]
      LOG(INFO) << outputs[0].matrix<float>();
      return 0;
      }


      I run my build with the following command:



      build //perception:graph-loader


      But it fails with this message:



      ERROR: path/to/my/project/perception/BUILD:1:1: error loading package '@tensorflow//tensorflow': Extension file not found. Unable to load package for '@local_config_cuda//cuda:build_defs.bzl': The repository could not be resolved and referenced by '//perception:graph-loader'
      ERROR: Analysis of target '//perception:graph-loader' failed; build aborted: error loading package '@tensorflow//tensorflow': Extension file not found. Unable to load package for '@local_config_cuda//cuda:build_defs.bzl': The repository could not be resolved
      INFO: Elapsed time: 0.037s
      INFO: 0 processes.
      FAILED: Build did NOT complete successfully (1 packages loaded, 0 targets configured)
      currently loading: @tensorflow//tensorflow


      Here are the quesitions:




      1. What am I doing wrong so that my build keeps failing?

      2. Is it even possible to do what I'm trying here, i.e. build tensorflow inside of my project?










      share|improve this question















      I see a lot of tutorials that explain how to build projects inside Tensorflow's Bazel WORKSPACE (like this one). But I can't seem to be able to find a way to build my own project and include tensorflow as a dependency. I looked at this Bazel documentation, and there is clearly a way to build with external dependencies, which I tried to follow myself. (since tf is also built with bazel).



      Here is what my directory structure looks like:



      .
      ├── perception
      │   ├── BUILD
      │   └── graph_loader.cc
      ├── third-party
      │   └── tensorflow # I cloned tf repo into this folder
      └── WORKSPACE


      Here is what's inside my perception/BUILD file:



      cc_binary(
      name = "graph-loader",
      srcs = [
      "graph_loader.cc",
      ],
      deps = [
      "@tensorflow//tensorflow:libtensorflow.so",
      ]
      )


      Here is what's inside my WORKSPACE file:



      local_repository(
      name = "tensorflow",
      path = "path/to/my/project/third-party/tensorflow",
      )


      Here is what's inside my perception/graph_loader.cc file:



      #include "tensorflow/cc/client/client_session.h"
      #include "tensorflow/cc/ops/standard_ops.h"
      #include "tensorflow/core/framework/tensor.h"

      int main() {
      using namespace tensorflow;
      using namespace tensorflow::ops;
      Scope root = Scope::NewRootScope();
      // Matrix A = [3 2; -1 0]
      auto A = Const(root, { {3.f, 2.f}, {-1.f, 0.f} });
      // Vector b = [3 5]
      auto b = Const(root, { {3.f, 5.f} });
      // v = Ab^T
      auto v = MatMul(root.WithOpName("v"), A, b, MatMul::TransposeB(true));
      std::vector<Tensor> outputs;
      ClientSession session(root);
      // Run and fetch v
      TF_CHECK_OK(session.Run({v}, &outputs));
      // Expect outputs[0] == [19; -3]
      LOG(INFO) << outputs[0].matrix<float>();
      return 0;
      }


      I run my build with the following command:



      build //perception:graph-loader


      But it fails with this message:



      ERROR: path/to/my/project/perception/BUILD:1:1: error loading package '@tensorflow//tensorflow': Extension file not found. Unable to load package for '@local_config_cuda//cuda:build_defs.bzl': The repository could not be resolved and referenced by '//perception:graph-loader'
      ERROR: Analysis of target '//perception:graph-loader' failed; build aborted: error loading package '@tensorflow//tensorflow': Extension file not found. Unable to load package for '@local_config_cuda//cuda:build_defs.bzl': The repository could not be resolved
      INFO: Elapsed time: 0.037s
      INFO: 0 processes.
      FAILED: Build did NOT complete successfully (1 packages loaded, 0 targets configured)
      currently loading: @tensorflow//tensorflow


      Here are the quesitions:




      1. What am I doing wrong so that my build keeps failing?

      2. Is it even possible to do what I'm trying here, i.e. build tensorflow inside of my project?







      c++ tensorflow bazel






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 at 1:56









      Milo Lu

      1,58711327




      1,58711327










      asked Nov 11 at 22:55









      nikolaevra

      626




      626
























          1 Answer
          1






          active

          oldest

          votes


















          2














          You are getting this error because you have not added the required repository rules in your WORKSPACE. Bazel currently does not have recursive workspace, so you need to manually copy those repositories of all your dependencies into the main WORKSPACE.



          In your WORKSPACE file, copy this:



          local_repository(
          name = "org_tensorflow",
          path = "third-party/tensorflow",
          )


          Append all content of https://github.com/tensorflow/tensorflow/blob/master/WORKSPACE into the WORKSPACE file. Remove the workspace(name = "org_tensorflow") line.



          Finally, change all //* in WOKRSPACE to @org_tensorflow//*.



          Note that building Tensorflow in sub-folder is not officially supported.






          share|improve this answer





















          • Wow, your help was amazing. Everything works now! Thanks a lot
            – nikolaevra
            Nov 13 at 4:06











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254061%2ffailing-to-bazel-build-c-project-with-tensorflow-as-a-dependency%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          You are getting this error because you have not added the required repository rules in your WORKSPACE. Bazel currently does not have recursive workspace, so you need to manually copy those repositories of all your dependencies into the main WORKSPACE.



          In your WORKSPACE file, copy this:



          local_repository(
          name = "org_tensorflow",
          path = "third-party/tensorflow",
          )


          Append all content of https://github.com/tensorflow/tensorflow/blob/master/WORKSPACE into the WORKSPACE file. Remove the workspace(name = "org_tensorflow") line.



          Finally, change all //* in WOKRSPACE to @org_tensorflow//*.



          Note that building Tensorflow in sub-folder is not officially supported.






          share|improve this answer





















          • Wow, your help was amazing. Everything works now! Thanks a lot
            – nikolaevra
            Nov 13 at 4:06
















          2














          You are getting this error because you have not added the required repository rules in your WORKSPACE. Bazel currently does not have recursive workspace, so you need to manually copy those repositories of all your dependencies into the main WORKSPACE.



          In your WORKSPACE file, copy this:



          local_repository(
          name = "org_tensorflow",
          path = "third-party/tensorflow",
          )


          Append all content of https://github.com/tensorflow/tensorflow/blob/master/WORKSPACE into the WORKSPACE file. Remove the workspace(name = "org_tensorflow") line.



          Finally, change all //* in WOKRSPACE to @org_tensorflow//*.



          Note that building Tensorflow in sub-folder is not officially supported.






          share|improve this answer





















          • Wow, your help was amazing. Everything works now! Thanks a lot
            – nikolaevra
            Nov 13 at 4:06














          2












          2








          2






          You are getting this error because you have not added the required repository rules in your WORKSPACE. Bazel currently does not have recursive workspace, so you need to manually copy those repositories of all your dependencies into the main WORKSPACE.



          In your WORKSPACE file, copy this:



          local_repository(
          name = "org_tensorflow",
          path = "third-party/tensorflow",
          )


          Append all content of https://github.com/tensorflow/tensorflow/blob/master/WORKSPACE into the WORKSPACE file. Remove the workspace(name = "org_tensorflow") line.



          Finally, change all //* in WOKRSPACE to @org_tensorflow//*.



          Note that building Tensorflow in sub-folder is not officially supported.






          share|improve this answer












          You are getting this error because you have not added the required repository rules in your WORKSPACE. Bazel currently does not have recursive workspace, so you need to manually copy those repositories of all your dependencies into the main WORKSPACE.



          In your WORKSPACE file, copy this:



          local_repository(
          name = "org_tensorflow",
          path = "third-party/tensorflow",
          )


          Append all content of https://github.com/tensorflow/tensorflow/blob/master/WORKSPACE into the WORKSPACE file. Remove the workspace(name = "org_tensorflow") line.



          Finally, change all //* in WOKRSPACE to @org_tensorflow//*.



          Note that building Tensorflow in sub-folder is not officially supported.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 at 1:17









          John London

          665621




          665621












          • Wow, your help was amazing. Everything works now! Thanks a lot
            – nikolaevra
            Nov 13 at 4:06


















          • Wow, your help was amazing. Everything works now! Thanks a lot
            – nikolaevra
            Nov 13 at 4:06
















          Wow, your help was amazing. Everything works now! Thanks a lot
          – nikolaevra
          Nov 13 at 4:06




          Wow, your help was amazing. Everything works now! Thanks a lot
          – nikolaevra
          Nov 13 at 4:06


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254061%2ffailing-to-bazel-build-c-project-with-tensorflow-as-a-dependency%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud

          Zucchini