tensorflow: Windows import library is missing sufficient symbols to use C++ API

System information

  • OS Platform and Distribution Windows 10
  • TensorFlow installed from (source or binary): Source
  • TensorFlow version: 2.3.0
  • Python version: 3.8.5
  • Bazel version (if compiling from source): 3.3.1
  • GCC/Compiler version (if compiling from source): Visual Studio 2019

Expected Result

The generated Windows Binaries should export sufficient symbols to compile and link a basic program using the C++ API

Actual Result

Compiling and linking a basic Tensorflow C++ program with the Windows Binaries results in undefined symbols.

Provide the exact sequence of commands / steps that you executed before running into the problem

Example from Tensorflow 2.3.0 API Reference

main.cpp


#include <tensorflow/cc/framework/scope.h>
#include <tensorflow/cc/client/client_session.h>
#include <tensorflow/cc/ops/array_ops.h>
#include <tensorflow/cc/ops/math_ops.h>

#include <vector>

namespace tf = tensorflow;
using namespace tf::ops;

int main() {
    tf::Scope root = tf::Scope::NewRootScope();
    auto a = Placeholder(root, tf::DT_INT32);
    auto c = Add(root, a, {41});

    tf::ClientSession session(root);
    std::vector<tf::Tensor> outputs;

    auto status = session.Run({{a, {1}}}, {c}, &outputs);

    return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
project(tensorflow_example)

set(CMAKE_VERBOSE_MAKEFILE TRUE)
add_executable(example main.cpp)

target_link_libraries(example PRIVATE "${TENSORFLOW_ROOT}/tensorflow_cc.lib")
target_compile_definitions(example PRIVATE NOMINMAX)
target_include_directories(example PRIVATE
    "${TENSORFLOW_ROOT}/include"
    "${TENSORFLOW_ROOT}/include/src"
)
python configure.py
<Accept defaults>
bazel build --config=opt //tensorflow:tensorflow_cc
bazel build --config=opt //tensorflow:tensorflow_cc_dll_import_lib
bazel build --config=opt //tensorflow:install_headers
mkdir build
cd build
cmake .. -DTENSORFLOW_ROOT=<path to bazel-bin/tensorflow>
cmake --build .

The compilation succeeds but linking fails with the following missing externals:

Microsoft (R) Build Engine version 16.6.0+5ff7b0c9e for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.

main.obj : error LNK2019: unresolved external symbol "public: __cdecl tensorflow::Operation::Operation(class tensorflow::Node *)" (??0Operation@tensorflow@@QEAA@PEAVNode@1@@Z) referenced in function "public: __cdecl tensorflow::Input::Input(class std::initializer_list<struct tensorflow::Input::Initializer> const &)" (??0Input@tensorflow@@QEAA@AEBV?$initializer_list@UInitializer@Input@tensorflow@@@std@@@Z) [C:\Users\plane\projects\tf_test\build\example.vcxproj]
main.obj : error LNK2019: unresolved external symbol "public: __cdecl tensorflow::Input::Initializer::Initializer(class std::initializer_list<struct tensorflow::Input::Initializer> const &)" (??0Initializer@Input@tensorflow@@QEAA@AEBV?$initializer_list@UInitializer@Input@tensorflow@@@std@@@Z) referenced in function "public: __cdecl tensorflow::Input::Input(class std::initializer_list<struct tensorflow::Input::Initializer> const &)" (??0Input@tensorflow@@QEAA@AEBV?$initializer_list@UInitializer@Input@tensorflow@@@std@@@Z) [C:\Users\plane\projects\tf_test\build\example.vcxproj]
main.obj : error LNK2019: unresolved external symbol "public: __cdecl tensorflow::Scope::~Scope(void)" (??1Scope@tensorflow@@QEAA@XZ) referenced in function main [C:\Users\plane\projects\tf_test\build\example.vcxproj]
main.obj : error LNK2019: unresolved external symbol "public: static class tensorflow::Scope __cdecl tensorflow::Scope::NewRootScope(void)" (?NewRootScope@Scope@tensorflow@@SA?AV12@XZ) referenced in function main [C:\Users\plane\projects\tf_test\build\example.vcxproj]
main.obj : error LNK2019: unresolved external symbol "public: __cdecl tensorflow::ClientSession::ClientSession(class tensorflow::Scope const &)" (??0ClientSession@tensorflow@@QEAA@AEBVScope@1@@Z) referenced in function main [C:\Users\plane\projects\tf_test\build\example.vcxproj]
main.obj : error LNK2019: unresolved external symbol "public: __cdecl tensorflow::ClientSession::~ClientSession(void)" (??1ClientSession@tensorflow@@QEAA@XZ) referenced in function main [C:\Users\plane\projects\tf_test\build\example.vcxproj]
main.obj : error LNK2019: unresolved external symbol "public: class tensorflow::Status __cdecl tensorflow::ClientSession::Run(class std::unordered_map<class tensorflow::Output,struct tensorflow::Input::Initializer,struct tensorflow::OutputHash,struct std::equal_to<class tensorflow::Output>,class std::allocator<struct std::pair<class tensorflow::Output const ,struct tensorflow::Input::Initializer> > > const &,class std::vector<class tensorflow::Output,class std::allocator<class tensorflow::Output> > const &,class std::vector<class tensorflow::Tensor,class std::allocator<class tensorflow::Tensor> > *)const " (?Run@ClientSession@tensorflow@@QEBA?AVStatus@2@AEBV?$unordered_map@VOutput@tensorflow@@UInitializer@Input@2@UOutputHash@2@U?$equal_to@VOutput@tensorflow@@@std@@V?$allocator@U?$pair@$$CBVOutput@tensorflow@@UInitializer@Input@2@@std@@@7@@std@@AEBV?$vector@VOutput@tensorflow@@V?$allocator@VOutput@tensorflow@@@std@@@5@PEAV?$vector@VTensor@tensorflow@@V?$allocator@VTensor@tensorflow@@@std@@@5@@Z) referenced in function main [C:\Users\plane\projects\tf_test\build\example.vcxproj]
main.obj : error LNK2019: unresolved external symbol "public: __cdecl tensorflow::ops::Placeholder::Placeholder(class tensorflow::Scope const &,enum tensorflow::DataType)" (??0Placeholder@ops@tensorflow@@QEAA@AEBVScope@2@W4DataType@2@@Z) referenced in function main [C:\Users\plane\projects\tf_test\build\example.vcxproj]
main.obj : error LNK2019: unresolved external symbol "public: __cdecl tensorflow::ops::Add::Add(class tensorflow::Scope const &,class tensorflow::Input,class tensorflow::Input)" (??0Add@ops@tensorflow@@QEAA@AEBVScope@2@VInput@2@1@Z) referenced in function main [C:\Users\plane\projects\tf_test\build\example.vcxproj]
C:\Users\plane\projects\tf_test\build\Debug\example.exe : fatal error LNK1120: 9 unresolved externals [C:\Users\plane\projects\tf_test\build\example.vcxproj]

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 25 (4 by maintainers)

Most upvoted comments

I had the same problem as @planetmarshall. Applying the patch of meteorcloudy@4b8e7be, i have a linking problem for ClientSession . After applying meteorcloudy@4b8e7be, I added some lines in “tensorflow/BUILD” so I had this:

filegroup(
    name = "cc_ops_def_file",
    srcs = [
        "//tensorflow/cc:cc_ops",
        "//tensorflow/cc:array_ops",
        "//tensorflow/cc:const_op",
        "//tensorflow/cc:math_ops",
        "//tensorflow/cc:scope",
        "//tensorflow/cc:client_session",
        "//tensorflow/cc/profiler",
        "//tensorflow/core:tensorflow",
    ],
    output_group = "def_file",
)

Then i rebuild the .lib.

Now I have no more linking problems, but the Add operation returns an error during runtime: Unhandled exception at 0x00007FFCB07BA719 in ConsoleApplication1.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0000005151BEEAF0.

I see, https://github.com/meteorcloudy/tensorflow/commit/4b8e7bef11040c465453e973eb0c6ad43cfd3c96 basically offers an example to get the DEF file from cc_library instead of cc_binary. The DEF file will not contains symbols from the transitive cc_libraries, so it makes it easier to not exceed the symbol limits (64K). I think if you add the corresponding cc_libraries for the missing symbols in

filegroup(
    name = "cc_ops_def_file",
    srcs = [
        "//tensorflow/cc:cc_ops",
        "//tensorflow/cc:array_ops",
        "//tensorflow/cc:const_op",
        "//tensorflow/cc:math_ops",
    ],
    output_group = "def_file",
)

It will probably work.

@Shadowman82 Hi,

Did you manage to use FreezeSavedModel ?I have, like you, the problem with a lot of link error during bazel buidl after adding to the BUILD file of tensorflow through filegroup. Can you tell me which TF version and bazel you are using to make it work ? And which file you are building.

I use bazel build --config=opt --config=cuda tensorflow:tensorflow_cc.dll then bazel build --config=opt --config=cuda tensorflow:tensorflow_cc.lib then bazel build --config=opt --config=cuda tensorflow:install_headers

All of them works nicely and I can use them in my project until i add “//tensorflow/cc/tools:freeze_saved_model” to the filegroup.

Thank you a lot.

I solved putting TF_EXPORT macro in ops.h in front of the requested class methods Operation::Operation and Input::Initializer, and adding this include at the beginning of ops.h include section: #include "tensorflow/core/platform/macros.h"

Now I have no more linking problems, but the Add operation returns an error during runtime: Unhandled exception at 0x00007FFCB07BA719 in ConsoleApplication1.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0000005151BEEAF0.

I’ve just resolved the bad_alloc problem. I switch from Debug to Release in Microsoft Visual Studio. Now i’m able to execute the example from Tensorflow 2.3.0 API Reference.

Thanks for your help!