Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Error in Running Tensorflow program using qt



  • Hello All,
    I have successfully build tensorflow 1.8 using bazel.
    The output is libtensorflow.so which they said can be used in windows.

    My problem is that when i used bazel to build the program the sample program
    gets build successfully. Where as when i use the qt-creator to build the program i get
    error messages.
    Sample Program:
    ```
    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>();
    
    Error:
    tensorflow.obj : error LNK2019: unresolved external symbol "public: __cdecl tensorflow::internal::LogMessage::LogMessage(char const *,int,int)" (??0LogMessage@internal@tensorflow@@QEAA@PEBDHH@Z) referenced in function "public: __cdecl TensorFlow::TensorFlow(class QWidget *)" (??0TensorFlow@@QEAA@PEAVQWidget@@@Z)
    tensorflow.obj : error LNK2019: unresolved external symbol "public: virtual __cdecl tensorflow::internal::LogMessage::~LogMessage(void)" (??1LogMessage@internal@tensorflow@@UEAA@XZ) referenced in function "public: __cdecl TensorFlow::TensorFlow(class QWidget *)" (??0TensorFlow@@QEAA@PEAVQWidget@@@Z)
    tensorflow.obj : error LNK2019: unresolved external symbol "public: __cdecl tensorflow::internal::LogMessageFatal::LogMessageFatal(char const *,int)" (??0LogMessageFatal@internal@tensorflow@@QEAA@PEBDH@Z) referenced in function "private: void __cdecl tensorflow::Tensor::FillDimsAndValidateCompatibleShape<1>(class tensorflow::gtl::ArraySlice<__int64>,class std::array<__int64,1> *)const " (??$FillDimsAndValidateCompatibleShape@$00@Tensor@tensorflow@@AEBAXV?$ArraySlice@_J@gtl@1@PEAV?$array@_J$00@std@@@Z)
    tensorflow.obj : error LNK2019: unresolved external symbol "public: virtual __cdecl tensorflow::internal::LogMessageFatal::~LogMessageFatal(void)" (??1LogMessageFatal@internal@tensorflow@@UEAA@XZ) referenced in function "private: void __cdecl tensorflow::Tensor::FillDimsAndValidateCompatibleShape<1>(class tensorflow::gtl::ArraySlice<__int64>,class std::array<__int64,1> *)const " (??$FillDimsAndValidateCompatibleShape@$00@Tensor@tensorflow@@AEBAXV?$ArraySlice@_J@gtl@1@PEAV?$array@_J$00@std@@@Z)
    tensorflow.obj : error LNK2019: unresolved external symbol "public: __cdecl tensorflow::internal::CheckOpMessageBuilder::CheckOpMessageBuilder(char const *)" (??0CheckOpMessageBuilder@internal@tensorflow@@QEAA@PEBD@Z) referenced in function "private: void __cdecl tensorflow::Tensor::FillDimsAndValidateCompatibleShape<1>(class tensorflow::gtl::ArraySlice<__int64>,class std::array<__int64,1> *)const " (??$FillDimsAndValidateCompatibleShape@$00@Tensor@tensorflow@@AEBAXV?$ArraySlice@_J@gtl@1@PEAV?$array@_J$00@std@@@Z)
    tensorflow.obj : error LNK2019: unresolved external symbol "public: __cdecl tensorflow::internal::CheckOpMessageBuilder::~CheckOpMessageBuilder(void)" (??1CheckOpMessageBuilder@internal@tensorflow@@QEAA@XZ) referenced in function "private: void __cdecl tensorflow::Tensor::FillDimsAndValidateCompatibleShape<1>(class tensorflow::gtl::ArraySlice<__int64>,class std::array<__int64,1> *)const " (??$FillDimsAndValidateCompatibleShape@$00@Tensor@tensorflow@@AEBAXV?$ArraySlice@_J@gtl@1@PEAV?$array@_J$00@std@@@Z)
    tensorflow.obj : error LNK2019: unresolved external symbol "public: class std::basic_ostream<char,struct std::char_traits<char> > * __cdecl tensorflow::internal::CheckOpMessageBuilder::ForVar2(void)" (?ForVar2@CheckOpMessageBuilder@internal@tensorflow@@QEAAPEAV?$basic_ostream@DU?$char_traits@D@std@@@std@@XZ) referenced in function "private: void __cdecl tensorflow::Tensor::FillDimsAndValidateCompatibleShape<1>(class tensorflow::gtl::ArraySlice<__int64>,class std::array<__int64,1> *)const " (??$FillDimsAndValidateCompatibleShape@$00@Tensor@tensorflow@@AEBAXV?$ArraySlice@_J@gtl@1@PEAV?$array@_J$00@std@@@Z)
    tensorflow.obj : error LNK2019: unresolved external symbol "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > * __cdecl tensorflow::internal::CheckOpMessageBuilder::NewString(void)" (?NewString@CheckOpMessageBuilder@internal@tensorflow@@QEAAPEAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) referenced in function "private: void __cdecl tensorflow::Tensor::FillDimsAndValidateCompatibleShape<1>(class tensorflow::gtl::ArraySlice<__int64>,class std::array<__int64,1> *)const " (??$FillDimsAndValidateCompatibleShape@$00@Tensor@tensorflow@@AEBAXV?$ArraySlice@_J@gtl@1@PEAV?$array@_J$00@std@@@Z)
    tensorflow.obj : error LNK2019: unresolved external symbol "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > * __cdecl tensorflow::TfCheckOpHelperOutOfLine(class tensorflow::Status const &,char const *)" (?TfCheckOpHelperOutOfLine@tensorflow@@YAPEAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@AEBVStatus@1@PEBD@Z) referenced in function "public: __cdecl TensorFlow::TensorFlow(class QWidget *)" (??0TensorFlow@@QEAA@PEAVQWidget@@@Z)
    tensorflow.obj : error LNK2019: unresolved external symbol "private: void __cdecl tensorflow::TensorShapeRep::DestructorOutOfLine(void)" (?DestructorOutOfLine@TensorShapeRep@tensorflow@@AEAAXXZ) referenced in function "public: __cdecl tensorflow::Input::Initializer::Initializer<float,void>(class std::initializer_list<float> const &)" (??$?0MX@Initializer@Input@tensorflow@@QEAA@AEBV?$initializer_list@M@std@@@Z)
    tensorflow.obj : error LNK2019: unresolved external symbol "public: __cdecl tensorflow::TensorShapeBase<class tensorflow::TensorShape>::TensorShapeBase<class tensorflow::TensorShape>(class tensorflow::gtl::ArraySlice<__int64>)" (??0?$TensorShapeBase@VTensorShape@tensorflow@@@tensorflow@@QEAA@V?$ArraySlice@_J@gtl@1@@Z) referenced in function "public: __cdecl tensorflow::Input::Initializer::Initializer<float,void>(class std::initializer_list<float> const &)" (??$?0MX@Initializer@Input@tensorflow@@QEAA@AEBV?$initializer_list@M@std@@@Z)
    tensorflow.obj : error LNK2019: unresolved external symbol "public: __int64 __cdecl tensorflow::TensorShapeBase<class tensorflow::TensorShape>::dim_size(int)const " (?dim_size@?$TensorShapeBase@VTensorShape@tensorflow@@@tensorflow@@QEBA_JH@Z) referenced in function "public: struct Eigen::DSizes<__int64,2> __cdecl tensorflow::TensorShape::AsEigenDSizesWithPadding<2>(void)const " (??$AsEigenDSizesWithPadding@$01@TensorShape@tensorflow@@QEBA?AU?$DSizes@_J$01@Eigen@@XZ)
    tensorflow.obj : error LNK2019: unresolved external symbol "private: void __cdecl tensorflow::TensorShape::CheckDimsEqual(int)const " (?CheckDimsEqual@TensorShape@tensorflow@@AEBAXH@Z) referenced in function "public: struct Eigen::DSizes<__int64,2> __cdecl tensorflow::TensorShape::AsEigenDSizes<2>(void)const " (??$AsEigenDSizes@$01@TensorShape@tensorflow@@QEBA?AU?$DSizes@_J$01@Eigen@@XZ)
    tensorflow.obj : error LNK2019: unresolved external symbol "private: void __cdecl tensorflow::TensorShape::CheckDimsAtLeast(int)const " (?CheckDimsAtLeast@TensorShape@tensorflow@@AEBAXH@Z) referenced in function "public: struct Eigen::DSizes<__int64,2> __cdecl tensorflow::TensorShape::AsEigenDSizesWithPadding<2>(void)const " (??$AsEigenDSizesWithPadding@$01@TensorShape@tensorflow@@QEBA?AU?$DSizes@_J$01@Eigen@@XZ)
    tensorflow.obj : error LNK2019: unresolved external symbol "public: __cdecl tensorflow::Tensor::Tensor(void)" (??0Tensor@tensorflow@@QEAA@XZ) referenced in function "public: __cdecl tensorflow::Input::Initializer::Initializer<float,void>(class std::initializer_list<float> const &)" (??$?0MX@Initializer@Input@tensorflow@@QEAA@AEBV?$initializer_list@M@std@@@Z)
    tensorflow.obj : error LNK2019: unresolved external symbol "public: __cdecl tensorflow::Tensor::Tensor(enum tensorflow::DataType,class tensorflow::TensorShape const &)" (??0Tensor@tensorflow@@QEAA@W4DataType@1@AEBVTensorShape@1@@Z) referenced in function "public: __cdecl tensorflow::Input::Initializer::Initializer<float,void>(class std::initializer_list<float> const &)" (??$?0MX@Initializer@Input@tensorflow@@QEAA@AEBV?$initializer_list@M@std@@@Z)
    tensorflow.obj : error LNK2019: unresolved external symbol "public: __cdecl tensorflow::Tensor::~Tensor(void)" (??1Tensor@tensorflow@@QEAA@XZ) referenced in function "public: __cdecl tensorflow::Input::Initializer::Initializer<float,void>(class std::initializer_list<float> const &)" (??$?0MX@Initializer@Input@tensorflow@@QEAA@AEBV?$initializer_list@M@std@@@Z)
    tensorflow.obj : error LNK2019: unresolved external symbol "private: void __cdecl tensorflow::Tensor::CheckTypeAndIsAligned(enum tensorflow::DataType)const " (?CheckTypeAndIsAligned@Tensor@tensorflow@@AEBAXW4DataType@2@@Z) referenced in function "public: __cdecl tensorflow::Input::Initializer::Initializer<float,void>(class std::initializer_list<float> const &)" (??$?0MX@Initializer@Input@tensorflow@@QEAA@AEBV?$initializer_list@M@std@@@Z)
    tensorflow.obj : error LNK2019: unresolved external symbol "private: void __cdecl tensorflow::Tensor::CopyFromInternal(class tensorflow::Tensor const &,class tensorflow::TensorShape const &)" (?CopyFromInternal@Tensor@tensorflow@@AEAAXAEBV12@AEBVTensorShape@2@@Z) referenced in function "public: __cdecl tensorflow::Input::Initializer::Initializer<float,void>(class std::initializer_list<float> const &)" (??$?0MX@Initializer@Input@tensorflow@@QEAA@AEBV?$initializer_list@M@std@@@Z)
    tensorflow.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::TensorFlow(class QWidget *)" (??0TensorFlow@@QEAA@PEAVQWidget@@@Z)
    tensorflow.obj : error LNK2019: unresolved external symbol "public: __cdecl tensorflow::Scope::~Scope(void)" (??1Scope@tensorflow@@QEAA@XZ) referenced in function "public: __cdecl TensorFlow::TensorFlow(class QWidget *)" (??0TensorFlow@@QEAA@PEAVQWidget@@@Z)
    tensorflow.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 "public: __cdecl TensorFlow::TensorFlow(class QWidget *)" (??0TensorFlow@@QEAA@PEAVQWidget@@@Z)
    tensorflow.obj : error LNK2019: unresolved external symbol "public: class tensorflow::Scope __cdecl tensorflow::Scope::WithOpName(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)const " (?WithOpName@Scope@tensorflow@@QEBA?AV12@AEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function "public: __cdecl TensorFlow::TensorFlow(class QWidget *)" (??0TensorFlow@@QEAA@PEAVQWidget@@@Z)
    tensorflow.obj : error LNK2019: unresolved external symbol "public: __cdecl tensorflow::ClientSession::ClientSession(class tensorflow::Scope const &)" (??0ClientSession@tensorflow@@QEAA@AEBVScope@1@@Z) referenced in function "public: __cdecl TensorFlow::TensorFlow(class QWidget *)" (??0TensorFlow@@QEAA@PEAVQWidget@@@Z)
    tensorflow.obj : error LNK2019: unresolved external symbol "public: __cdecl tensorflow::ClientSession::~ClientSession(void)" (??1ClientSession@tensorflow@@QEAA@XZ) referenced in function "public: __cdecl TensorFlow::TensorFlow(class QWidget *)" (??0TensorFlow@@QEAA@PEAVQWidget@@@Z)
    tensorflow.obj : error LNK2019: unresolved external symbol "public: class tensorflow::Status __cdecl tensorflow::ClientSession::Run(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?$vector@VOutput@tensorflow@@V?$allocator@VOutput@tensorflow@@@std@@@std@@PEAV?$vector@VTensor@tensorflow@@V?$allocator@VTensor@tensorflow@@@std@@@5@@Z) referenced in function "public: __cdecl TensorFlow::TensorFlow(class QWidget *)" (??0TensorFlow@@QEAA@PEAVQWidget@@@Z)
    tensorflow.obj : error LNK2019: unresolved external symbol "class tensorflow::Output __cdecl tensorflow::ops::Const(class tensorflow::Scope const &,struct tensorflow::Input::Initializer const &)" (?Const@ops@tensorflow@@YA?AVOutput@2@AEBVScope@2@AEBUInitializer@Input@2@@Z) referenced in function "public: __cdecl TensorFlow::TensorFlow(class QWidget *)" (??0TensorFlow@@QEAA@PEAVQWidget@@@Z)
    tensorflow.obj : error LNK2019: unresolved external symbol "public: __cdecl tensorflow::ops::MatMul::MatMul(class tensorflow::Scope const &,class tensorflow::Input,class tensorflow::Input,struct tensorflow::ops::MatMul::Attrs const &)" (??0MatMul@ops@tensorflow@@QEAA@AEBVScope@2@VInput@2@1AEBUAttrs@012@@Z) referenced in function "public: __cdecl TensorFlow::TensorFlow(class QWidget *)" (??0TensorFlow@@QEAA@PEAVQWidget@@@Z)
    release\TensorFlowExp1.exe : fatal error LNK1120: 28 unresolved externals
    
    I have the following query:
    1. Can we run tensorflow program in qt build using bazel(tensorflow library)
    2. Exactly how to resolve the link error and figure out which function in which file is missing.
    3.I have seen examples where the library is build in bazel and ran using qt.

  • Qt Champions 2019

    @Kira You're not linking against the Tensorflow library. Can you show your pro file?


  • Lifetime Qt Champion

    @Kira

    As @jsulm said, this is a linker problem. So please be more specific with the thread title, you don't even get to run the program ;)



  • @jsulm @aha_1980 : Sure will be specific with the title.
    Here is the .pro file.

    #-------------------------------------------------
    #
    # Project created by QtCreator 2018-10-30T14:34:45
    #
    #-------------------------------------------------
    
    QT       += core gui
    
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    
    TARGET = TensorFlowExp1
    TEMPLATE = app
    
    # The following define makes your compiler emit warnings if you use
    # any feature of Qt which as been marked as deprecated (the exact warnings
    # depend on your compiler). Please consult the documentation of the
    # deprecated API in order to know how to port your code away from it.
    #DEFINES += QT_DEPRECATED_WARNINGS
    DEFINES += COMPILER_MSVC
    
    # You can also make your code fail to compile if you use deprecated APIs.
    # In order to do so, uncomment the following line.
    # You can also select to disable deprecated APIs only up to a certain version of Qt.
    #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
    
    
    SOURCES += main.cpp\
            tensorflow.cpp
    
    HEADERS  += tensorflow.h
    
    FORMS    += tensorflow.ui
    
    INCLUDEPATH +=C:\\tensorflow-r1.8
    INCLUDEPATH +=C:\\tensorflow-r1.8\\bazel-tensorflow-r1.8\\external\\eigen_archive
    INCLUDEPATH +=C:\\tensorflow-r1.8\\bazel-tensorflow-r1.8\\external\\protobuf\\src
    INCLUDEPATH +=C:\\tensorflow-r1.8\\bazel-genfiles
    
    #LIBS += -LC:\\tensorflow-r1.8\\bazel-bin\\tensorflow
    LIBS+= -LC:\\tensorflow-r1.8\\bazel-out\\x64_windows-opt\\bin\\tensorflow
            -llibtensorflow
    
    LIBS += -LC:\\tensorflow-r1.8\\bazel-tensorflow-r1.8\\bazel-out\\x64_windows-opt\\bin\\tensorflow\\cc
            -lcc_ops.lo
            -lconst_op
            -lclient_session
            -lscope
            -
    
    LIBS+= -LC:\\tensorflow-r1.8\\bazel-tensorflow-r1.8\\bazel-out\\x64_windows-opt\\bin\\external\\protobuf_archive
           -lprotobuf
    
    #LIBS+= -LC:\\tensorflow-r1.8\\bazel-out\\x64_windows-opt\\bin\\tensorflow\\cc
    #        -lops
    #        -lcc_ops.lo
    

  • Qt Champions 2019

    @Kira Are the Tensorflow built using same compiler you're using to build your app?


  • Lifetime Qt Champion

    Hi,

    In addition to @jsulm, are they built for the same architecture (i.e. 32bit VS 64bit) ?



  • @jsulm : Yes
    @jsulm : 64-bit


  • Qt Champions 2019

    @Kira You should check the build log, especially the linker calls


  • Qt Champions 2019

    Since you're using MSVC your LIBS command is wrong. It must be

    LIBS += C:\tensorflow-r1.8\bazel-out\x64_windows-opt\bin\tensorflow\libtensorflow.lib

    The MSVC linker doesn't understand '-L' and '-l'
    See http://doc.qt.io/qt-5/qmake-variable-reference.html#libs


  • Lifetime Qt Champion

    @Christian-Ehrlicher

    I think that's not correct. The link you gave explicietely states:

    "If you use the Unix -l (library) and -L (library path) flags, qmake handles the libraries correctly on Windows (that is, passes the full path of the library to the linker)."

    I'm using LIBS += -L/path/ -llibrary also on Windows, as QtCreator automatically extends the library seach path then. I can't verify now, but I'm really sure that is no problem with MSVC.


  • Lifetime Qt Champion

    @Kira

    If your linker lines in the pro file span multiple lines, they must be terminated by \.

    Also, use forward slashes for paths in Qt, they will be converted automatically.

    Example:

    LIBS+= -LC:/tensorflow-r1.8/bazel-out/x64_windows-opt/bin/tensorflow \
            -llibtensorflow
    

    You can also try to omit "lib" from the library name, as it's usual in Unix (I can't 100% tell you if it works): -ltensorflow



  • @aha_1980 : Its look like there can be something to do with bazel saw a following issue at github
    https://github.com/tensorflow/tensorflow/issues/22047#issuecomment-421452033
    I tried to resolve it using the method mentioned in the link but makes no difference.
    Still i am not able to figure the issue is on which end


Log in to reply