C++ Tensorflow lite, undefined reference on some functions - c++

I'm trying to build and run a project using tensorflow lite on my debian 11 intel x86_64 architecture. So far I've followed the official documentation and the official github example.
Here are the steps I've followed:
On ~/Desktop/ I ran git clone https://github.com/tensorflow/tensorflow.git tensorflow_src
mkdir tflite_build & cd ~/Desktop/tflite_build
cmake ../tensorflow_src/tensorflow/lite
cmake --build . I've removed the -J flag regardless of what the docs says because it causes my pc to freeze.
mkdir ~/Desktop/tf_test & cd ~/Desktop/tf_test
Create a CMakeLists.txt and a main.cpp file inside tf_testdirectory.
Put the main code from the minimal example on the github repo provided above then this code in CMake:
cmake_minimum_required(VERSION 3.16)
project(minimal C CXX)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DTFLITE_DISABLE_TELEMETRY=1")
set(TENSORFLOW_SOURCE_DIR "" CACHE PATH
"Directory that contains the TensorFlow project" )
if(NOT TENSORFLOW_SOURCE_DIR)
get_filename_component(TENSORFLOW_SOURCE_DIR
"/home/user/Desktop/tensorflow_src" ABSOLUTE)
endif()
add_subdirectory(
"${TENSORFLOW_SOURCE_DIR}/tensorflow/lite"
"${CMAKE_CURRENT_BINARY_DIR}/tensorflow-lite" EXCLUDE_FROM_ALL)
add_executable(minimal minimal.cc)
target_link_libraries(minimal tensorflow-lite)
Created the folder tf_Test/build and ran cmake .. inside it.
After cmake is completed I run make inside the build directory and I'm getting the following error:
...
[100%] Linking CXX executable minimal
/usr/bin/ld: tensorflow-lite/libtensorflow-lite.a(interpreter.cc.o): in function `tflite::impl::Interpreter::ReportTelemetrySettings(char const*)':
interpreter.cc:(.text+0x292f): undefined reference to `tflite::telemetry::TelemetryReportSettings(TfLiteContext*, char const*, TfLiteTelemetryInterpreterSettings const*)'
/usr/bin/ld: tensorflow-lite/libtensorflow-lite.a(subgraph.cc.o): in function `tflite::Subgraph::Invoke()':
subgraph.cc:(.text+0x41c0): undefined reference to `tflite::telemetry::TelemetryReportEvent(TfLiteContext*, char const*, TfLiteStatus)'
/usr/bin/ld: tensorflow-lite/libtensorflow-lite.a(subgraph.cc.o): in function `tflite::Subgraph::ModifyGraphWithDelegate(TfLiteDelegate*)':
subgraph.cc:(.text+0x6ad0): undefined reference to `tflite::telemetry::TelemetryReportEvent(TfLiteContext*, char const*, TfLiteStatus)'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/minimal.dir/build.make:184: minimal] Error 1
make[1]: *** [CMakeFiles/Makefile2:1408: CMakeFiles/minimal.dir/all] Error 2
make: *** [Makefile:149: all] Error 2
Notice that it's not saying this for all the functions. For example this works std::unique_ptr<tflite::FlatBufferModel> model = tflite::FlatBufferModel::BuildFromFile(filename); without errors.
These tree are causing trouble: tflite::ops::builtin::BuiltinOpResolver resolver; std::unique_ptr<tflite::Interpreter> interpreter; tflite::InterpreterBuilder(*model, resolver)(&interpreter);
Note: I've trimmed some code compare to the github example for the sake of testing it, so only the above 4 lines are present on my main.
Why am I getting this error? I've tried compiling with bazel aswell but I'm getting the same error. What am I missing?

Probably it is missing from the CMakeLists file
Update CMakeLists.txt and add
tensorflow/lite/profiling/telemetry/telemetry.cc and
tensorflow/lite/profiling/telemetry/telemetry.h
to TFLITE_PROFILER_SRCS
It is also worth creating issue on Tensorflow repo for the team

Related

Undefined reference using external library with CMake and Conan

