I am working on a text-classification project, which is big and doesn't use bazel as its build tool. I want to integrate tensorflow into my project, but I find it is hard to change my build tool to bazel. So I wish to build static libraries on tensorflow and link them into my project. Does anyone know how to build standalone static libraries on tensorflow source and link them in the existing c++ project? Thanks a lot.
Asked
Active
Viewed 3,865 times
4
-
Refer to this: [Creating Static Library](http://stackoverflow.com/questions/5947067/how-to-create-a-static-library-with-g) and [Creating and using static libraries](http://codingfreak.blogspot.com/2010/01/creating-and-using-static-libraries-in.html) for detailed procedure. – Saqib Ahmed Feb 15 '17 at 09:14
-
Just put them in a `.a` archive with `ar(1)`, and then run `ranlib(1)` on it. The `ranlib(1)` is not needed in linux, but is a good use to do it anyway. If you call the library `lib` *
* `.a`, and put it in the proper directory, you will be able to call the compiler with `-l` * – Luis Colorado Feb 17 '17 at 06:33* parameter.
1 Answers
5
The TensorFlow repository has some Makefiles you can use to build a static library (see tensorflow/contrib/Makefile).
Alternatively, you could use bazel
to build the TensorFlow C++ shared library and then load and use the shared library in your application (bazel build -c opt //tensorflow:libtensorflow_cc.so
). Unfortunately, bazel can't yet produce a static library (#1920).
Hope that helps.

ash
- 6,681
- 3
- 18
- 30
-
Thanks, following the makefile tutorial, I successfully build libtensorflow-core.a. But when I try to include "tensorflow/core/public/session.h" in my main.cc and run "gcc -c main.cc". It shows an error: 'tensorflow/core/framework/graph.pb.h' file not found. That file is automatically generated by bazel. However I couldn't use bazel. Do you have any idea of it? – Zikun Hu Feb 16 '17 at 08:55
-
The `pb.h` files are also generated by the Makefile build, somewhere in the `gen/` subdirectory (perhaps `gen/host_obj`). You will need to add that directory to the include path for `gcc`. – ash Feb 16 '17 at 09:33
-
Thanks for advice. I include necessary directories in the include path. I write a demo code test.cc. It includes "session.h" and "env.h". And I initialize a session in test.cc, using "Status status = NewSession(SessionOptions(), &session);". Then I try to link libtensorflow-core.a to it, using "gcc -o test test.cc -I... -L... -ltensorflow-core -std=c++11". It shows an error "ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)". It also warns "building for OSX, but linking in object file built for iOS." – Zikun Hu Feb 17 '17 at 14:01
-
Does that mean lib tensor-core.a can only be used for iOS app rather than c++ project? – Zikun Hu Feb 17 '17 at 14:02
-
1Did you use `build_all_ios.sh`? It should build with multiple architectures, and you can see the set using `lipo -info libtensorflow-core.a` - which should show all the architectures it was compiled for. – ash Feb 19 '17 at 04:14