TFlite 2.4.2 build flex delegate error
@yide1235,
TensorFlow 2.4 is pretty older version. Could you please update TensorFlow to the latest stable version v2.15 and check if you are facing the same issue.

Also I suspect you have missed to submit the CMakeLists Info:

```

project(load_model)
SET(TENSORFLOW_CORE_PATH ${CMAKE_SOURCE_DIR}/tensorflow/core)
MESSAGE(STATUS "TENSORFLOW_CORE_PATH ${TENSORFLOW_CORE_PATH}")
SET(TENSORFLOW_LIBARY ${CMAKE_SOURCE_DIR}/tensorflow/lib/libtensorflow-core.a)
MESSAGE(STATUS "TENSORFLOW_LIBARY ${TENSORFLOW_LIBARY}")
SET(TENSORFLOW_PROTOBUF_INCLUDE_PATH ${CMAKE_SOURCE_DIR}/tensorflow/protobuf/include)
SET(TENSORFLOW_PROTOBUF_LIBRARY_PATH ${CMAKE_SOURCE_DIR}/tensorflow/protobuf/lib)
MESSAGE(STATUS "TENSORFLOW_PROTOBUF_INCLUDE_PATH ${TENSORFLOW_PROTOBUF_INCLUDE_PATH}")
MESSAGE(STATUS "TENSORFLOW_PROTOBUF_LIBRARY_PATH ${TENSORFLOW_PROTOBUF_LIBRARY_PATH}")
SET(TENSORFLOW_PROTOBUF_LIBRARY ${TENSORFLOW_PROTOBUF_LIBRARY_PATH}/libprotobuf.a)
SET(TENSORFLOW_PROTOC_LIBRARY ${TENSORFLOW_PROTOBUF_LIBRARY_PATH}/libprotoc.a)
MESSAGE(STATUS "TENSORFLOW_NSYNC_INCLUDE_PATH ${TENSORFLOW_PROTOBUF_LIBRARY}")
MESSAGE(STATUS "TENSORFLOW_NSYNC_LIBRARY_PATH ${TENSORFLOW_PROTOC_LIBRARY}")
SET(TENSORFLOW_NSYNC_INCLUDE_PATH ${CMAKE_SOURCE_DIR}/tensorflow/nsyc/include)
SET(TENSORFLOW_NSYNC_LIBRARY_PATH ${CMAKE_SOURCE_DIR}/tensorflow/nsyc/lib)
MESSAGE(STATUS "TENSORFLOW_NSYNC_INCLUDE_PATH ${TENSORFLOW_NSYNC_INCLUDE_PATH}")
MESSAGE(STATUS "TENSORFLOW_NSYNC_LIBRARY_PATH ${TENSORFLOW_NSYNC_LIBRARY_PATH}")
SET(TENSORFLOW_NSYNC_LIBRARY ${TENSORFLOW_NSYNC_LIBRARY_PATH}/libnsync.a)
MESSAGE(STATUS "TENSORFLOW_NSYNC_LIBRARY ${TENSORFLOW_NSYNC_LIBRARY}")
SET(TENSORFLOW_PROTO_INCLUDE_PATH ${CMAKE_SOURCE_DIR}/tensorflow/proto)
SET(TENSORFLOW_PROTO_TEXT_INCLUDE_PATH ${CMAKE_SOURCE_DIR}/tensorflow/proto_text)
SET(TENSORFLOW_HOST_OBJ_INCLUDE_PATH ${CMAKE_SOURCE_DIR}/tensorflow/host_obj/)
SET(TENSORFLOW_EIGEN_INCLUDE_PATH ${CMAKE_SOURCE_DIR}/tensorflow/eigen3)
SET(TENSORFLOW_ABSL_INCLUDE_PATH ${CMAKE_SOURCE_DIR}/tensorflow/absl)
SET(TENSORFLOW_THIRD_PARTY_INCLUDE_PATH ${CMAKE_SOURCE_DIR}/tensorflow/tensorflow_third_party)
MESSAGE(STATUS "TENSORFLOW_PROTO_INCLUDE_PATH ${TENSORFLOW_PROTO_INCLUDE_PATH}")
MESSAGE(STATUS "TENSORFLOW_PROTO_TEXT_INCLUDE_PATH ${TENSORFLOW_PROTO_TEXT_INCLUDE_PATH}")
MESSAGE(STATUS "TENSORFLOW_HOST_OBJ_INCLUDE_PATH ${TENSORFLOW_HOST_OBJ_INCLUDE_PATH}")
MESSAGE(STATUS "TENSORFLOW_EIGEN_INCLUDE_PATH ${TENSORFLOW_EIGEN_INCLUDE_PATH}")
MESSAGE(STATUS "TENSORFLOW_ABSL_INCLUDE_PATH ${TENSORFLOW_ABSL_INCLUDE_PATH}")
MESSAGE(STATUS "TENSORFLOW_THIRD_PARTY_INCLUDE_PATH ${TENSORFLOW_THIRD_PARTY_INCLUDE_PATH}")
INCLUDE_DIRECTORIES(${TENSORFLOW_CORE_PATH})
INCLUDE_DIRECTORIES(${TENSORFLOW_PROTOBUF_INCLUDE_PATH})
INCLUDE_DIRECTORIES(${TENSORFLOW_PROTO_INCLUDE_PATH})
INCLUDE_DIRECTORIES(${TENSORFLOW_PROTO_TEXT_INCLUDE_PATH})
INCLUDE_DIRECTORIES(${TENSORFLOW_HOST_OBJ_INCLUDE_PATH})
INCLUDE_DIRECTORIES(${TENSORFLOW_EIGEN_INCLUDE_PATH})
INCLUDE_DIRECTORIES(${TENSORFLOW_ABSL_INCLUDE_PATH}) 
INCLUDE_DIRECTORIES(${TENSORFLOW_THIRD_PARTY_INCLUDE_PATH})
INCLUDE_DIRECTORIES(${TENSORFLOW_NSYNC_INCLUDE_PATH})
INCLUDE_DIRECTORIES(${TENSORFLOW_NSYNC_LIBRARY_PATH})
ADD_EXECUTABLE(load_model test.cpp)
SET(LOAD_MODEL_LIBRARIES
    ${TENSORFLOW_NSYNC_LIBRARY}
    ${TENSORFLOW_PROTOC_LIBRARY}
    ${TENSORFLOW_PROTOBUF_LIBRARY}
    )
SET(LDFLAGS "-std=c++11 -msse4.1 -fPIC -O3 -march=native -Wall -finline-functions -undefined")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}${LDFLAGS}")
add_compile_options(-Wl,--whole-archive  -lpthread -ldl)
TARGET_LINK_LIBRARIES(load_model -Wl,--whole-archive ${TENSORFLOW_LIBARY} -Wl,--no-whole-archive ${LOAD_MODEL_LIBRARIES}  ${CMAKE_CXX_FLAGS})
```