I am trying to develop a program that communicates with a PCSC USB reader using Conan and CMake with the LibLogicalAccess library. I followed the instructions of building and installing the library which seemed to have gone fine. I created a small simple console project with a "main.cpp" file. Following the C++ guide on the wiki of the library I tried to call a function from the library which resulted in a "Undefined reference to function. I know there are a lot of topics covering this but I have read as many as I could but could not seem to find the right solution.
I don't have much experience with Ubuntu/CMake/Conan/C++ so it might as well be a very simple fix.
OS: Kubuntu 18.04
Lang: C++
Related software: LibLogicalAccess
2.2.1,
CMake 3.17.1,
Conan 1.25.0
main.cpp
#include <iostream>
#include <logicalaccess/dynlibrary/librarymanager.hpp>
#include <logicalaccess/readerproviders/readerconfiguration.hpp>
#include <logicalaccess/cards/chip.hpp>
int main()
{
std::cout << "Program started\n";
// Reader configuration object to store reader provider and reader unit selection.
std::shared_ptr<logicalaccess::ReaderConfiguration> readerConfig(new logicalaccess::ReaderConfiguration());
// Set PCSC ReaderProvider by calling the Library Manager which will load the function from the corresponding plug-in
readerConfig->setReaderProvider(logicalaccess::LibraryManager::getInstance()->getReaderProvider("PCSC"));
std::cout << "after..\n";
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.16)
project(project)
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup(TARGETS)
set(CMAKE_CXX_FLAGS "-I /usr/include/PCSC")
add_executable(project main.cpp)
target_link_libraries(project PUBLIC CONAN_PKG::LogicalAccess)
When I try to build the program using cmake --build . this is the output:
[100%] Linking CXX executable bin/project
CMakeFiles/project.dir/main.cpp.o: In function `main':
main.cpp:(.text+0x140): undefined reference to `logicalaccess::LibraryManager::getReaderProvider(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
collect2: error: ld returned 1 exit status
CMakeFiles/project.dir/build.make:191: recipe for target 'bin/project' failed
make[2]: *** [bin/project] Error 1
CMakeFiles/Makefile2:95: recipe for target 'CMakeFiles/project.dir/all' failed
make[1]: *** [CMakeFiles/project.dir/all] Error 2
Makefile:103: recipe for target 'all' failed
make: *** [all] Error 2
The weird part is that the first line of code: std::shared_ptr<logicalaccess::ReaderConfiguration> readerConfig(...) works fine and the second line of code gives an undefined reference.
I have tried other functions in the same file which give the same result. The file compiles and runs fine when I remove the last "setReaderProvider" line of code. Also tried a lot of different little adjustments regarding the conanfile.txt and CMakeLists.txt.
Your error says:
main.cpp:(.text+0x140): undefined reference to `logicalaccess::LibraryManager::getReaderProvider(std::__cxx11::basic_string, std::allocator > const&)'
It occurs because your CMake is using libstdc++11 to link, however, Conan is configured to use libstdc++ due backward compatibility.
You need to update your default libcxx:
conan profile update settings.compiler.libcxx=libstdc++11 default
Please, read this section in Conan docs How to Manage GCC ABI to get more information.
Also, it's explained on step 5 of Getting Started.
Now when building again, you will need that your local packages won't be available, because it's a new package, using different settings, so you will need to install, or build from sources. The link to libstdc++11 is automatically managed by Conan, it passes the definitions to CMake.
Regards!

Undefined reference in libfreenect c++ wrapper

I want to print the number of connected devices with libfreenect in c++. As described in https://openkinect.org/wiki/C%2B%2B_Wrapper
i include the libfreenect.hpp header file in my TestKinectConnection.cpp.
My TestKinectConnection.cpp:
#include <iostream>
#include "libfreenect.hpp"
using namespace std;
int main(void) {
Freenect::Freenect nect;
freenect_context *f_ctx;
cout << nect.deviceCount() << endl;
return(0);
}
When i build with cmake --build build -- -j3 the terminal shows
CMakeFiles/projektinf.dir/src/main/TestKinectConnection.cpp.o: In function `Freenect::Freenect::Freenect()':
TestKinectConnection.cpp:(.text._ZN8Freenect8FreenectC2Ev[_ZN8Freenect8FreenectC5Ev]+0x40): undefined reference to `freenect_init'
TestKinectConnection.cpp:(.text._ZN8Freenect8FreenectC2Ev[_ZN8Freenect8FreenectC5Ev]+0x90): undefined reference to `freenect_select_subdevices'
TestKinectConnection.cpp:(.text._ZN8Freenect8FreenectC2Ev[_ZN8Freenect8FreenectC5Ev]+0xb0): undefined reference to `pthread_create'
CMakeFiles/projektinf.dir/src/main/TestKinectConnection.cpp.o: In function `Freenect::Freenect::~Freenect()':
TestKinectConnection.cpp:(.text._ZN8Freenect8FreenectD2Ev[_ZN8Freenect8FreenectD5Ev]+0xa5): undefined reference to `pthread_join'
TestKinectConnection.cpp:(.text._ZN8Freenect8FreenectD2Ev[_ZN8Freenect8FreenectD5Ev]+0xb4): undefined reference to `freenect_shutdown'
CMakeFiles/projektinf.dir/src/main/TestKinectConnection.cpp.o: In function `Freenect::Freenect::deviceCount()':
TestKinectConnection.cpp:(.text._ZN8Freenect8Freenect11deviceCountEv[_ZN8Freenect8Freenect11deviceCountEv]+0x17): undefined reference to `freenect_num_devices'
CMakeFiles/projektinf.dir/src/main/TestKinectConnection.cpp.o: In function `Freenect::Freenect::operator()()':
TestKinectConnection.cpp:(.text._ZN8Freenect8FreenectclEv[_ZN8Freenect8FreenectclEv]+0x4f): undefined reference to `freenect_process_events_timeout'
collect2: error: ld returned 1 exit status
CMakeFiles/projektinf.dir/build.make:95: recipe for target '../bin/projektinf' failed
make[2]: *** [../bin/projektinf] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/projektinf.dir/all' failed
make[1]: *** [CMakeFiles/projektinf.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
My CMakeLists.txt:
# Specify the minimum version for CMake
cmake_minimum_required(VERSION 3.10)
# Project's name
project(projektinf)
# Set the output folder where your program will be created
set(CMAKE_BINARY_DIR ${CMAKE_SOURCE_DIR}/bin)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/bin)
set(LIBRARY_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/lib)
set(PROJECT_SOURCE_DIR ${CMAKE_SOURCE_DIR})
set(EXTERNAL_INSTALL_LOCATION ${CMAKE_SOURCE_DIR}/lib)
# The following folder will be included
include_directories("${PROJECT_SOURCE_DIR}/src/include")
add_executable(projektinf ${PROJECT_SOURCE_DIR}/src/main/TestKinectConnection.cpp)
add_library(libfreenect ${PROJECT_SOURCE_DIR}/src/include/libfreenect.hpp)
target_link_libraries(projektinf PUBLIC libfreenect)
set_target_properties(libfreenect PROPERTIES LINKER_LANGUAGE CXX)
https://github.com/OpenKinect/libfreenect/blob/master/wrappers/cpp/libfreenect.hpp
https://github.com/OpenKinect/libfreenect/blob/master/include/libfreenect.h
In short summary, libfreenect.hpp tries to include libfreenect.h und completely fails.
From your question it is unclear whether you installed libfreenect globally or simply bundled the library with your code.
If you installed it globally, you forgot to tell CMake that the libfreenect target needs to link with libfreenect.so using the -lfreenect linker flag.
Adding the following should fix that:
set_property(TARGET libfreenect PROPERTY INTERFACE_LINK_LIBRARIES -lfreenect)
The proper approach is to make libfreenect an IMPORTED target, as documented in "It's time to do CMake right".
If you bundled the .cpp with your code, you need to add the .cpp file to the add_library statement that defines the libfreenect target.

undefined reference to `USER_ERROR__inconsistent_build_configuration__see_dlib_faq_1_'

So when I use the command make I get the following error:
[jalal#goku c++]$ make
[ 25%] Building CXX object CMakeFiles/TestSVM.dir/TestSVM.cpp.o
[ 50%] Linking CXX executable TestSVM
CMakeFiles/TestSVM.dir/TestSVM.cpp.o: In function `__static_initialization_and_destruction_0(int, int)':
TestSVM.cpp:(.text+0x28c3): undefined reference to `USER_ERROR__inconsistent_build_configuration__see_dlib_faq_1_'
TestSVM.cpp:(.text+0x28cf): undefined reference to `USER_ERROR__inconsistent_build_configuration__see_dlib_faq_2'
collect2: error: ld returned 1 exit status
make[2]: *** [TestSVM] Error 1
make[1]: *** [CMakeFiles/TestSVM.dir/all] Error 2
make: *** [all] Error 2
The files look like:
[jalal#goku c++]$ ls
CMakeCache.txt CMakeFiles CMakeLists.txt Makefile TestSVM.cpp TrainSVM.cpp cmake_install.cmake json.hpp trained_svms
I built the dlib in /scratch2/dlib from source using this repo:https://github.com/davisking/dlib
by following these commands:
mkdir build; cd build; cmake .. -DUSE_AVX_INSTRUCTIONS=1; cmake --build .
I am not sure how I should fix this problem.
I followed the following commands in the git repo I cloned:
$ cd c++
$ cmake .
$ make
https://github.com/sausax/pose_estimation
Additionally not sure how to make sense out of this for fixing the bug
https://github.com/davisking/dlib/blob/master/dlib/test_for_odr_violations.cpp
As the commenter said, don't forget to link to dlib! You can do this in CMake with:
target_link_libraries(<YOUR APP> dlib::dlib)
I wasn't able to get plain dlib to link, but I tried ${dlib_LIBRARIES} and finally CMake clued me in with:
CMake Warning at lib/cmake/dlib/dlibConfig.cmake:42 (message):
The variable 'dlib_LIBRARIES' is deprecated! Instead, simply use
target_link_libraries(your_app dlib::dlib). See
http://dlib.net/examples/CMakeLists.txt.html for an example.
I too was distracted by the ENABLE_ASSERTS pointing to the FAQ, but that was due to me having the includes without the linked library.

Can't compile with CMake and ncurses

I'm aware similar questions have been posted, but I can't see how they relate to my case. I will preface this question by saying I'm not very familiar with CMake, so it's entirely possible this is a quick fix and I just don't see it!
I am collaborating on a project, and yesterday my teammate added the ncurses library to the project to build a terminal GUI. Ever since ncurses was added, I haven't been able to compile my project. However, I did install all 6 ncurses-* packages, so it should behave with my machine.
I have pulled down the latest version of the master branch from our GitHub repo, which compiles and runs perfectly on his machine. However it won't even compile on mine.
System:
Linux Mint 18.1 Cinnamon
CMake Version 3.5.1
Things I've tried:
I deleted CMakeCache.txt and reloaded it
I deleted my entire CMake build directory and redid make
CMakeLists.txt:
cmake_minimum_required(VERSION 2.8)
project(irc)
set(SHARED_FLAGS " -Wall -Wextra -Wshadow -Werror -g -D_POSIX_C_SOURCE=200809L -lncurses")
set(CMAKE_C_FLAGS "-std=c11 ${SHARED_FLAGS}")
set(CMAKE_CXX_FLAGS "-std=c++11 ${SHARED_FLAGS}")
include_directories(include)
add_library(client_core SHARED src/client/irc_client.c)
add_executable(client src/client/irc_client_gui.c)
add_executable(server src/server/irc_server.c)
// SOLUTION - MISSING LINE
target_link_libraries(client ncurses)
install(FILES include/irc_client.h DESTINATION include)
install(FILES include/irc_server.h DESTINATION include)
CMakeOutput:
CMakeFiles/client.dir/src/client/irc_client_gui.c.o: In function `main':
/home/chrisjansson/Documents/irc/src/client/irc_client_gui.c:17:
undefined reference to `initscr'
/home/chrisjansson/Documents/irc/src/client/irc_client_gui.c:18:
undefined reference to `stdscr'
/home/chrisjansson/Documents/irc/src/client/irc_client_gui.c:18:
undefined reference to `stdscr'
/home/chrisjansson/Documents/irc/src/client/irc_client_gui.c:18:
undefined reference to `stdscr'
/home/chrisjansson/Documents/irc/src/client/irc_client_gui.c:18:
undefined reference to `stdscr'
/home/chrisjansson/Documents/irc/src/client/irc_client_gui.c:19:
undefined reference to `mvprintw'
/home/chrisjansson/Documents/irc/src/client/irc_client_gui.c:21:
undefined reference to `stdscr'
/home/chrisjansson/Documents/irc/src/client/irc_client_gui.c:21:
undefined reference to `wgetnstr'
/home/chrisjansson/Documents/irc/src/client/irc_client_gui.c:22:
undefined reference to `LINES'
/home/chrisjansson/Documents/irc/src/client/irc_client_gui.c:22:
undefined reference to `mvprintw'
/home/chrisjansson/Documents/irc/src/client/irc_client_gui.c:23:
undefined reference to `stdscr'
/home/chrisjansson/Documents/irc/src/client/irc_client_gui.c:23:
undefined reference to `wgetch'
/home/chrisjansson/Documents/irc/src/client/irc_client_gui.c:24:
undefined reference to `endwin'
collect2: error: ld returned 1 exit status
CMakeFiles/client.dir/build.make:94: recipe for target 'client' failed
make[2]: *** [client] Error 1
CMakeFiles/Makefile2:104: recipe for target 'CMakeFiles/client.dir/all'
failed
make[1]: *** [CMakeFiles/client.dir/all] Error 2
Makefile:127: recipe for target 'all' failed
make: *** [all] Error 2
Interestingly enough, if I copy/paste irc_client_gui.c somewhere else on my machine, compile it manually with gcc and run it, it works perfectly. So the issue isn't my machine, it's something with CMake trying to compile my entire project. Any ideas? Thanks so much!
The solution is to add a line like this:
target_link_libraries(client ncurses)
This tells CMake that when it is linking the client target, it should use the -lncurses option to link in the ncurses library.

Compiling Qt5 hello world .cpp file under Windows using MinGW-w64 gives "undefined reference" error

When I run make to compile the Makefile produced by cmake, to compile an hello world example of a Qt5 application, the compilation fails with the following error:
Why is the compilation failing?
(details of what exactly I'm trying to do follow)
I'm under Windows 10, using the Qt5.5 binaries downloaded from Qt official website, mingw-w64 gcc and g++ shipped with WinBuilds, and cmake v3.6 downloaded from the official website and installed with the Windows win64-x64 Installer.
I'm trying to compile the following hello world test file, provided in Qt5's official wiki:
#include <QCoreApplication>
int main(int argc, char *argv[])
{
QCoreApplication app (argc, argv);
return app.exec();
}
The Makefile is built successfully using the command cmake -G "MinGW Makefiles" .. from a folder called build inside the directory containing the .cpp file. The following CMakeLists.txt file (taken from Qt5's cmake wiki page, with the addition of the specification of the CMAKE_PREFIX_PATH variable, which is required as for example discussed in this SO post) was used:
cmake_minimum_required(VERSION 2.8.11)
SET(CMAKE_C_COMPILER C:/WinBuilds/bin/x86_64-w64-mingw32-gcc-4.8.3.exe)
SET(CMAKE_CXX_COMPILER C:/WinBuilds/bin/x86_64-w64-mingw32-g++-4.8.3.exe)
project(testproject)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
#set(CMAKE_PREFIX_PATH "C:/WinBuilds/lib64/cmake")
set(CMAKE_PREFIX_PATH "C:/Qt/5.5/mingw492_32/lib/cmake")
find_package(Qt5Widgets)
add_executable(testfile WIN32 test.cpp)
target_link_libraries(testfile Qt5::Widgets)
(I did not use the cmake shipped with Qt5 as that did not work).
Now, the problem arises when I run make (or more precisely, mingw32-make, again shipped with WinBuilds) on the Makefile produced by cmake.
When I do this, the compilation fails with the following error (same one showed in the screenshot above):
CMakeFiles\testfile.dir/objects.a(test.cpp.obj):test.cpp:(.text+0x35): undefined reference to `__imp__ZN16QCoreApplicationC1ERiPPci'
CMakeFiles\testfile.dir/objects.a(test.cpp.obj):test.cpp:(.text+0x3e): undefined reference to `__imp__ZN16QCoreApplication4execEv'
CMakeFiles\testfile.dir/objects.a(test.cpp.obj):test.cpp:(.text+0x50): undefined reference to `__imp__ZN16QCoreApplicationD1Ev'
CMakeFiles\testfile.dir/objects.a(test.cpp.obj):test.cpp:(.text+0x67): undefined reference to `__imp__ZN16QCoreApplicationD1Ev'
c:/winbuilds/bin/../lib64/gcc/x86_64-w64-mingw32/4.8.3/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\testfile.dir/objects.a(test.cpp.obj): bad reloc address 0xc in section `.xdata'
c:/winbuilds/bin/../lib64/gcc/x86_64-w64-mingw32/4.8.3/../../../../x86_64-w64-mingw32/bin/ld.exe: final link failed: Invalid operation
collect2.exe: error: ld returned 1 exit status
CMakeFiles\testfile.dir\build.make:127: recipe for target 'testfile.exe' failed
mingw32-make[2]: *** [testfile.exe] Error 1
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/testfile.dir/all' failed
mingw32-make[1]: *** [CMakeFiles/testfile.dir/all] Error 2
Makefile:82: recipe for target 'all' failed
mingw32-make: *** [all] Error 2
Why is the compilation failing?
A similar undefined reference error was reported in this other SO question, but the reason there seemed to be different than the present case.
The problem is you are using 32 bit mingw compiled Qt binaries when you are trying to build a 64 bit Qt application. You need to use 64 bit Qt binaries compiled with mingw for this to work.