Need help linking Tensorflow C++ API to C++ project

-1

I trained a model in Python using Tensorflow and exported it. My wish is to use the model from a c++ program for inference.

I followed this guide and I successfully built some libraries using Bazel. The selected Tensorflow version is v4.2.0, without GPU.

The following files were generated in the tensorflow directory:

  • tensorflow.dll
  • tensorflow.dll.runfiles_manifest
  • tf_custom_op_library_additional_deps.dll.gen.def
  • tensorflow.dll-2.params
  • tensorflow.lib
  • tf_custom_op_library_additional_deps.dll.gen.def-0.params
  • tensorflow.pdb
  • tensorflow.dll.if.exp
  • tensorflow.dll.if.lib
  • tensorflow_filtered_def_file.def
  • includes

In Visual Studio 2019 I added the includes directory for the compiler, and the tensorflow.dll.if.lib and tensorflow.lib for the linker as inputs. I solved all compile errors and I don't get any linker error. I copied the tensorflow.dll to the directory of the source. The program does not complain about missing dll.

When I run the folowing code:

#define NOMINMAX

#include "tensorflow/core/framework/graph.pb.h"
#include "tensorflow/core/public/session.h"
#include "tensorflow/core/public/version.h"

#include <iostream>

int main() {

    std::cout << tf_git_version() << std::endl;
    std::unique_ptr<tensorflow::Session> session(tensorflow::NewSession({}));
    return 0;
}

I obtain the following output:

unknown

C:\...\x64\Debug\TensorflowTest.exe (process 10236) exited with code -1073741819.
Press any key to close this window . . .

One can observe that the functions are not working as expected. The first one prints "unknown" instead of a version number, the second one crashes the application.

After running in the debugger the line before the return statement, the following exception is thrown:

Exception thrown at 0x00007FFE6DF4B24B (tensorflow.dll) in TensorflowTest.exe: 0xC0000005: Access violation reading location 0x0000000000000018.

Call stack:

    tensorflow.dll!google::protobuf::Map<int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::InnerMap::begin(void)   
    tensorflow.dll!google::protobuf::Map<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,int>::clear(void)  
    tensorflow.dll!google::protobuf::Map<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,int>::~Map<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,int>(void)  
    tensorflow.dll!tensorflow::ConfigProto::~ConfigProto(void)  
    TensorflowTest.exe!tensorflow::SessionOptions::~SessionOptions()

Please help me solve this problem.

c++
tensorflow
dll
asked on Stack Overflow May 13, 2021 by Áron Katona • edited May 13, 2021 by Áron Katona

1 Answer

0

I forgot that I must run the project in Release mode. After switching to release, the library is working, and the code does not crash.

answered on Stack Overflow May 13, 2021 by Áron Katona

User contributions licensed under CC BY-SA 3.0