and try to add a declare `class tensorflow::error::Code`

Thank you! Are you satisfied with the resolution of your issue?
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=Yes&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/62900">Yes</a>
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=No&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/62900">No</a>

Hi,

deso anyone know when i try to rebuild the tflite 2.4.2 with the flex delegate open for support the hitnet model, i have met this error:

this is my code: 
#include <iostream>
#include <vector>
#include <chrono>
#include "tensorflow/lite/interpreter.h"
#include "tensorflow/lite/kernels/register.h"
#include "tensorflow/lite/string_util.h"
#include "tensorflow/lite/examples/label_image/get_top_n.h"

#include "tensorflow/lite/model.h"

// // //add the delegate
#include "tensorflow/lite/delegates/flex/delegate.h"
// // #include "tensorflow/lite/delegates/flex/converter.h"

#define H 256
#define W 256
#define THREADS 4

int main() {
    const char* MODEL = "eth3d";
    const int CHANNEL = 2;

    // const char* model_path = "eth3d/saved_model_720x1280/model_float32.tflite";  // Replace with the actual path to your TFLite model


    std::unique_ptr<tflite::FlatBufferModel> model =tflite::FlatBufferModel::BuildFromFile("./eth3d/saved_model_720x1280/model_float32.tflite");
    // std::unique_ptr<tflite::FlatBufferModel> model =tflite::FlatBufferModel::BuildFromFile("./yolov8s_integer_quant.tflite");//this one works, means the issue is the flex delegate
    // std::unique_ptr<tflite::delegate> flex_delegate = tflite::FlexDelegate::Create();

    tflite::ops::builtin::BuiltinOpResolver resolver;
    std::unique_ptr<tflite::Interpreter> interpreter;
    tflite::InterpreterBuilder(*model, resolver)(&interpreter);
    tflite::TfLiteDelegateUniquePtr flex_delegate = tflite::FlexDelegate::Create();

    if (flex_delegate != nullptr) {
        if (interpreter->ModifyGraphWithDelegate(std::move(flex_delegate)) != kTfLiteOk) {
            std::cerr << "Failed to modify graph with flex delegate." << std::endl;
            return 1;
        }
    }


    std::vector<int> input_shape = {1, H, W, CHANNEL};
    std::vector<float> input_tensor(1 * H * W * CHANNEL, 1.0f);


    // if (!model) {
    //     std::cerr << "Failed to load the model." << std::endl;
    //     return 1;
    // }

    // tflite::InterpreterBuilder builder(*model, resolver);
    // if (builder(&interpreter) != kTfLiteOk) {
    //     std::cerr << "Failed to build interpreter." << std::endl;
    //     return 1;
    // }

    // // Allocate tensors
    interpreter->AllocateTensors();



    // Get input and output details
    const auto* input_details = interpreter->input_tensor(0);
    const auto* output_details = interpreter->output_tensor(0);

    // Perform inference
    int roop_count = 10;
    float* reference_output_disparity = nullptr;

    auto start = std::chrono::high_resolution_clock::now();
    for (int i = 0; i < roop_count; ++i) {
        memcpy(input_details->data.f, input_tensor.data(), input_tensor.size() * sizeof(float));
        interpreter->Invoke();
        reference_output_disparity = interpreter->typed_output_tensor<float>(0);
    }
    auto end = std::chrono::high_resolution_clock::now();

    double inference_time = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count() / static_cast<double>(roop_count) / 1000.0;

    // Print results
    std::cout << "Model: " << MODEL << std::endl;
    std::cout << "Input resolution: " << H << "x" << W << std::endl;
    std::cout << "Number of Threads: " << THREADS << std::endl;
    std::cout << "Average of " << roop_count << " times inference: " << inference_time << "ms" << std::endl;

    return 0;
}

and this is my CMakeLists.txt:


cmake_minimum_required(VERSION 3.10)
project(TFLiteImageClassification)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)


# OpenCV Integration
find_package(OpenCV 4.7.0 REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})

# TensorFlow Lite Integration
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../tflite-dist/include/)
include_directories(/home/myd/Downloads/tensorflow-2.4.2/)

# Include the directory for generated protobuf headers
include_directories(/home/myd/Downloads/tensorflow-2.4.2/bazel-bin/)

# Define the TensorFlow Lite library and Flex delegate library
add_library(tensorflowlite SHARED IMPORTED)
add_library(flexdelegate SHARED IMPORTED)

# Set the location of the TensorFlow Lite library
set_property(TARGET tensorflowlite PROPERTY IMPORTED_LOCATION /home/myd/Desktop/Stereo-reconstruct-C++/tflite/tflite-dist/libs/linux_x64/libtensorflowlite.so)

# Set the location of the Flex delegate library
set_property(TARGET flexdelegate PROPERTY IMPORTED_LOCATION /home/myd/Downloads/tensorflow-2.4.2/bazel-out/k8-opt/bin/tensorflow/lite/delegates/flex/libdelegate_so.so)



# Add executable target
add_executable(${PROJECT_NAME} main.cpp)

target_link_libraries(${PROJECT_NAME} PRIVATE  ${OpenCV_LIBS}  tensorflowlite flexdelegate )



# Abseil
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/third_party/abseil-cpp)
link_directories(${CMAKE_CURRENT_SOURCE_DIR}/third_party/abseil-cpp/build)


# Find Eigen3 package
find_package(Eigen3 3.4.0 REQUIRED NO_MODULE)

# Link Eigen3 (if it is found)
if(EIGEN3_FOUND)
    target_link_libraries(${PROJECT_NAME} PRIVATE Eigen3::Eigen)
endif()

and this is what i got: Files/ && cmake .. && make && ./TFLiteImageClassification
-- The C compiler identification is GNU 9.4.0
-- The CXX compiler identification is GNU 9.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found OpenCV: /usr/local (found suitable version "4.7.0", minimum required is "4.7.0") 
-- Configuring done
-- Generating done
-- Build files have been written to: /home/myd/Desktop/Stereo-reconstruct-C++/tflite/pose_stereovision/build
Scanning dependencies of target TFLiteImageClassification
[ 50%] Building CXX object CMakeFiles/TFLiteImageClassification.dir/main.cpp.o
[100%] Linking CXX executable TFLiteImageClassification
/usr/bin/ld: CMakeFiles/TFLiteImageClassification.dir/main.cpp.o: in function `tensorflow::core::RefCounted::~RefCounted()':
main.cpp:(.text._ZN10tensorflow4core10RefCountedD2Ev[_ZN10tensorflow4core10RefCountedD5Ev]+0xf8): undefined reference to `tensorflow::internal::LogMessageFatal::LogMessageFatal(char const*, int)'
/usr/bin/ld: main.cpp:(.text._ZN10tensorflow4core10RefCountedD2Ev[_ZN10tensorflow4core10RefCountedD5Ev]+0x120): undefined reference to `tensorflow::internal::LogMessageFatal::~LogMessageFatal()'
/usr/bin/ld: CMakeFiles/TFLiteImageClassification.dir/main.cpp.o: in function `tflite::FlexDelegate::Create()':
main.cpp:(.text._ZN6tflite12FlexDelegate6CreateEv[_ZN6tflite12FlexDelegate6CreateEv]+0x40): undefined reference to `tflite::FlexDelegate::Create(std::unique_ptr<tflite::FlexDelegate, std::default_delete<tflite::FlexDelegate> >)'
/usr/bin/ld: CMakeFiles/TFLiteImageClassification.dir/main.cpp.o: in function `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >* tensorflow::internal::MakeCheckOpString<long, int>(long const&, int const&, char const*)':
main.cpp:(.text._ZN10tensorflow8internal17MakeCheckOpStringIliEEPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKT_RKT0_PKc[_ZN10tensorflow8internal17MakeCheckOpStringIliEEPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKT_RKT0_PKc]+0x37): undefined reference to `tensorflow::internal::CheckOpMessageBuilder::CheckOpMessageBuilder(char const*)'
/usr/bin/ld: main.cpp:(.text._ZN10tensorflow8internal17MakeCheckOpStringIliEEPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKT_RKT0_PKc[_ZN10tensorflow8internal17MakeCheckOpStringIliEEPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKT_RKT0_PKc]+0x61): undefined reference to `tensorflow::internal::CheckOpMessageBuilder::ForVar2()'
/usr/bin/ld: main.cpp:(.text._ZN10tensorflow8internal17MakeCheckOpStringIliEEPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKT_RKT0_PKc[_ZN10tensorflow8internal17MakeCheckOpStringIliEEPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKT_RKT0_PKc]+0x7f): undefined reference to `tensorflow::internal::CheckOpMessageBuilder::NewString[abi:cxx11]()'
/usr/bin/ld: main.cpp:(.text._ZN10tensorflow8internal17MakeCheckOpStringIliEEPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKT_RKT0_PKc[_ZN10tensorflow8internal17MakeCheckOpStringIliEEPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKT_RKT0_PKc]+0x8f): undefined reference to `tensorflow::internal::CheckOpMessageBuilder::~CheckOpMessageBuilder()'
/usr/bin/ld: main.cpp:(.text._ZN10tensorflow8internal17MakeCheckOpStringIliEEPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKT_RKT0_PKc[_ZN10tensorflow8internal17MakeCheckOpStringIliEEPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKT_RKT0_PKc]+0xb6): undefined reference to `tensorflow::internal::CheckOpMessageBuilder::~CheckOpMessageBuilder()'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/TFLiteImageClassification.dir/build.make:154: TFLiteImageClassification] Error 1
make[1]: *** [CMakeFiles/Makefile2:68: CMakeFiles/TFLiteImageClassification.dir/all] Error 2
make: *** [Makefile:84: all] Error 2.

if anyone understand this and can help me, i really appreciated!


best
yide

